繁体   English   中英

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

[英]Change background position when scrolled to bottom of page

嗨,当页面一直滚动到底部时,我需要背景位置上升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做出贡献!

暂无
暂无

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

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