jQuery: simultaneously fadeIn and fadeOut - javascript

the following code which is called periodically by setInterval performs the following sequence:
1. fade in an image for 750 msec
2. diplay it for 6 secs
3. fade out the image for 750 msec
4. randomly select another image (function randomPic)
5. fade in for 750 msec and so on:
$("#_fadee_").fadeIn(750, function() {
$("#_fadee_").delay(6000).fadeOut(750, randomPic);
});
You can see the effect here. How can I get the fadeOut of the old image and the fadeIn of the new one to run simultaneously?
Thanks, Ralf

Basically, you need to load the new image in another div that has a z-index beneath the image fading out. It's not that it's fading in simultaneously, it's just uncovered as the initial is fading out. Once the top div is faded out completely, you load the mew image into it and return its opacity to 1 so that it covers the div that you will load the next image into. In pseudocode it would look something like this:
var fadeO = function () {
var $fo = $('#_fadeO_');
var $fi = $('#_fadeI_');
var newImg = // load image here;
$fi.html(newImg);
$fo.fadeOut(1500, function() {
// put #_fadeO_ back on top with new image
$fo.html(newImg);
$fo.css({'display':'block', 'opacity':1});
// call function again after 6 seconds
setTimeout(fadeO, 6000);
});
};
fadeO();
...but I made a fiddle of it so you could see it in action, switching background colors instead of image contents. You should be able to get the idea of how to do the same with loaded images based on the above pseudo-code and how the HTML, CSS, and JS is set up here:
http://jsfiddle.net/UbmS9/

If you aren't doing anything else you could do this:
$("#_fadee_").fadeIn(750, function() {
setTimeout(function() {
$("#_fadee_").fadeOut(750, randomPic);
// Start fading in your other pic
}, 6000);
});
Alternatively, if they're both being displayed in the same area, you could fade in, and once faded in set the background image to the next image. Then, once that's faded out, set the background to the next, and so on.

Related

Main Page Background Image Slider Question

I am using JavaScript to change a background image on a page. It works, but there is a minor problem. For a split second, between images there is nothing (just white screen). Again it lasts just a split second but it is annoying.
How can I get one image to fade out while the other is fading in?
I've tried adjusting the fadeIn and fadeOut time, but I realized the second image doesn't start to fadeIn until the first completely fades out.
$(document).ready(function(){
var imgArr = new Array(
'images/home/background_image_1.jpg',
'images/home/background_image_2.jpg',
'images/home/background_image_3.jpg'
);
var preloadArr = new Array();
var i;
/* preload images */
for(i=0; i < imgArr.length; i++){
preloadArr[i] = new Image();
preloadArr[i].src = imgArr[i];
}
var currImg = 1;
var intID = setInterval(changeImg, 20000);
/* image rotator */
function changeImg(){
$('#background-image').fadeOut(1500, function(){
$(this).css('background','url(' + preloadArr[currImg++%preloadArr.length].src +')')
.css("background-position", "center").css("background-repeat", "no-repeat").css("background-size", "cover");
}).fadeIn(1500);
}
});
Any help would be greatly appreciated
Jquery's .fadeOut() has optional parameters .fadeOut( [duration ] [, complete callback ] ).
Where complete callback is the function that will run when the fading animation is done. So that split second is actually the duration you've passed to your .fadeOut() function.
Your code takes 1.5s to perform the animation and then only when it's done then it calls your function that fades in another background image. That's what causing the delay.
An alternative option would be to create seperate executions of .fadeOut() and .fadeIn(). And please don't apply the images on the same element, because you won't achieve it that way. You will need to create an <img> for each picture and set position: absolute on them to position them on top of each other. and then apply fade animation on each <img>
You might need this for revision here

How to crossfade an image with jQuery

I'm trying to crossfade an array of images, that transfer smoothly between each one, for a slideshow. I've tried fadeIn and fadeOut, and fadeTo but none seem to create the crossfade effect. None of the other articles on here could answer my question exactly.
The function isn't entirely finished, so I know the images won't reset, I'm just trying to get the crossfade to work.
$(function() {
var slides = ["images/slideshow1.jpg", "images/slideshow2.jpg", "images/slideshow3.jpg", "images/slideshow4.jpg", "images/slideshow5.jpg"];
var slidePos = 0;
setInterval(function() {
slidePos++;
$("#slideshow-image").fadeOut(500).fadeIn(1000).attr("src", slides[slidePos]);
}, 3000);
});
Actual: Image switches to next then fades out, then fades in
Expected: Cross-Fade
You must make a second image element with the opacity of zero and have it overlap by having its position set to absolute.
Then animate its opacity to 1.0 and remove the old image element which is now hidden underneath the new one.
Then repeat this process.
You don't have to create new elements if you just have two and cycle between them.

Image Slideshow not fading out then fading in?

I am trying to make a image slideshow that chooses a random image, fades out the current image, and fades in the randomly selected image. If that makes sense. If not, the code might explain what I'm trying to do:
function cycleImages(){
var images = [
"gallery-1.jpg",
"gallery-2.jpg",
"gallery-3.jpg"
];
var randomImg = images[Math.floor(Math.random()*images.length)]
$("#switch").attr("src",randomImg).fadeOut(4000).stop(true,true).hide().fadeIn(4000)
}
$(document).ready(function(){
setInterval('cycleImages()', 7000);
})
This is my first time using Jquery. I want it to fade out before it selects a randomly selected image, but it simply disappears with a hard transition and fades in the next image. Please help me out, thank you!
fadeOut takes a second parameter. A callback function when fading out is complete.
Something like this:
$("#switch").attr("src",randomImg).fadeOut(4000, () => {
//this will run after fading out.
$("#switch").fadeIn(4000);
});
fiddle: https://jsfiddle.net/17w3edgv/1/

Slideshow transition in javascript

I cant seem to get this slide show to work with javascript. the image fades in and out correctly on the first image but when transitioning for the second image it seems to immediately skip to the image instead of transition like the previous.
<script type="text/javascript">
var images = ["image1.png", "image2.png", "image3.jpg"];
var imagecount = 0;
window.setInterval(setImage,5000);
function setImage(){
$('.bgimage').fadeIn(5000);
$('.bgimage').css('background-image','url('+images[imagecount]+')');
$('.bgimage').fadeOut(5000);
imagecount++;
if(imagecount > 2){
imagecount=0;
}
}
</script>
Without seeing the html structure or a fiddle, it's hard to trouble-shoot with any accuracy. I'm guessing the issue is probably with your fadeIn and fadeOut calls, as they are currently set to animate for a full 5 seconds each, and animating at the same time as one another (They are called asynchronously). Instead, you should adjust the fadeIn method to execute after the fadeOut finishes using the built-in jQuery callback. Like so:
$('.bgimage').fadeOut(500, function() {
$('.bgimage').css('background-image','url('+images[imagecount]+')');
$('.bgimage').fadeIn(500);
});
I've also switched the order of your fade calls, as you should fade out the image, then update it (while it's not visible), then fade it back in. Your interval should still work the way you want, running every 5 seconds. Now the transitions won't take a full 5 seconds to animate.

Visual Delay when Hiding and Showing Images successively at a fast speed

I have about 50 Images that should be shown one after another inside a div.
The delay between showing one image then another is about 750 milliseconds and decreasing with each image.
I made sure that all images are loaded before this animation kicks in, by using:
(window).load(function() { });
The animaton is done using setTimeout
var index = 1;
function newImage(index) {
var interval = setTimeout( function(){
$("#image-container .image").css("display","none");
$("#image-container .image:nth-child("+index+")").css("display","block");
clearTimeout(interval);
index = index + 1;
newImage(index);
},delay[index-1]);
}
Where delay is an array of delays, something like [750,750,650,...].
The animation works fine, but there's a visual delay as fast as a blink of an eye, where no image is shown and only the background is visible, how can I avoid it?
try to use visibility css property instead of display

Categories

Resources