繁体   English   中英

滚动到该 div 后如何使 div 切换到底部固定?

[英]How to make div switch to bottom fixed after you scroll to that div?

今天是个好日子。 我试图在上下滚动时将我的 div 切换到底部的固定 div 。

我发现了这个堆栈溢出主题,它完成了我正在尝试做的事情,但在此处的顶部。

代码:

var fixmeTop = $('.fixme').offset().top;       // get initial position of the element

$(window).scroll(function() {                  // assign scroll event listener

    var currentScroll = $(window).scrollTop(); // get current position

    if (currentScroll >= fixmeTop) {           // apply position: fixed if you
        $('.fixme').css({                      // scroll to that element or below it
            position: 'fixed',
            top: '0',
            left: '0'
        });
    } else {                                   // apply position: static
        $('.fixme').css({                      // if you scroll above it
            position: 'static'
        });
    }

});

经过多次尝试和许多文章红色后,我无法将其调整为固定在底部(对 javascript 来说有点新)。 所以我请求你的帮助。

嘿,你可以这样做

const heightOfText = 15
if (currentScroll >= fixmeTop - $(window).height() + heightOfText) {
   $('.fixme').css({
       position: 'fixed',
       bottom: '0',
       left: '0'
   });
}

因此,当您在文本底部看到.fixme时,它会以bottom: '0'附加到底部。 让我知道它是否有帮助。

您可以使用position: sticky

 /*QuickReset*/ *{margin:0;box-sizing:border-box;}.content { min-height: 200vh; border: 4px dashed #000; }.sticky-bottom { position: sticky; bottom: 0; padding: 2em 0; background: #0bf; }
 <div> <div class="content"> 1. Lorem ut florem... </div> </div> <div> <div class="content"> 2. Lorem ut florem... </div> <div class="sticky-bottom">Stick me when in viewport</div> </div>

暂无
暂无

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

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