我也不完全确定你的意思(所以如果他的答案是你想要的,我向你和 @mrtsherman 道歉),但我想你是在问如何获得视差滚动效果。您链接到的网站使用 MooTools
$$("#floaters-0, #floaters-01, #floaters-02, #floaters-1, #floaters-11, #floaters-12, #floaters-2, #floaters-21, #floaters-22, #floaters-3, #floaters-31, #floaters-32").each(function(item) {
item.store('top', parseInt(item.getStyle('top')));
item.store('y', item.getPosition().y);
item.store('friction', parseFloat(item.get("data-friction")));
});
this.addEvent('scroll', verticalParallax);
function verticalParallax(e) {
var windowScrollY = window.getScroll().y;
$$("#floaters-0, #floaters-01, #floaters-02, #floaters-1, #floaters-11, #floaters-12, #floaters-2, #floaters-21, #floaters-22, #floaters-3, #floaters-31, #floaters-32").each(function(item) {
if ((windowScrollY) >= item.getPosition().y)
item.setStyle("top", item.retrieve('top') + (windowScrollY - item.retrieve('y')) * item.retrieve('friction'));
});
};
jQuery
我已将其移至 jQuery(以一种粗略且现成的方式)并将其卡在 JSFiddle 中 http://jsfiddle.net/meloncholy/FhxZ3/1/
$(function ()
{
$(".float").each(function ()
{
var $this = $(this);
$this.data("startTop", $this.offset().top);
});
$(window).scroll(function ()
{
var documentScrollTop = $(document).scrollTop();
$(".float").each(function ()
{
var $this = $(this);
$this.offset({ top: $this.data("startTop") + documentScrollTop * $this.data("friction"), left: $this.offset().left });
});
});
});
CSS
.float { display: block; left: 100px; position: fixed; }
#float-1 { top: 50px; }
#float-2 { top: 400px; }
#float-3 { top: 850px; }
.content { font-size: 48px; position: relative; z-index: 2; }
代码挂钩scroll 事件并在触发时调整图像的位置。滚动量由摩擦属性控制,因此将其设置为0.5(如示例中所示)意味着它们应该以文本速度的一半移动。事实证明(尽管我想回想起来很明显)将视差元素设置为具有position: fixed 很重要,否则你会得到一些令人讨厌的跳跃。
正如我所说,这有点粗糙和准备好了(特别是其他网站通常只在某个垂直窗口中运行视差效果),所以你可能想点击谷歌parallax scroll jquery或其他东西,看看别人是如何正确地做到这一点的。 :)