简体   繁体   English

如何在滚动上左右移动div?

[英]How to move left to right a div on scroll?

I would like to animate in js a simple div on scroll as you can find here grooveshark . 我想在js中滚动一个简单的div动画,因为您可以在此处找到grooveshark

As you can see, the animation reacts to scroll up/down and move horizontally. 如您所见,动画会向上/向下滚动并水平移动。 This is typically what I need. 这通常是我所需要的。 How can I easily determine this feature in js? 如何在js中轻松确定此功能? Thanks 谢谢

It's called parallax . 这就是所谓的视差

You can achieve this yourself using a combination of CSS attribute changes and scroll detection events, or just use a plugin like SkrollR http://prinzhorn.github.io/skrollr/ 您可以结合使用CSS属性更改和滚动检测事件来实现此目的,也可以只使用SkrollR这样的插件http://prinzhorn.github.io/skrollr/

For this particular example, you can see the inline styles of that div being changed on scroll (in developer tools): 对于此特定示例,您可以看到该div的内联样式在滚动状态下(在开发人员工具中)被更改:

<div class="albums-holder" style="background-position: 1174px 50%;">...

You can do this on the scroll event: 您可以在滚动事件上执行此操作:

$(window).scroll(function() {
  $(".albums-holder").css( "background-position", [VALUE]);
});

Where [VALUE] would be computed by incrementing/decrementing the background-position by a value each time. 其中[VALUE]将通过每次将背景位置增加/减少一个值来计算。

EDIT: I failed to mention, my answer is based on the assumption you have jQuery on the page, I would recommend it is for tasks like this anyway. 编辑:我没有提及,我的答案是基于您在页面上有jQuery的假设,我还是建议它用于此类任务。

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

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