0
私は水平な画像カルーセルを開発しています。マウスを動かすと、マウスを動かすと停止し続けます。 JSコードは、マウスがどこにあるかを検出し、それに応じて左または右にスクロールするために、ホバーイベントを使用します。私はそれを働かせることはできません、しかし、どんな助けも高く評価されるでしょう。事前に水平divを連続的にスクロールするにはホバー
おかげ
JS:
$('.carousel-frame ul').on('hover', function(e) {
var container = $(this).parent();
if ((e.pageX - this.offsetLeft) < container.width()/2) {
var direction = function() {
container.stop().animate({
scrollLeft: '-=600'
}, 1000, 'linear', direction);
}
container.stop().animate({
scrollLeft: '-=600'
}, 1000, 'linear', direction);
} else {
var direction = function() {
container.stop().animate({
scrollLeft: '+=600'
}, 1000, 'linear', direction);
}
container.stop().animate({
scrollLeft: '+=600'
}, 1000, 'linear', direction);
}
}, function() {
var container = $(this).parent();
container.stop();
});
CSS:
.carousel-frame {
width: 100%;
margin-bottom: 0.5em;
padding-bottom: 1em;
position: relative;
overflow-x: scroll;
white-space: nowrap;
}
.carousel-frame ul {
margin: 0;
padding: 0;
height: 100%;
list-style: none;
}
.carousel-frame li.carousel-item {
cursor: pointer;
display: inline-block;
margin: 0 5px 0 0;
padding: 0;
}
HTML:
<div class="carousel-frame">
<ul>
<li class="carousel-item">
<img src="http://placehold.it/200x150" />
</li>
<li class="carousel-item">
<img src="http://placehold.it/200x150" />
</li>
<li class="carousel-item">
<img src="http://placehold.it/200x150" />
</li>
<li class="carousel-item">
<img src="http://placehold.it/200x150" />
</li>
<li class="carousel-item">
<img src="http://placehold.it/200x150" />
</li>
</ul>
</div>
FIDDLE:
https://jsfiddle.net/btLoenzf/1/