Created
June 4, 2012 18:54
-
-
Save halfempty/2870170 to your computer and use it in GitHub Desktop.
Simple jQuery Fade-in/Fade-out slideshow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="slideshow"> | |
<div> | |
<img src="1.jpg" alt="Image One" /> | |
<p class="controls"><a href="#2" class="next">Next</a></p> | |
</div> | |
<div> | |
<img src="2.jpg" alt="Image Two" /> | |
<p class="controls"><a href="#1" class="prev">Prev</a> / | |
<a href="#3" class="next">Next</a></p> | |
</div> | |
<div> | |
<img src="3.jpg" alt="Image Three" /> | |
<p class="controls"><a href="#2" class="prev">Prev</a></p> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
// Set the initial state: Hide all but the first slide | |
$('.slideshow').find('> div:eq(0)').nextAll().css({'opacity':'0','display':'none'}); | |
// On click of a controller link... | |
$('.controls > a').click(function(event) { | |
event.preventDefault(); | |
// Get the div containing the clicked link... | |
var currentslide = $(this).parents('div:first'); | |
// ... and get the index of that div | |
var currentposition = $('.slideshow div').index(currentslide); | |
// Use that index to get the slide we'll be fading to | |
var nextposition = ($(this).attr('class')=='next') ? currentposition+1 : currentposition-1; | |
// Fade the current slide out... | |
$('.slideshow div:eq('+currentposition+')').animate({opacity: 0}, 250, function() { | |
// ... and hide it. | |
$('.slideshow div:eq('+currentposition+')').css('display','none'); | |
// Show the next slide... | |
$('.slideshow div:eq('+nextposition+')').css('display','block'); | |
// ... and fade it in. | |
$('.slideshow div:eq('+nextposition+')').animate({opacity: 100}, 1500); | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rtyrtdfvjdkjhdkfj hfdgkjdh fgkjdgh dfhd fjkgfhdg dfg