【问题标题】:Change background position when scrolled to bottom of page滚动到页面底部时更改背景位置
【发布时间】:2024-01-12 02:25:01
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery css background background-position


    【解决方案1】:
    $(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');
       }
    });
    

    【讨论】:

    • 这不是要把它往下推吗?
    • 你应该将 background-position 设置为 '0 -50px' 以便背景上升
    • @unobf wtf 没关系,OP 将在那里插入他想要的内容:)。主要是检查页面何时滚动到底部。我还能为他做些什么,也许可以针对搜索引擎优化他的应用程序,通过 TeamViewer 连接并在他抽雪茄和啜饮 20 瓶旧威士忌时手动将其更改为他的需求?
    • 注意细节:-P 只是说
    • 大声笑...那你在 * 上做什么?
    【解决方案2】:

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

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

    【讨论】:

    • 啊抱歉,我开始写这篇文章并没有看到你发布你的答案。
    【解决方案3】:

    这也是另一种方法......

    $(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 贡献一个答案!

    【讨论】: