滚动时已修复jQuery元素-定位问题

时间:2018-08-17 15:30:37

标签: jquery scroll

我在滚动方面具有这种滚动效果,但是定位没有正确重置。如果向下滚动,则元素滚动太远。如果向上滚动,它也会向上滚动,但停在的位置大约有100px或更多。

如果您知道我做错了什么或需要做些什么来纠正此问题,我将不胜感激!在此先感谢朋友:)

以下是您可以在其中看到此操作的URL: https://blakes-seeds-test-site.myshopify.com/collections/frontpage/products/chocolate-chip-snack-bar

.product-box-wrap {
    position: fixed;
    top: 0;
    z-index: 9999;
}
.product-box-wrap.o-layout__item {
    width: 25% !important;
    margin-top: 250px;
}

$(document).ready(function () {
    var el = $('.product-box-wrap');
    var originalelpos = el.offset().top; // take it where it originally is on the page

    //run on scroll
    $(window).scroll(function () {
        var el = $('.product-box-wrap'); // important! (local)
        var elpos = el.offset().top; // take current situation
        var windowpos = $(window).scrollTop();
        var finaldestination = windowpos + originalelpos;
        el.stop().animate({ 'top': finaldestination }, 2000);
    });
});

0 个答案:

没有答案
相关问题