问题
So I have a javascript/jquery image slider. Each slide is a <div> element, children of the div "container". Every five seconds, the last slide slides up and the new image fades in. Simple stuff. Here's my code:
window.onload = function start() {
slide();
}
function slide() {
var num = 0;
window.setInterval(function () {
$("#container div:eq(" + num + ")").slideUp(450);
num = (num + 1) % 4;
$("#container div:eq(" + num + ")").fadeIn(450);
}, 5000);
So, what do I need to do to add buttons that take you to the next slide and last slide?
回答1:
Next:
$("#next").click(function () {
$("#container div:eq(" + num + ")").slideUp(450);
num = (num + 1) % 4;
$("#container div:eq(" + num + ")").fadeIn(450);
});
Last:
$("#last").click(function () {
$("#container div:eq(" + num + ")").slideUp(450);
num = 4;
$("#container div:eq(" + num + ")").fadeIn(450);
});
回答2:
HTML:
<a id="next" href="#">next</a>
<a id="last" href="#">last</a>
JavaScript:
$(document).ready(function () {
var cur = 0;
var slide = function (n) {
$("#container div:eq(" + cur + ")").slideUp(450);
cur = n;
$("#container div:eq(" + cur + ")").fadeIn(450);
)
setInterval(function () { slide((cur + 1) % 4); }, 5000);
$('#next').click(function () { slide((cur + 1) % 4); });
$('#last').click(function () { slide(3); });
});
来源:https://stackoverflow.com/questions/11695405/how-do-i-add-buttons-to-my-javascript-jquery-slider