导航栏中的徽标垂直对齐不起作用

我正在尝试将“徽标”与导航栏垂直对齐。我希望它在正常状态和折叠状态下居中。

我尝试了一些可能的解决方案,但到目前为止似乎没有任何效果。我尝试添加不同的头寸类型,例如,我尝试使用保证金顶部和底部 50%。可能有一个非常简单的方法来实现这一切。

https://jsfiddle.net/qb0dj3yp/90/

提前致谢!

//header Effekt beim scrollen


$(function() {

  var shrinkHeader = 100;

  $(window).scroll(function() {

    var scroll = getCurrentScroll();

    if (scroll >= shrinkHeader) {

      $('#navbar').addClass('shrink');

    } else {

      $('#navbar').removeClass('shrink');

    }

  });


  function getCurrentScroll() {

    return window.pageYOffset || document.documentElement.scrollTop;

  }

});

// JavaScript Document


$(document).ready(function() {


  var navTop = $('#navbar').offset().top;

  var navHeight = $('#navbar').height();

  var windowH = $(window).height();


  $('.section').height(windowH);


  $(document).scroll(function() {

    var st = $(this).scrollTop();


    //for the nav bar:

    if (st > navTop) {

      $('#navbar').addClass('fix');

      $('.section:eq(0)').css({

        'margin-top': navHeight

      }); //fix  scrolling issue due to the fix nav bar

    } else {

      $('#navbar').removeClass('fix');

      $('.section:eq(0)').css({

        'margin-top': '0'

      });

    }


    $('.section').each(function(index, element) {

      if (st + navHeight > $(this).offset().top && st + navHeight <= $(this).offset().top + $(this).height()) {

        $(this).addClass('active');


        var id = $(this).attr('id');

        $('a[href="#' + id + '"]').parent('li').addClass('active');

        // or $('#nav li:eq('+index+')').addClass('active');

      } else {

        $(this).removeClass('active');


        var id = $(this).attr('id');

        $('a[href="#' + id + '"]').parent('li').removeClass('active');

        //or $('#nav li:eq('+index+')').removeClass('active');

      }


    });


  });


});




//


波斯汪
浏览 124回答 3
3回答

慕容森

我调整了您的 JSFiddle 以display: flex使用align-items: center https://jsfiddle.net/xv29r1sm/

holdtom

也许这就是你想要的在 css 部分中,搜索#navbar.shrink img。从那里删除这一行: margin-top: -30px;或者像这样评论它: /* margin-top: -30px; */现在它应该垂直居中。那是因为 margin-top 属性会将图像进一步拖动到顶部,这在您的情况下是不需要的。&nbsp; &nbsp; //header Effekt beim scrollen$(function() {&nbsp; var shrinkHeader = 100;&nbsp; $(window).scroll(function() {&nbsp; &nbsp; var scroll = getCurrentScroll();&nbsp; &nbsp; if (scroll >= shrinkHeader) {&nbsp; &nbsp; &nbsp; $('#navbar').addClass('shrink');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $('#navbar').removeClass('shrink');&nbsp; &nbsp; }&nbsp; });&nbsp; function getCurrentScroll() {&nbsp; &nbsp; return window.pageYOffset || document.documentElement.scrollTop;&nbsp; }});// JavaScript Document$(document).ready(function() {&nbsp; var navTop = $('#navbar').offset().top;&nbsp; var navHeight = $('#navbar').height();&nbsp; var windowH = $(window).height();&nbsp; $('.section').height(windowH);&nbsp; $(document).scroll(function() {&nbsp; &nbsp; var st = $(this).scrollTop();&nbsp; &nbsp; //for the nav bar:&nbsp; &nbsp; if (st > navTop) {&nbsp; &nbsp; &nbsp; $('#navbar').addClass('fix');&nbsp; &nbsp; &nbsp; $('.section:eq(0)').css({&nbsp; &nbsp; &nbsp; &nbsp; 'margin-top': navHeight&nbsp; &nbsp; &nbsp; }); //fix&nbsp; scrolling issue due to the fix nav bar&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $('#navbar').removeClass('fix');&nbsp; &nbsp; &nbsp; $('.section:eq(0)').css({&nbsp; &nbsp; &nbsp; &nbsp; 'margin-top': '0'&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; $('.section').each(function(index, element) {&nbsp; &nbsp; &nbsp; if (st + navHeight > $(this).offset().top && st + navHeight <= $(this).offset().top + $(this).height()) {&nbsp; &nbsp; &nbsp; &nbsp; $(this).addClass('active');&nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).attr('id');&nbsp; &nbsp; &nbsp; &nbsp; $('a[href="#' + id + '"]').parent('li').addClass('active');&nbsp; &nbsp; &nbsp; &nbsp; // or $('#nav li:eq('+index+')').addClass('active');&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $(this).removeClass('active');&nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).attr('id');&nbsp; &nbsp; &nbsp; &nbsp; $('a[href="#' + id + '"]').parent('li').removeClass('active');&nbsp; &nbsp; &nbsp; &nbsp; //or $('#nav li:eq('+index+')').removeClass('active');&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; });});//&nbsp; &nbsp; @charset "utf-8";/* CSS Document */* {&nbsp; font-family: 'Roboto', sans-serif;}#container {&nbsp; width: 1280px;&nbsp; height: 100%;&nbsp; margin-left: auto;&nbsp; margin-right: auto;}body {&nbsp; background-color: grey;&nbsp; margin: 128px 0 0 0;}/* Navigation */#navlinks {&nbsp; margin: 0;}ul {&nbsp; color: black;&nbsp; list-style: none;&nbsp; float: right;&nbsp; margin-right: 20px;&nbsp; padding-top: 30px;}ul li {&nbsp; display: inline-table;&nbsp; margin-left: 5px;&nbsp; padding: 5px;&nbsp; border-bottom: 1.5px solid;&nbsp; border-bottom-color: white;}ul li a {&nbsp; color: black;&nbsp; text-decoration: none;&nbsp; padding: 10px;}#logo {&nbsp; height: 128px;&nbsp; vertical-align: middle;}/* Smart Navbar / weiß, wo man auf der Seite ist von https://stackoverflow.com/questions/19697696/change-underline-of-active-nav-by-section */#navbar.fix {&nbsp; position: fixed;&nbsp; top: 0;}#navbar li.active {&nbsp; border-bottom: 1.5px solid;&nbsp; border-bottom-color: #f6bd60;}/* Smart Navbar Ende *//* fixed Navigation von https://codepen.io/malZiiirA/pen/cbfED?css-preprocessor=none */#navbar {&nbsp; border-bottom-style: solid;&nbsp; border-bottom-width: 1.25px;&nbsp; box-shadow: 0px 2.5px 5px rgba(0, 0, 0, 0.2);&nbsp; background-color: white;&nbsp; height: 128px;&nbsp; transition: 0.32s;&nbsp; position: fixed;&nbsp; top: 0;&nbsp; width: 1280px;}#navbar.shrink {&nbsp; height: 80px;&nbsp; line-height: 18px;}#navbar li {&nbsp; font-size: 1.2rem;&nbsp; font-weight: normal;&nbsp; -webkit-transition: all 0.3s;&nbsp; -moz-transition: all 0.3s;&nbsp; transition: all 0.3s;}#navbar.shrink li {&nbsp; font-size: 1.2rem;&nbsp; margin-top: -30px;&nbsp; -webkit-transition: all 0.3s;&nbsp; -moz-transition: all 0.3s;&nbsp; transition: all 0.3s;}#navbar img {&nbsp; height: 128px;&nbsp; -webkit-transition: all 0.3s;&nbsp; -moz-transition: all 0.3s;&nbsp; transition: all 0.3s;}#navbar.shrink img {&nbsp; height: 80px;&nbsp; /*margin-top: -30px;*/&nbsp; -webkit-transition: all 0.3s;&nbsp; -moz-transition: all 0.3s;&nbsp; transition: all 0.3s;}/* fixed nav Ende *//*#spacer {&nbsp; height: 128px;&nbsp; border-bottom: 4px solid;}&nbsp; margin-top: 0 !important;*/#Home {&nbsp; height: 100%;&nbsp; display: block;&nbsp; margin-top: -80px;&nbsp; padding-top: 80px;&nbsp; z-index: -1;&nbsp; background-color: darkred;&nbsp; position: relative;&nbsp; top: -80px;}#UberUns {&nbsp; height: 100%;&nbsp; display: block;&nbsp; margin-top: -80px;&nbsp; padding-top: 80px;&nbsp; z-index: -2;&nbsp; background-color: lightblue;&nbsp; position: relative;&nbsp; top: -80px;}#Aktionen {&nbsp; height: 100%;&nbsp; display: block;&nbsp; margin-top: -80px;&nbsp; padding-top: 80px;&nbsp; z-index: -3;&nbsp; background-color: blue;&nbsp; position: relative;&nbsp; top: -80px;}#Terminvereinbarung {&nbsp; height: 100%;&nbsp; display: block;&nbsp; margin-top: -80px;&nbsp; padding-top: 80px;&nbsp; z-index: -4;&nbsp; background-color: red;&nbsp; position: relative;&nbsp; top: -80px;}#Infos {&nbsp; height: 100%;&nbsp; display: block;&nbsp; margin-top: -80px;&nbsp; padding-top: 80px;&nbsp; z-index: -5;&nbsp; background-color: yellow;&nbsp; position: relative;&nbsp; top: -80px;}/* Hover Effekt bei Navigation von https://github.com/IanLunn/Hover/blob/master/css/hover.css */.hvr-sweep-to-top {&nbsp; display: inline-block;&nbsp; vertical-align: middle;&nbsp; -webkit-transform: perspective(1px) translateZ(0);&nbsp; transform: perspective(1px) translateZ(0);&nbsp; box-shadow: 0 0 1px rgba(0, 0, 0, 0);&nbsp; position: relative;&nbsp; -webkit-transition-property: color;&nbsp; transition-property: color;&nbsp; -webkit-transition-duration: 0.3s;&nbsp; transition-duration: 0.3s;}.hvr-sweep-to-top:before {&nbsp; content: "";&nbsp; position: absolute;&nbsp; z-index: -1;&nbsp; top: 0;&nbsp; left: 0;&nbsp; right: 0;&nbsp; bottom: 0;&nbsp; background: #f6bd60;&nbsp; -webkit-transform: scaleY(0);&nbsp; transform: scaleY(0);&nbsp; -webkit-transform-origin: 50% 100%;&nbsp; transform-origin: 50% 100%;&nbsp; -webkit-transition-property: transform;&nbsp; transition-property: transform;&nbsp; -webkit-transition-duration: 0.3s;&nbsp; transition-duration: 0.3s;&nbsp; -webkit-transition-timing-function: ease-out;&nbsp; transition-timing-function: ease-out;}.hvr-sweep-to-top:hover,.hvr-sweep-to-top:focus,.hvr-sweep-to-top:active {&nbsp; color: white;}.hvr-sweep-to-top:hover:before,.hvr-sweep-to-top:focus:before,.hvr-sweep-to-top:active:before {&nbsp; -webkit-transform: scaleY(1);&nbsp; transform: scaleY(1);}/* Hover Effekt Ende *//* Loader */.loader {&nbsp; display: inline-block;&nbsp; width: 30px;&nbsp; height: 30px;&nbsp; position: relative;&nbsp; border: 4px solid #Fff;&nbsp; animation: loader 2s infinite ease;}.loader-inner {&nbsp; vertical-align: top;&nbsp; display: inline-block;&nbsp; width: 100%;&nbsp; background-color: #fff;&nbsp; animation: loader-inner 2s infinite ease-in;}@keyframes loader {&nbsp; 0% {&nbsp; &nbsp; transform: rotate(0deg);&nbsp; }&nbsp; 25% {&nbsp; &nbsp; transform: rotate(180deg);&nbsp; }&nbsp; 50% {&nbsp; &nbsp; transform: rotate(180deg);&nbsp; }&nbsp; 75% {&nbsp; &nbsp; transform: rotate(360deg);&nbsp; }&nbsp; 100% {&nbsp; &nbsp; transform: rotate(360deg);&nbsp; }}@keyframes loader-inner {&nbsp; 0% {&nbsp; &nbsp; height: 0%;&nbsp; }&nbsp; 25% {&nbsp; &nbsp; height: 0%;&nbsp; }&nbsp; 50% {&nbsp; &nbsp; height: 100%;&nbsp; }&nbsp; 75% {&nbsp; &nbsp; height: 100%;&nbsp; }&nbsp; 100% {&nbsp; &nbsp; height: 0%;&nbsp; }}.loader-wrapper {&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; position: absolute;&nbsp; top: 0;&nbsp; left: 0;&nbsp; background-color: #242f3f;&nbsp; display: flex;&nbsp; align-items: center;&nbsp; justify-content: center;}/* loader Ende */<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>OptikTack</title>&nbsp; &nbsp; <link href="style.css" rel="stylesheet" type="text/css">&nbsp; &nbsp; <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div id="container">&nbsp; &nbsp; &nbsp; <div class="body">&nbsp; &nbsp; &nbsp; <!-- Navigation -->&nbsp; &nbsp; &nbsp; &nbsp; <nav id="navbar">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script src="javascript/navbar fixed.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#Home" id="logo"><img src="https://www.w3schools.com/images/lamp.jpg"></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="hvr-sweep-to-top active"><a href="#Home">Home</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="hvr-sweep-to-top"><a href="#UberUns">Wir über uns</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="hvr-sweep-to-top"><a href="#Aktionen">Aktionen</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="hvr-sweep-to-top"><a href="#Terminvereinbarung">Terminvereinbarung</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="hvr-sweep-to-top"><a href="#Infos">Infos</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </nav>&nbsp; &nbsp; &nbsp; &nbsp; <!-- body -->&nbsp; &nbsp; &nbsp; &nbsp; <div id="spacer"></div>&nbsp; &nbsp; &nbsp; &nbsp; <!-- home section -->&nbsp; &nbsp; &nbsp; &nbsp; <section id="Home" class="section">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </section>&nbsp; &nbsp; &nbsp; &nbsp; <!-- UberUns section -->&nbsp; &nbsp; &nbsp; &nbsp; <section id="UberUns" class="section">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>section 2</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </section>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Aktionen section -->&nbsp; &nbsp; &nbsp; &nbsp; <div id="reference"></div>&nbsp; &nbsp; &nbsp; &nbsp; <section id="Aktionen" class="section">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>section 3</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </section>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Terminvereinbarung section -->&nbsp; &nbsp; &nbsp; &nbsp; <section id="Terminvereinbarung" class="section">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>section 4</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </section>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Infos section -->&nbsp; &nbsp; &nbsp; &nbsp; <section id="Infos" class="section">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>section 5</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </section>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="loader-wrapper">&nbsp; &nbsp; &nbsp; <span class="loader"><span class="loader-inner"></span></span>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; $(window).on("load", function() {&nbsp; &nbsp; &nbsp; &nbsp; $(".loader-wrapper").fadeOut("slow");&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; $('a').click(function() {&nbsp; &nbsp; &nbsp; &nbsp; $('html, body').animate({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollTop: $($(this).attr('href')).offset().top&nbsp; &nbsp; &nbsp; &nbsp; }, 500);&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; </body></html>

慕的地10843

margin: auto是你的朋友吗?将其应用于子 div。另请检查弹性盒的用法
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript