(Apologies for the lengthy post. I like to provide detailed explanations.)
Just starting out with Jquery, trying my hand at creating a custom image slider from scratch. Got the slider working with fade effects using this code:
Javascript:
$(document).ready(
$(
$(function(){
$('.moonslide img:gt(0)').hide();
setInterval(function(){$('.moonslide img:first').fadeOut(3000).next('img').fadeIn(3000).end().appendTo('.moonslide');}, 6000);
})
));
HTML
<div id="viewport">
<div id="topslider">
<div class="moonslide">
<img src="/images/Slider/filer.jpg" class="slimage" id="ms0">
<img src="/images/Slider/greenroom.jpg" class="slimage" id="ms1">
<img src="/images/Slider/ng1.png" class="slimage" id="ms2">
<img src="/images/Slider/ngs.png" class="slimage" id="ms3">
<img src="/images/Slider/worldworks.jpg" class="slimage" id="ms4">
</div>
</div>
<div id="linkul" class="moonslidelinks">
<a href="#" id="ml0">Slide 1</a><br />
<a href="#" id="ml1">Slide 2</a><br />
<a href="#" id="ml2">Slide 3</a><br />
<a href="#" id="ml3">Slide 4</a> <br />
<a href="#" id="ml4">Slide 5</a><br />
</div>
</div>
Next step was animating the navigation list along with it. Started by attempting to fade link color from blue to white when corresponding image shows (using Jquery color plugin for CSS animations). Tried breaking up the chain of effects but ran into issues getting the next anchor object after "a:first". The following code worked for the first and second images and the first anchor but not for the second anchor.
$(document).ready($(
function(){
$('.moonslide img:gt(0)').hide();
$('.moonslidelinks a:first').css("color","#FFF");
$('.moonslide img:first').fadeOut(3000).next('img').fadeIn(3000);
$('.moonslidelinks a:first').animate({color: "#0083ff"}, 3000).next('a').animate({color: "#FFF"}, 3000);
}
));
When it didn't work, I tested if I could reach the next Anchor object:
$(document).ready($(
function(){
var nextImg = $('.moonslide img:first').next('img');
var nextA = $('.moonslidelinks a:first').next('a');
console.log(nextImg.attr('id'));
console.log(nextA.attr('id'));
}
));
Output showed ID for second image but undefined for the anchor.
[00:02:11.080] "ms1"
[00:02:11.080] undefined
Adding a list to the navigation and trying to get the next "li" ID worked fine.
$(document).ready($(
function(){
var nextImg = $('.moonslide img:first').next('img');
var nextA = $('.moonslidelinks li:first').next('li');
console.log(nextImg.attr('id'));
console.log(nextA.attr('id'));
}
));
<ul class="moonslidelinks">
<li id="li0"><a href="#" id="0">Slide 0</a></li>
<li id="li1"><a href="#" id="1">Slide 1</a></li>
<li id="li2"><a href="#" id="2">Slide 2</a></li>
<li id="li3"><a href="#" id="3">Slide 3</a></li>
<li id="li4"><a href="#" id="4">Slide 4</a></li>
</ul>
Outputs:
[00:07:36.031] "ms1"
[00:07:36.031] "li1"
FINAL QUESTION: Why is this not working for the anchor but works for LI and IMG? Is there something wrong in my approach? Am I on the right track for creating a slider with navigation effects or should I reconsider? Appreciate any help despite the length of my post!