gpt4 book ai didi

javascript - JQuery轮播轨道循环

转载 作者:搜寻专家 更新时间:2023-10-31 08:54:48 27 4
gpt4 key购买 nike

我无法使 slider 内的图像循环播放。我希望轨道在隐藏后从两侧循环。这里是我现在拥有的 jsfiddle。 http://jsfiddle.net/V2x6s/3/

这是 Javascript

setInterval(function() {
var left1 = parseInt($('#track1').css('left')),
left2 = parseInt($('#track2').css('left')),
left3 = parseInt($('#track3').css('left'));

if ($('#left1').is(":hover")) {
$('#track1').css('left', left1+2);
}
else if ($('#left2').is(":hover")) {
$('#track2').css('left', left2+2);
}
else if ($('#left3').is(":hover")) {
$('#track3').css('left', left3+2);
}
else if ($('#right1').is(":hover")) {
$('#track1').css('left', left1-2);
}
else if ($('#right2').is(":hover")) {
$('#track2').css('left', left2-2);
}
else if ($('#right3').is(":hover")) {
$('#track3').css('left', left3-2);
}

}, 10);
.slider {
position: relative;

background-color: #ccc;
height: 150px;
margin: 10px;
padding: 10px;

overflow: hidden;
}
.track {
position: absolute;
top: 10px;
left: 10px;

margin: 0;
padding: 0;
border: 0;

width: 2000px;
}
.book {
float: left;

margin: 0;
margin-right: 10px;
padding: 0;
border: 0;

width: 150px;
height: 150px;

-webkit-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
-ms-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
-webkit-transition: opacity 0.2s;
}
.book:hover {
opacity: 0.5;
}
.book:nth-child(1) {
background-color: #ff0000;
}
.book:nth-child(2) {
background-color: #ffaa00;
}
.book:nth-child(3) {
background-color: #ffff00;
}
.book:nth-child(4) {
background-color: #00ff00;
}
.book:nth-child(5) {
background-color: #0000ff;
}
.book:nth-child(6) {
background-color: #aa00ff;
}
.book:nth-child(7) {
background-color: #ff00ff;
}
.left, .right {
position: absolute;
color: white;
font-size: 48px;
top: 57px;

cursor: pointer;
z-index: 1;
}
.left {
left: 0;
}
.right {
right: 0;
}
<div class="slider">
<div id="left1" class="left">&lt;</div>
<div id="right1" class="right">&gt;</div>
<div class="track" id="track1">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>
<div class="slider">
<div id="left2" class="left">&lt;</div>
<div id="right2" class="right">&gt;</div>
<div class="track" id="track2">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>
<div class="slider">
<div id="left3" class="left">&lt;</div>
<div id="right3" class="right">&gt;</div>
<div class="track" id="track3">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>

最佳答案

fiddle demo

这就是我要做的……我会计算窗口宽度和元素宽度,并使用它们……

// $('#track1').parent().width() - gives window width
// $('#track1 .book').length * ($('#track1 .book').width() + 5) - gives combined elements width
if ($('#left1').is(":hover")) {
var move = left1+20;
if(move > $('#track1').parent().width())
move = $('#track1 .book').length * ($('#track1 .book').width() + 5) * -1;
$('#track1').css('left', move);
//console.log(left1+20, tmp,$('#track1').parent().width() );
}
else if ($('#right1').is(":hover")) {
var move = left1-20;
if(move < $('#track1 .book').length * ($('#track1 .book').width() + 5) * -1 )
move = $('#track1').parent().width();
$('#track1').css('left', move);
//console.log(left1-20, tmp,$('#track1').parent().width() );
}


你可以类似地修改其他的,如果你问我,也使用 setInterval 来监视是一个坏主意。

编辑:

我会这样做:demo

$('.left').hover(function(){
moveLeft($(this));
});

$('.right').hover(function(){
moveRight($(this));
});
function moveLeft(ele){
if(ele.is(":hover")){
var track = ele.next().next(),
move = parseInt(track.css('left'))+10,
books = track.find('.book'),
wid = (books.length * (books.width()+5)) * -1;
if(move > track.parent().width()) move = wid;
track.css('left', move);
setTimeout(moveLeft.bind(null, ele), 50); // change 20 to whatever number you like
}
}
function moveRight(ele){
if(ele.is(":hover")){
var track = ele.next(),
move = parseInt(track.css('left'))-10,
books = track.find('.book'),
wid = (books.length * (books.width()+5)) * -1;
if(move < wid ) move = track.parent().width();
track.css('left', move);
setTimeout(moveRight.bind(null, ele), 50); // change 20 to whatever number you like
}
}

关于javascript - JQuery轮播轨道循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28846187/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com