I'm making in jquery an image slider. First time I've done in javascript and now I want to convert the code in jquery.
The question is how can I write onclick="slide(-1)"
in jquery?
HTML:
<div id="container">
<img id="img" src="img/img1.jpg">
<div id="left-holder"><img class="left" src="img/arrow-left.png"></div>
<div id="right-holder"><img class="right" src="img/arrow-right.png"></div>
</div>
CSS:
#container {
width: 1024px;
height: 768px;
margin: 20px auto;
position: relative;
}
#img {
width: 1024px;
height: 768px;
position: absolute;
}
#left-holder {
height: 768px;
width: 100px;
position: absolute;
left: 0px;
top: 0px;
}
#right-holder {
height: 768px;
width: 100px;
position: absolute;
right: 0px;
top: 0px;
}
.left {
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 0px;
cursor: pointer;
}
.right {
width: 50px;
height: 50px;
position: absolute;
top: 50%;
right: 0px;
cursor: pointer;
}
Jquery:
var imagecount = 1;
var total = 8;
function slide(x){
var image = $("#img");
imagecount = imagecount + x;
if(imagecount > total) { imagecount = 1;}
if(imagecount < 1) { imagecount = total;}
image.attr("src", "img/img"+ imagecount + ".jpg");
}
Images are called : img1.jpg, img2.jpg, ..
Thank you in advance!