【问题标题】:Fixed div on scroll down & Set absolute when scroll up修复了向下滚动时的 div 并在向上滚动时设置绝对值
【发布时间】:2016-01-23 08:25:14
【问题描述】:

先生,我想在向下滚动窗口时将 div id menu 固定在窗口顶部锁定我的菜单,并且我想在向上滚动时将位置设置为绝对位置我尝试使用此代码。它正确地完成了第一份工作。我可以设置固定在页面顶部的菜单。但是向上滚动时无法设置页面绝对位置是我的代码

  <script type="application/javascript">
    $(window).bind('scroll', function () {
        if (window.scrollY=100){
            document.getElementById("menu").style.position = "fixed";
            document.getElementById("menu").style.top = "0px";
            }
        else if(window.scrollY < 100){
            document.getElementById("menu").style.position = "absolute";
            document.getElementById("menu").style.top = "100px";
            }
    });

    </script>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    你是在赋值而不是比较window.scrollY=100 代码应该是:

    $(window).bind('scroll', function () {
        if (window.scrollY>=100){
            //            ^^-----------use >= here
            document.getElementById("menu").style.position = "fixed";
            document.getElementById("menu").style.top = "0px";
            }
        else if(window.scrollY < 100){
            document.getElementById("menu").style.position = "absolute";
            document.getElementById("menu").style.top = "100px";
            }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多