简体   繁体   English

使DIV与页面一起滚动而不使用pos:fixed

[英]Make DIV scroll with page without using pos:fixed

So I have a div on the LHS of my page and I want it to scroll with the page, using position:fixed works fine but once I decrease the width of the browser window, the div hovers above the other elements on the page and gets in the way. 所以我在页面的LHS上有一个div,我希望它使用position:fixed随页面滚动,但是效果很好,但是一旦我减小了浏览器窗口的宽度,div就会悬停在页面上其他元素的上方,并得到顺便说一句 This is obviously a result of the pos:fixed. 这显然是pos:fixed的结果。

Can I make the div scroll with the page without having to use pos:fixed? 我可以在不使用pos:fixed的情况下使div随页面滚动吗? With JS perhaps? 也许用JS? Any advice or suggestions would be appreciated. 任何意见或建议,将不胜感激。

    #scrollerDiv{
        position: fixed;
        right:100px;
        top:238px;
        padding: 10px;
        width:48px;
        height:48px;
    }

You can try this: 您可以尝试以下方法:

 $(function(){ //<-----------------------doc ready
    $(window).on('scroll', function () {
      var scrollPos = $(document).scrollTop();
      $('.scroll').css({
         top: scrollPos
      });
   }).scroll();
});

For this i have made a fiddle for you. 为此,我为您做了一件小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM