I am having trouble with a slider I am trying to build. Basically, it auto scrolls through the images just fine, however the next and previous arrows have no effect at all.
I get this error when Next is clicked -
Uncaught ReferenceError: id is not defined gallery.js:48 event.returnValue is deprecated. Please use the standard event.preventDefault() instead. Uncaught ReferenceError: id is not defined
Here is the fiddle link - http://jsfiddle.net/8L5xA/
But here is my Html and JS -
HTML
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
<link rel="stylesheet" href="css/gallery.css">
</head>
<body>
<div class="wrapper">
<div id="slider">
<Img id="1" src="images/slide1.jpg">
<Img id="2" src="images/slide 2.jpg">
<Img id="3" src="images/slide 3.jpg">
</div>
<a href="#" class="left" onclick="prev(); return false;">Previous</a>
<a href="#" class="right" onclick="next(); return false;">Next</a>
</div>
<script src="js/jquery.js"></script>
<script src="js/gallery.js"></script>
</body>
</html>
And this is JS
sliderInt=1;
sliderNext=2;
$(document).ready(function(){
$("#slider>img#1").fadeIn(300);
startSlider();
$(".right").click(function(){
next();
});
$(".left").click(function(){
next();
});
});
var count=0;
function startSlider(){
count=$("#slider>Img").size();
loop=setInterval(function(){
if(sliderNext>count){
sliderNext=1;
sliderInt=1;
}
$("#slider>Img").fadeOut(300);
$("#slider>Img#" + sliderNext).fadeIn(300);
sliderInt=sliderNext;
sliderNext=sliderNext + 1;
},3000);
}
function prev(){
newSlide=sliderInt-1;
showSlide(newSlide);
}
function next(){
newSlide=sliderInt + 1;
showSlide(newSlide);
}
function showSlide(){
if(id>count){
id=1;
}else if(id<1){
id=count;
}
$("#slider>Img").fadeOut(300);
$("#slider>Img#" + id).fadeIn(300);
sliderInt=id;
sliderNext=id + 1;
}
I am only a beginner and any help would be hugely appreciated.
Many thanks
On the line
showSlide(newSlide);
you pass the int newSlide, but on the function definition, you do not catch any arguments.
Change
function showSlide(){
to
function showSlide(id){
Hope this helps
there were lot of logically errors in your code, i have tuned it here
CODE:
sliderInt = 1;
sliderNext = 2;
var count = 0;
var loop;
$(document).ready(function () {
$("#slider>img#1").fadeIn(300);
startSlider();
$(".right").click(function (event) {
//alert("clicked next");
next();
event.preventDefault();
});
$(".left").click(function (event) {
prev();
event.preventDefault();
});
});
function startSlider() {
count = $("#slider>Img").size();
delayCall();
}
function delayCall() {
loop = setInterval(function () {
if (sliderNext > count) {
sliderNext = 1;
sliderInt = 1;
}
$("#slider>Img").fadeOut(300);
$("#slider>Img#" + sliderNext).fadeIn(300);
sliderInt = sliderNext;
sliderNext = sliderNext + 1;
}, 3000);
}
function prev() {
// alert("previous");
clearInterval(loop);
if (sliderInt == 1) newSlide = count;
else newSlide = sliderInt - 1;
showSlide(newSlide);
}
function next() {
// alert(sliderInt+":"+count);
clearInterval(loop);
if (sliderInt == count) sliderInt = 0;
newSlide = sliderInt + 1;
showSlide(newSlide);
}
function showSlide(id) {
// alert(id+":"+count);
/* if(id>count){
id=1;
}else{
id;
}*/
$("#slider>Img").fadeOut(300);
$("#slider>Img#" + id).fadeIn(300).delay(1000);
//Thread.sleep(3000);
sliderInt = id;
sliderNext = id + 1;
delayCall();
}
FIDDLE: http://jsfiddle.net/8L5xA/4/
Your code had nothing to do with Chrome browser, just the logic was wrong that's it. i have verified this code on Firefox 25.0 and on Chrome 30.0.1599.101 and its working smoothly, hope this solves your problem.
Happy Coding :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.