滚动到页面底部时更改背景位置

Change background position when scrolled to bottom of page

本文关键字:背景 位置 底部 滚动      更新时间:2023-09-26

嗨,当页面一直滚动到底部时,我需要我的背景位置上升 50px。有什么办法可以做到这一点吗?

$(window).scroll(function() {
   //Checking if scrolled to the bottom
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       //Adding background position CSS property to the desired element
       $('.YOUR-ELEMENT-CLASS').css('background-position', '0 50px');
   }
   else {
       $('.YOUR-ELEMENT-CLASS').css('background-position', 'default_values');
   }
});
我不知道

你想要你的背景位置是什么,但说它是 0 100px,你希望它上升到 50px,假设你的元素的 id 为 #wrapper 你会做这样的事情:

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       $('#wrapper').css('background-position', "0 50px");
   }
});

这也是另一种方法...

$(window).scroll(function() {
if (  document.documentElement.clientHeight + 
      $(document).scrollTop() >= document.body.offsetHeight )
{ 
    //add here your CSS code for changing color and do whatever you want
}
 });

我知道这与以前的代码相同,但我也想为 Stack Overflow 贡献一个答案!