我无法在滑块循环中制作图像。隐藏后,我希望跟踪从两侧环绕。这里是 我现在拥有的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"><</div>
<div id="right1" class="right">></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"><</div>
<div id="right2" class="right">></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"><</div>
<div id="right3" class="right">></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>
&#13;
答案 0 :(得分:1)
小提琴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
监视moniter是一个坏主意。
我会这样做: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
}
}