而不是命名您的操作按钮
.linkSlide1
.linkSlide2
.linkSlide3
.etc...
你可以做
.linkslide
并确保按钮的父级内没有其他元素,这样您的.linkslide
元素将按index
from 0
to组织19
(对于 20 个按钮)。
比去抓住.index()
点击的按钮并将其发送到.superSlider( THAT INDEX!! )
.
要为滑块设置动画,您可以将检索到的索引乘以 one-slide-width:
LIVE DEMO
jQuery:
$('.linkSlide, .back').click(function() {
var ind = $(this).index()+1; // +1 cause we are already at 0
if($(this).hasClass('back')) ind = 0; // go to slide index 0
superSlider( ind );
});
function superSlider( ind ){
var oneSlideWidth = $('.slide').width();
$('#allSlides').stop().animate({left: -(ind*oneSlideWidth) },700);
}
HTML:
<div id="slider">
<div id="allSlides">
<div class="slide"> <!-- index = 0 -->
Slide 0
<button class="linkSlide">Tile index 1</button> <!-- index = 0, add 1-->
<button class="linkSlide">Tile index 2</button> <!-- index = 1, add 1 -->
<button class="linkSlide">Tile index 3</button> <!-- index = 2, add 1 -->
</div>
<div class="slide"> <!-- index = 1 -->
Slide 1
<button class="back">Back to 0</button>
</div>
<div class="slide"> <!-- index = 2 -->
Slide 2
<button class="back">Back to 0</button>
</div>
<div class="slide"> <!-- index = 3 -->
Slide 3
<button class="back">Back to 0</button>
</div>
</div>
</div>
CSS:
#slider{
position:relative;
width:100px;
height:95px;
background:#eee;
text-align:center;
overflow:hidden;
}
#allSlides{
position:absolute;
left:0;
width:9999px;
height:95px;
}
.slide{
position:relative;
width:100px;
height:95px;
float:left;
}
探索我使用的 jQuery 方法:
http://api.jquery.com/click/
http://api.jquery.com/index/
http://api.jquery.com/hasclass/
http://api.jquery.com/width/
http: //api.jquery.com/stop/
http://api.jquery.com/animate/