javascript - 需要帮助使我的 HTML 和 CSS 代码更有效地实现导航效果

标签 javascript jquery html css transit

$(document).ready(function(){       
    $("#sticky-header").hide();
});   

$(window).scroll(function(){
   if( $(document).scrollTop() > 160 ) {
      $.fx.speeds._default = 300;
      $('#sticky-header').show();
      $("#sticky-header").transition({ y: 60 });
   } else {
      $.fx.speeds._default = 0;
      $('#sticky-header').clearQueue().transition({ y: 0 });
      $('#sticky-header').hide();
   }
});

这是我的代码:http://jsfiddle.net/de74ezo5/

我试图在滚动经过顶部标题时向下滑动新导航,然后在返回时将其隐藏。有没有更有效的方法来做到这一点?可能与 CSS 过渡?我的方法对我来说显得笨拙且效率低下。动画有时会跳动。

最佳答案

您可以尝试使用 jQuery .animate() 和一些缓动。

$(window).scroll(function(){
   if( $(document).scrollTop() > 160 ) {

       $("#header").stop(true,false).animate({top:"-160px"}, 700, "easeInOutQuart")  
       $("#sticky-header").stop(true,false).animate({top:"0px"},700, "easeInOutQuart")  


   } else {

        $("#header").stop(true,false).animate({top:"0px"},1000, "easeInOutQuart")  
        $("#sticky-header").stop(true,false).animate({top:"-100px"},1000, "easeInOutQuart")  

   }
});

示例如下:http://jsfiddle.net/9c56n442/1/

关于javascript - 需要帮助使我的 HTML 和 CSS 代码更有效地实现导航效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26642910/

相关文章:

javascript - 在 Wordpress 帖子中使用 jQuery

javascript - 有没有办法实用地检测IE11企业模式?

javascript - jQuery 滚动函数在每个滚动之前添加 bool 值

javascript - 用于清除 appcache/cookies/localstorage 的小书签?

javascript - 重置 tinymce 工具栏选择

javascript - 框阴影和其他颜色效果不适用于 mozilla firefox

javascript - 带有 html 输入控件的动态生成的表行在回发时消失

jquery - 如果输入字段为空或未正确填写,则将提交按钮恢复为初始状态

javascript - 尝试将 json 对象数组放入 knockout 可观察数组时出现奇怪的行为(包括 jsfiddle)

javascript - react : Call a function from a non-owner parent component