【发布时间】:2025-12-28 00:15:10
【问题描述】:
我正在尝试创建一个与此类似的水平滚动条。 http://www.k2.pl/
我在这里做了一个原型。 http://jsfiddle.net/Uu3aP/1
我查看了轮播插件,但没有一个功能符合我的要求。
我只是好奇我是如何根据左右 x 位置执行速度计算的,并且可能对两侧使用缓动方法。
这是我目前所在的位置,我们将不胜感激。
HTML
<div id="scroll">
<div id="scrollContainer">
<ul>
<li><img src="http://placehold.it/350x350" alt="" /></li>
<li><img src="http://placehold.it/350x350" alt="" /></li>
<li><img src="http://placehold.it/350x350" alt="" /></li>
<li><img src="http://placehold.it/350x350" alt="" /></li>
<li><img src="http://placehold.it/350x350" alt="" /></li>
</ul>
</div>
CSS
body {
margin: 0;
}
#scroll {
width: 100%;
overflow: hidden;
}
#scrollContainer * {
float: left;
list-style: none;
margin: 0;
padding: 0;
}
JAVASCRIPT
$(document).ready(function() {
var $totalwidth = 0;
var $paneTarget = $('#scroll');
var $paneContainer = $('#scrollContainer');
var $this = $(this);
var $w = $this.width();
$paneTarget.find('li').each(function() {
$totalwidth += $this.width();
$paneContainer.width($totalwidth);
});
$paneTarget.on('mousemove', function(e) {
if ((e.pageX) < $w / 2) {
$paneTarget.stop().scrollTo( {top:'0', left:'-=100'}, 200, {easing: 'linear'});
} else if ((e.pageX) > ($w / 2)) {
$paneTarget.stop().scrollTo( {top:'0', left:'+=100'}, 200, {easing: 'linear'});
}
else {
$paneTarget.stop();
}
});
});
【问题讨论】:
标签: javascript jquery scroll slider scrollto