Reputation: 7059
I have this really simple jQuery Slideshow: http://jsfiddle.net/6zA4B/
HTML:
<div class="fadein">
<img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">
<img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">
<img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">
</div>
JavaScript:
$(function(){
$('.fadein img:gt(0)').hide();
setInterval(function(){$('.fadein :first-child').fadeOut().next('img').fadeIn().end().appendTo('.fadein');}, 3000);
});
It works perfectly, I just want to replace the 3 img tags with 3 divs (so I can include a caption below the images). How can I modify the script to achieve that? I tried with this but probably I'm doing something wrong...
HTML:
<div class="fadein">
<p><img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">image1</p>
<p><img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">image2</p>
<p><img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">image3</p>
</div>
JavaScript:
$(function(){
$('.fadein p:gt(0)').hide();
setInterval(function(){$('.fadein :first-child').fadeOut().next('p').fadeIn().end().appendTo('.fadein');}, 3000);
});
http://jsfiddle.net/S4SmM/1/ :D
Upvotes: 3
Views: 33227
Reputation: 1
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<style>
.slider{
position: relative;
width: 1000px;
height: 300px;
margin: 0px auto;
}
img {
position: absolute;
top:0;
left:0;
width: 1000px;
height: 300px;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
$( document ).ready(function(){
fade();
setInterval(fade, 5000);
});
var i = 1;
function fade() {
$('#img-' + i).fadeOut(2000);
i++;
if (i>3){
i = 1
}
$('#img-' + i).fadeIn(2000);
} </script>
</head>
<body>
<div class="slider">
<img id="img-1" src="https://i.picsum.photos/id/1028/1921/1480.jpg?hmac=7rV8P9L5kV_5PMZcIIXOT-wN3gn7VwoYLl5YC4uNr0g">
<img id="img-2" src="https://i.picsum.photos/id/455/1922/1480.jpg?hmac=vqLUitWLN713y2jRmGcpEAChI9qUH5q7HB1yLjPNjYU">
<img id="img-3" src="https://i.picsum.photos/id/798/1920/1480.jpg?hmac=9yv1GtCO1YXMiM0ShMmoT-LJn0ZrV6A7a_aY0VWWqr8">
</div>
</body>
</html>
Upvotes: 0
Reputation: 5721
You can also use a plugin jquery-fade-slider
$(selector).fadeSlider()
Upvotes: 0
Reputation: 249
I like Andrew Whittaker's answer, but prefer to use the Fadeout callback to give it a little more "animation":
setInterval(function(){$('.fadein > :first-child').fadeOut(1000, function() {
$(this).next('p').fadeIn(1000).end().appendTo('.fadein');
})
}, 5000);
Upvotes: 0
Reputation: 2655
Use the following code:
HTML:
<div class="fadein">
<div><img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg"><p>image1</p></div>
<div><img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg"><p>image2</p></div>
<div><img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg"><p>image3</p></div>
</div>
CSS:
.fadein { position:relative; height:332px; width:500px; }
.fadein div {position:absolute;text-align:center;height:100%;}
.fadein p { position:absolute; bottom:0;width:100%;color:white;background-color:rgba(0,0,0,0.6);height:1em;padding-bottom:10px;}
JS:
$.fn.nextOrFirst = function(selector) {
var next = this.next(selector);
return (next.length) ? next : this.prevAll(selector).last();
}
$(function() {
$('.fadein div:eq(0)').addClass("active");
$('.fadein div:gt(0)').hide();
setInterval(function() {
$('.active:eq(0)').fadeOut().removeClass("active").nextOrFirst('div').addClass("active").fadeIn().end()
}, 3000);
});
JsFiddle: http://jsfiddle.net/S4SmM/5/
Upvotes: 4
Reputation: 126042
One of your selectors is slightly off:
$('.fadein :first-child')
Is selecting all elements that are first children underneath .fadein
. This includes the first child of the p
elements, which are the images you're trying to rotate to.
You want to restrict the :first-child
selector to elements directly under .fadein
. One way would be to use the child selector:
$('.fadein > :first-child')
Example: http://jsfiddle.net/S4SmM/4/
Upvotes: 8