Javascript 如何为不确定的页面高度制作视差背景?

Javascript 如何为不确定的页面高度制作视差背景?,javascript,jquery,css,html,Javascript,Jquery,Css,Html,我有一个网站,使用视差背景,背景是1200px长 问题是: 我有一个高度不确定的页面,页面会动态扩展到其内容。因此,如果用户按read more键,它将在不刷新的情况下展开,这会破坏视差效果,因为背景在页面完成之前就已到达终点 背景是复杂的设计图像,它不能重复和添加背景色来掩盖空白无法做到,但我希望我能保持凉爽的视差效果 我的问题: 当视差到达特定y位置时,是否可能使视差停止,并在滚动超过特定y位置时冻结背景?但也能够触发视差效应时,滚动回到特定的y-位置及以上 如果我们假设背景以0.1的速度移

我有一个网站,使用视差背景,背景是
1200px

问题是:
我有一个高度不确定的页面,页面会动态扩展到其内容。因此,如果用户按read more键,它将在不刷新的情况下展开,这会破坏视差效果,因为背景在页面完成之前就已到达终点

背景是复杂的设计图像,它不能重复和添加背景色来掩盖空白无法做到,但我希望我能保持凉爽的视差效果

我的问题:
当视差到达特定y位置时,是否可能使视差停止,并在滚动超过特定y位置时冻结背景?但也能够触发视差效应时,滚动回到特定的y-位置及以上

如果我们假设背景以0.1的速度移动,那么最大高度将为
1200/0.1=12000px

如果页面达到
y-position=12000px
->停止视差效果并按原样冻结图像->如果页面返回到
1199px
再次启动视差

如何在Javascript中实现这一点?如果可能的话,在CSS中也会很好

编辑:

$(window).stellar({responsive:false});
    $( window ).scroll( function(){
      var ypos = $( window ).scrollTop(); //pixels the site is scrolled down
      var visible = $( window ).height(); //visible pixels
      const img_height = 1261; //image height
      var max_scroll = img_height - visible; //number of pixels of the image not visible at bottom
    //change position of background-image as long as there is something not visible at the bottom  
    if ( max_scroll > ypos) {
         $('body').css('background-position', "center -" + ypos + "px");
      } else {
        $('body').css('background-position', "center -" + max_scroll + "px");
      }
    });
以下是我在Stackoverflow上发布之前所做的:

我使用Stellar.js实现视差效果,只是添加了以下javascript:

$(window).stellar({responsive:false});
    $( window ).scroll( function(){
      var ypos = $( window ).scrollTop(); //pixels the site is scrolled down
      var visible = $( window ).height(); //visible pixels
      const img_height = 1261; //image height
      var max_scroll = img_height - visible; //number of pixels of the image not visible at bottom
    //change position of background-image as long as there is something not visible at the bottom  
    if ( max_scroll > ypos) {
         $('body').css('background-position', "center -" + ypos + "px");
      } else {
        $('body').css('background-position', "center -" + max_scroll + "px");
      }
    });
并将以下代码添加到标记(其中包含背景图像):


在这种情况下,它确实会滚动到背景的末尾,但我不知道如何降低背景的滚动速度。

您能为我们提供一个工作示例来说明您的问题吗?是的,可以这样做,但为了进一步帮助您,我们需要知道更多关于你如何实现视差效果的细节。谢谢你的回复,请检查我的编辑,我的朋友们,对我的情况有任何建议。。您的指导原则将非常受欢迎:)在浏览器上运行的应用程序是否支持CSS
calc