【发布时间】:2016-08-25 04:44:03
【问题描述】:
我将其位置设置为固定在页面顶部。这将在向下滚动到文档或页面的中间时跟随我。在文件中间之后,我想把它贴在文件中间,没有它就向下滑动。再次向上滚动后,我希望它回来跟随我(固定位置)。
这是我正在处理的一段代码。
if ($('body').hasClass('catalog-product-view')) {
if ($(this).scrollTop() >= $('.product-collateral-wrapper').innerHeight()/2)
{
my div should remain in its place now. waiting for me to scoll up again to follow me
}else{
have my div fixed positioned so when I scroll the page, I always see it.
}
Here is the full function
$(window).scroll(function() {
if ($(this).scrollTop() > offset && !stickyActive) {
stickyActive = true;
$header.addClass("sticky-active");
$(stickyPlaceholder).show();
var logo = $('<li></li>').append($('.main-header .logo-container > a').clone()).html();
logo = '<li class="logo-fixed">'+logo+'</li>';
$('.main-header nav.top-bar.main-nav .top-bar-section .left').prepend(logo);
if ($('.top-bar-section .main-logo-sticky').length) {
$('.top-bar-section .main-logo').hide();
}
topbarLeftHeight = $('.main-header nav.top-bar.main-nav .top-bar-section .left').outerHeight();
} else if ($(this).scrollTop() < offset && stickyActive) {
$header.removeClass("sticky-active");
$('nav.top-bar.main-nav .logo-fixed').remove();
$(stickyPlaceholder).hide();
stickyActive = false;
}
if ($('body').hasClass('catalog-product-view') && Modernizr.mq('only screen and (min-width: 64.063em)')) {
setTimeout(function(){
$('.magellan-nav.magellan-fixed').css('margin-top',topbarLeftHeight+'px');
},100);
}
//HERE GOES CHANGES I AM DOING. It started to make sense how to get it working, but will this be effecient or will it have performance drawback cause of scroll event is triggered with every scroll, and same calculations will be redone again and again?
if ($('body').hasClass('catalog-product-view')) {
if ($(this).scrollTop() >= $('.product-collateral-wrapper').innerHeight()/2)
{
//will enter code here`l toggle position and top property here based on some calculations
}else{
//will toggle position and top property here based on some calculations
}
}
});
$(window).scroll();
};
我尝试将位置从固定更改为相对。这将回到页面顶部,使其在视口中不可见。
【问题讨论】:
-
我没有看到问题
-
您希望图像出现这种情况吗?
标签: javascript css sticky