问题
I have this very simple slideshow here: http://jsfiddle.net/Jtec5/2/
Here's the codes:
HTML:
<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
<img src="http://gillespaquette.ca/images/stack-icon.png">
</div>
</div>
<ul></ul>
CSS:
#slideshow {
margin: 50px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
#slideshow > div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}
#slideshow img {
max-width:240px;
max-height:240px;
}
ul {
list-style:none;
margin:0px;
padding:0px;
}
ul li {
float:left;
border-radius:10px;
width:10px;
height:10px;
border:1px solid white;
background:grey;
}
ul li.active {
background:black;
}
Jquery:
$("#slideshow > div:gt(0)").hide();
var index = 1;
var maxindex = $('#slideshow > div').length;
setInterval(function () {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
$('ul li').removeClass('active');
$('ul li:eq(' + index + ')').addClass('active');
index = index < maxindex - 1 ? index + 1 : 0;
}, 3000);
for (var i = 0; i < maxindex; i++) {
$('ul').append('<li class="' + (i == 0 ? 'active' : '') + '"></li>');
}
You can see in the slideshow there's buttons that tells me in which photo I am, I'm trying to relate every button with its photo so when I click it it takes me to its photo, if changing the whole code of the button is needed it will be better because I didn't like my code, I think it's too long.
回答1:
I've also made a fiddle which hopefully does what you want. http://jsfiddle.net/L7s4e/ The main code is as follows:
$("#slideshow > div:gt(0)").hide();
index = 0;
maxIndex = $('#slideshow > div').length;
$mainDiv = $('#slideshow div');
for (var i = 0; i < maxIndex; i++) {
var $li = $("<li/>", {
"data-index": i,
"class": (i == 0 ? 'active' : '')
}).on("click",
function () {
/* uncomment below if you want slideshow to stop on selection
to restart call startSlideshow();*/
//clearInterval(intervalId);
var imageIndex = $(this).data("index");
$mainDiv.eq(index).fadeOut(1000);
$mainDiv.eq(imageIndex).fadeIn(1000);
index = imageIndex;
$('ul li').removeClass('active');
$('ul li:eq(' + index + ')').addClass('active');
});
$('ul').append($li);
}
function startSlideshow() {
intervalId = setInterval(function () {
$('#slideshow > div').eq(index).fadeOut(1000);
index = index < maxIndex - 1 ? index + 1 : 0;
$('#slideshow > div').eq(index).fadeIn(1000);
$('ul li').removeClass('active');
$('ul li:eq(' + index + ')').addClass('active');
}, 3000);
}
startSlideshow();
Hope it works as you wanted. R.
回答2:
see following :
var $mainDiv = $('#slideshow div');
for (var i = 0; i < maxindex; i++) {
var $li = $("<li/>",{"data-index":i, "class":(i == 0 ? 'active' : '')})
.on("click",function(){
var imageIndex = $(this).data("index");
$mainDiv.eq(imageIndex).fadeIn(1000).prev().fadeOut(1000);
});
$('ul').append($li);
}
working fiddle here: http://jsfiddle.net/Jtec5/36/
i hope it helps
来源:https://stackoverflow.com/questions/18275327/adding-slideshow-buttons-jquery