我有这个网站:
在右侧边栏中,您会注意到最后一个元素在滚动时运行。
问题是不会停止......走到最底层。
我尝试过这个功能来完成任务
CODE JS:
var aux=0;
$("#text-3").addClass("follow-scroll");
var _footerH=$("#footer-container").outerHeight();
var _docH=$(document).height();
var _scrollP= $(window).scrollTop();
console.log("pozitie scroll",_scrollP);
console.log("inaltime footer",_footerH);
console.log("inaltime document HTML",_docH);
var element = $('.follow-scroll'),
originalY = element.offset().top;
var topMargin = 20;
element.css('position', 'relative');
$(window).on('scroll', function(event) {
var scrollTop = $(window).scrollTop();
console.log(scrollTop);
console.log("originalY:",originalY);
console.log("Scrolltop:",scrollTop);
console.log("topMargin:",topMargin);
var y = scrollTop - originalY + topMargin;
if(scrollTop < originalY) {
console.log("nu s-a declansat evenimentul");
}else {
//aici trebe adaugat top la div
aux=scrollTop - originalY + topMargin
$( '.follow-scroll' ).css( 'top', aux );
if(scrollTop>6000)
{
var _div = $( ".follow-scroll" );
var position = _div.position();
var top2=aux;
//aici trebuie sa primeasca ultimul top
$( '.follow-scroll' ).css( 'top', aux );
}
}
// top=aux;
// console.log("top_nou:",aux);
});
你可以帮我解决这个问题吗?
我怎样才能在某个时候停止这个元素?
提前致谢!
答案 0 :(得分:1)
尝试将代码更改为此
var aux = 0;
$("#text-3").addClass("follow-scroll");
var _footerH = $("#footer-container").outerHeight();
var _docH = $(document).height();
var _scrollP = $(window).scrollTop();
console.log("pozitie scroll", _scrollP);
console.log("inaltime footer", _footerH);
console.log("inaltime document HTML", _docH);
var element = $('.follow-scroll'),
originalY = element.offset().top;
var topMargin = 20;
element.css('position', 'relative');
$(window).on('scroll', function (event) {
var scrollTop = $(window).scrollTop();
console.log(scrollTop);
console.log("originalY:", originalY);
console.log("Scrolltop:", scrollTop);
console.log("topMargin:", topMargin);
var y = scrollTop - originalY + topMargin;
if (scrollTop < originalY) {
console.log("nu s-a declansat evenimentul");
} else {
//aici trebe adaugat top la div
aux = scrollTop - originalY + topMargin
if (scrollTop > 6000) {
var _div = $(".follow-scroll");
var position = _div.position();
var top2 = aux;
//aici trebuie sa primeasca ultimul top
}else{
$('.follow-scroll').css('top', aux);
}
}
});
您会注意到,您应该只设置$('.follow-scroll').css('top', aux);
scrollTop <= 6000
,否则请将其留在原来的位置