javascript - 修复了向下滚动时标题摇晃的问题

标签 javascript jquery css

当我使用 fixed header 时,添加 is-sticky 时它会摇晃 CSS 类。它以 top:0; 开头,因为它是向下滚动时的动画。我希望它以不会引起明显抖动的方式平稳地固定在顶部。例如:http://www.lazada.com.my/
这是我的 demo .

$(window).load(function(){
  $(window).scroll(function(){
    if($(window).scrollTop()>0){
      if( ! ($('#scroller').hasClass('is-sticky'))) {
        $('#scroller')
        .addClass('is-sticky')
        .css('top',9)
        .animate({
          'top': 84
        },'1000');
      }



    } else {
      if($('#scroller').hasClass('is-sticky')) {
        $('#scroller')
        .removeClass('is-sticky')
        .css('top',9)
        .animate({
          'top':84
        },1000);

      }
    }
  });
});
body{
    height:1000px;
    margin:0;
    padding:0;
    position:relative;
}
#scroller {
    position: absolute;
    left: 0;
    top: 84px;
    width: 100%;
    z-index: 30;
    background:#CCC;
    height:20px;
}
#scroller.is-sticky {
    position: fixed;
    width: 100%;
    left: 0;
    top: 9px;
    margin-top: -31px;
    height: 53px;
    z-index: 701;
    background: #CCC;
    opacity: .97;
    filter: alpha(opacity = 97);
    -webkit-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.1);
    box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<body>
  <div id="scroller">Some controls</div>
</body>

最佳答案

从您链接的网站来看,效果实际上很容易做到。

首先,您希望标题元素具有position: fixed;无需通过 javascript 动态添加。默认情况下它应该具有此属性(如您链接的网站中所示)。

他们所做的是在某个滚动点处隐藏 header 中的顶部导航。

您可以使用 jquery 非常简单地完成此操作。

DEMO

var $el = $('header .two');
$(window).scroll(function(){
    if ($(this).scrollTop() > 200) {
        $el.addClass('hide');
    } else {
        $el.removeClass('hide');
    }
});
* {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
}

header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    text-align: center;
}

header .one {
    height: 20px;
    width: 100%;
    background: lime;
    position: relative;
    z-index: 10;
}

header .one.hide {
    height: 0;
}

header .two {
    background: red;
    height: 40px;
    position: relative;
    z-index: 20;
    -webkit-transition: -webkit-transform .25s;
    transition: transform .25s;
}

header .two.hide {
    -webkit-transform: translateY(-20px);
    transform: translateY(-20px);
}

main {
    background: lightblue;
    height: 1200px;
    width: 100%;
    padding-top: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<header>
    <div class="one">div</div>
    <div class="two">fixed</div>
</header>
<main>
    content
</main>
<footer>footer</footer>

关于javascript - 修复了向下滚动时标题摇晃的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716502/

相关文章:

html - 方框中的可变图像纵横比

css - 在 jCarousel 中定位链接

javascript - 方法 piSession.buildPageInteractionSession 无效

jquery - 仅在 IE10 Metro 模式下出现奇怪的过渡行为

javascript - 如何检查光标是否在文本区域内的空行上?

javascript - 使用 .attr 向元素添加多个类?

Jquery 在动画之前切换显示?

javascript - 如何在 ruby​​ on Rails 中使用 javascript 获取 datetime_select 字段的值

javascript 在 Chrome 中有效,在 IE 中无效

javascript - 关于laravel图片上传ajax的问题