这个网站的滚动功能是如何实现的
How is the scrolling functionality done in this website?
我对这个网站让扬声器以一定的间隔滚动的方式很感兴趣。
我不确定这是否是一个jQuery插件,但我很想知道/了解这个功能是如何实现的。
创建一个容器元素,该元素设置为要显示的维度。然后将其overflow
属性设置为hidden
,并给它一个更高的孩子。然后使用setInterval
设置从子对象到父对象的偏移动画:
HTML--
<div id="container">
<div id="child"></div>
</div>
CSS--
#container {
position : relative;
width : 500px;
height : 300px;
overflow : hidden;
}
#child {
position : absolute;
top : 0;
left : 0;
width : 100%;
height : 900px;
}
JS--
$(function () {
var $child = $('#child'),
timer = setInterval(function () {
$child.animate({ top : '-=300' }, 500);
}, 1500);
});
更新
然后,您可以检测#child
元素的整个高度显示后,是否应将其动画设置回起始位置:
$(function () {
var $child = $('#child'),
height = $child.height(),
interval = 300,
current = 0,
timer = setInterval(function () {
current++;
if ((current * interval) >= height) {
current = 0;
$child.stop().animate({ top : 0 }, 1000);
} else {
$child.stop().animate({ top : (current * interval * -1) }, 500);
}
}, 1500);
});
下面是一个演示:http://jsfiddle.net/BH5gK/2/
相关文章:
- 如何使用动画实现纸张推车
- 客户端服务器REST API captcha实现
- 如何实现此布局
- Meteor忘记了密码的实现
- 使用Native Sockets在Android中实现WebSockets
- 在样板文件中实现Ajax
- instanceof是如何在JavaScript中实现的
- 如何正确实现Jquery多选小部件
- 实现一个建立在google.com之上的自定义搜索引擎
- 多个组件是如何实现的
- window.location使用jquery mobile实现chrome跳转
- 如何在OpenERP中实现网络摄像头
- Node.js使用Series函数(模式?)实现流控制时出现意外结果
- javascript加密实现,包括可信否认
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 如何让程序员在javascript中实现正确的回调
- 如何使用自定义辅助对象(handler)实现嵌套的每个循环
- AngularJS智能表全局配置实现
- Expressjs/AngularJS:实现req-flash后出错
- Java' Rhino实现实现了哪个JavaScript (ECMAScript)版本(更新策略是什么?)