简体   繁体   English

使用Javascript(jQuery)的深度效果

[英]depth effect using Javascript (jQuery)

Does anyone know (as in http://james.padolsey.com/ ) how the upper body layer is scrolling at a faster speed while the background is scrolled at a slower speed making it give an effect of depth?? 有没有人知道(如在http://james.padolsey.com/中 )上身层如何以更快的速度滚动,而背景以较慢的速度滚动,使其产生深度效果?

Can I implement multiple layers (more then 2) scrolling at different speeds using the similar concept?? 我可以使用类似的概念实现多个层(超过2个)以不同的速度滚动吗?

Below is the bit of code that does this. 下面是执行此操作的代码。 I did not write it, just looked at his source and got it. 我没有写它,只是看着他的来源并得到它。

var a = document.body,
    e = document.documentElement;
$(window).unbind("scroll").scroll(function () {
    a.style.backgroundPosition = "0px " + -(Math.max(e.scrollTop, a.scrollTop) / 8) + "px"
})

Live Demo 现场演示

I didn't find the code in the source, but I recreated it reasonably: 我没有在源代码中找到代码,但我合理地重新创建了代码:

$(window).scroll(function(e){
    $('body').css('background-position', 
                  '0 ' + parseInt($(this).scrollTop()*0.5,10) + 'px');
});

Demo → 演示→

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

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