Attenuation between images
5 answers
A simple example that I knocked out.
<div id="imageSwap">
</div>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
setImageOne();
});
function setImageOne() {
$('#imageSwap').fadeIn(500).html('<img src="image01.jpg" />').delay(2000).fadeOut(500, function () { setImageTwo(); });
}
function setImageTwo() {
$('#imageSwap').fadeIn(500).html('<img src="image02.jpg" />').delay(2000).fadeOut(500, function () { setImageOne(); });
}
</script>
+6
rewrite your html to
<div>
<img class="current slide" src="image01.jpg" style="position:absolute;"/>
<img class="slide" src="image02.jpg" style="position:absolute;display:none"/>
<img class="slide" src="image03.jpg" style="position:absolute;display:none"/>
<img class="dummy" src="image01.jpg" style="visibility:none;"/>
</div>
I added a 3rd image for clarity. Add a jquery function like
function nextSlide() {
jQuery('.slide.current').each(function() {
var next = jQuery(this).next('.slide');
if (!next.size()) next = jQuery(this).siblings('.slide').eq(0);
jQuery(this).fadeOut().removeClass('current');
next.fadeIn().addClass('current');
});
slidetimer=setTimeout('nextSlide()',slidespeed);
}
And in your base javascript add
var slidespeed = 2000;
var slidetimer = null;
jQuery(document).ready(function() {
nextSlide()
});
, . div , - , div, - : . , , , div . , .
, , , .
$2, * -pike
+2
-, . , , - , , , .
-
I came up with this.
<style type="text/css">
#webcamImageDiv {
text-align: center;
background-image: url('webcam.jpg');
background-position: top center;
background-repeat: no-repeat;
}
</style>
<div id="webcamImageDiv">
<img id="webcamImageImg" src="webcam.jpg" alt="Image Not Available" />
</div>
<script type="text/javascript">
var refreshMillis = 10000;
function refreshWebcam() {
// set the div to the height of the image
$("#webcamImageDiv").height( $("#webcamImageImg").height() );
// get the path to the new image
// (in your case, your other image)
var newImg = "webcam.jpg?t=" + new Date().getTime();
// set the background of the div
$("#webcamImageDiv").css("background-image","url('" + newImg + "')");
// fade out the image
$("#webcamImageImg").fadeOut(500, function() {
// update the img tag, show the image
$("#webcamImageImg").removeAttr("src").attr("src", newImg).show();
// do it again
window.setTimeout(getWebcam, refreshMillis);
});
}
$(document).ready( function() {
window.setTimeout(refreshWebcam, refreshMillis);
});
</script>
0
You can do it without jquery. Only with css: And the cool wilting between the image
.fade img {
transition: all .8s;
max-width: 200px;
}
.fade:hover img:nth-child(2) {
opacity: 0;
}
.fade img:first-child {
position: absolute;
z-index: -1;
}<div class='fade'>
<img src="https://image.freepik.com/free-vector/software-logo_1103-316.jpg">
<img src='https://image.freepik.com/free-vector/m-letter-logo-template_1061-150.jpg'>
</div>0
Better control time with .animate and .css instead
This will create a coil with a soft crossfade.
In each cycle, it moves the invisible image forward than the opacity of the animation to 100. After the previous visible image is executed, then the opacity is set to 0 and moved forward, etc.
<div>
<div id="imageSwapA" style="display:block; z-index:100; opacity:0;">
<img src="imagea.png"/>
</div>
<div id="imageSwapB" style="display:block; z-index:101; opacity:0;">
<img src="imagea.png"/>
</div>
</div>
<script>
$(document).ready(function () {
setRollOne(); // <- animation can be triggered
function setRollOne() {
$('#imageSwapA').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
$('#imageSwapB').css({'opacity':0});
$('#imageSwapA').css({'z-index':100});
setTimeout(function(){ setRollTwo(); }, 1500);
});
}
function setRollTwo() {
$('#imageSwapB').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
$('#imageSwapA').css({'opacity':0});
$('#imageSwapB').css({'z-index':100});
setTimeout(function(){ setRollOne(); }, 1500);
});
}
});
</script>
-1