简体   繁体   English

jQuery页脚div使用主体滚动值的视差滚动效果

[英]JQuery footer div Parallax scrolling effect using body scroll value

I tought it would be simple to do a minor parallax effect on a footer 'div' background, using the 'body' scrollTop() to change the background-position 我坚信,使用“ body” scrollTop()更改背景位置,对页脚“ div”背景进行轻微的视差效果很简单

This is my trial, but the background of the footer 'div' doesn't move. 这是我的试用,但是页脚“ div”的背景没有变化。 I don't see what I'm doing wrong here: 我在这里看不到我在做什么错:

HTML is like HTML就像

<body>
<div class="footer_parallax"></div>
</body>

JS is like JS就像

$('body').scroll(function(){
var x = $(this).scrollTop();
$('.footer_parallax').css('background-position','0% '+parseInt(-x/10)+'px');
});

codepen here: http://codepen.io/ssstofff/pen/zxXyMp 此处的codepen: http ://codepen.io/ssstofff/pen/zxXyMp

Since codepen uses iframes I think theres some problem with fetching scroll element via body tag. 由于Codepen使用iframe,因此我认为通过body标签获取滚动元素存在一些问题。 When I changed it to window it worked. 当我将其更改为window它可以工作。

Here is full snippet (CSS method notation changed): 这是完整的代码段(CSS方法符号已更改):

$(window).scroll(function(){

  var x = $(this).scrollTop();

  $('.footer_parallax').css({'background-position': '0% '+parseInt(-x/10)+'px'});

});

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

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