sev*_*ens 9 javascript jquery scroll
使用下面的标记,当我单击或悬停在"#scrollUp"或"#scrollDown"锚标记上时,如何向上或向下滚动"#content"div.滚动应该是平滑的.如果点击它应滚动特定数量(对于触摸设备)如果鼠标悬停它可以滚动直到mouseout.
<style>
#content {
overflow:auto;
height: 50px; /*could be whatever*/
}
</style>
<a id="scrollUp" href="#">up</a>
<a id="scrollDown" href="#">down</a>
<div id="wrapper">
<div id="content">
<ul>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
And*_*ker 35
您可以使用jQuery的animate函数来完成对平滑滚动效果click或者mouseover:
var step = 25;
var scrolling = false;
// Wire up events for the 'scrollUp' link:
$("#scrollUp").bind("click", function(event) {
event.preventDefault();
// Animates the scrollTop property by the specified
// step.
$("#content").animate({
scrollTop: "-=" + step + "px"
});
}).bind("mouseover", function(event) {
scrolling = true;
scrollContent("up");
}).bind("mouseout", function(event) {
// Cancel scrolling continuously:
scrolling = false;
});
$("#scrollDown").bind("click", function(event) {
event.preventDefault();
$("#content").animate({
scrollTop: "+=" + step + "px"
});
}).bind("mouseover", function(event) {
scrolling = true;
scrollContent("down");
}).bind("mouseout", function(event) {
scrolling = false;
});
function scrollContent(direction) {
var amount = (direction === "up" ? "-=1px" : "+=1px");
$("#content").animate({
scrollTop: amount
}, 1, function() {
if (scrolling) {
// If we want to keep scrolling, call the scrollContent function again:
scrollContent(direction);
}
});
}
Run Code Online (Sandbox Code Playgroud)
工作示例:http://jsfiddle.net/andrewwhitaker/s5mgX/
(您必须禁用mouseover和mouseout事件才能正确查看click事件处理程序的效果)
这个怎么运作:
animate上面提到的功能在点击时按指定的量平滑滚动.mouseover事件处理程序被调用时启用连续滚动,并在链接的mouseout事件处理程序时禁用滚动.scrollContent被调用时,如果该scrolling标志仍然是true动画完成后,在同一方向上再次动画.回调函数参数animate允许我们在动画完成后执行操作.| 归档时间: |
|
| 查看次数: |
35776 次 |
| 最近记录: |