Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过滚动jquery更改背景位置_Javascript_Jquery_Background Position - Fatal编程技术网

Javascript 通过滚动jquery更改背景位置

Javascript 通过滚动jquery更改背景位置,javascript,jquery,background-position,Javascript,Jquery,Background Position,尝试使用滚动位置更改正文背景位置。 背景图像具有以下设置: background: #fff url(images/bg.png) center 100% no-repeat; background-attachment:fixed; 我在那页有页脚,高度为120px。 我试图改变身体背景位置(y轴)时,滚动达到页脚与鼠标滚轮或滚动条 编辑: 我已经设法使它既适用于鼠标滚轮,也适用于酒吧,以防有人需要在“固定”背景之外制作一个页脚 $(window).scroll(function(

尝试使用滚动位置更改正文背景位置。 背景图像具有以下设置:

 background: #fff url(images/bg.png) center 100% no-repeat; background-attachment:fixed;    
我在那页有页脚,高度为120px。 我试图改变身体背景位置(y轴)时,滚动达到页脚与鼠标滚轮或滚动条

编辑

我已经设法使它既适用于鼠标滚轮,也适用于酒吧,以防有人需要在“固定”背景之外制作一个页脚

$(window).scroll(function() {
        var def_bg_pos = 300;
        var footer = $(document).height() - 120; // footer height

        if($(window).scrollTop() + $(window).height() > footer) {
            var counter = ($(window).scrollTop() + $(window).height()) - footer;
            $('body').css('background-position', 'center '+ ((counter * -1) - def_bg_pos) + 'px');
        }
        else
            $('body').css('background-position', 'center -'+def_bg_pos+'px');
    });

<body style="background: #fff url(images/bg.png) center -300px no-repeat; background-attachment:fixed;">
$(窗口)。滚动(函数(){
var def_bg_pos=300;
var footer=$(document).height()-120;//页脚高度
如果($(窗口).scrollTop()+$(窗口).height()>页脚){
变量计数器=($(窗口).scrollTop()+$(窗口).height())-页脚;
$('body').css('background-position','center'+((counter*-1)-def_bg_pos)+'px');
}
其他的
$('body').css('background-position','center-'+def_-bg_-pos+'px');
});

这可以通过jQuery完成。你编码了什么吗?你有它作为背景附件:固定。可能需要将此更改为scroll?grainne-后台附件:scroll,没有按照我的要求正常工作。解决方案将发布在帖子中。谢谢