JS image carousel - how can I use CSS transitions for effect?

Hello, all,

I’ve got a very simple JS image carousel for the home page of a side-project that I’m working on. I’d like to implement some kind of fade transition for the images, but don’t know where to start.

Here’s the code that I have, so far.

To start, I create some nested DIVs - the outer DIV is going to have its background image set to display the images; the next nested DIV is just a container; the third nested DIV holds text for the image that is currently displayed.

#home_image {
	width: 100%;
	position: relative;
	height: 400px;
	border: 0px;
	overflow: auto;
	vertical-align: middle;
	border-bottom: 3px solid #AAAAAA;
	background-size: cover;
	background-repeat: no-repeat;
	background: center center;
}

#home_image_caption {
	width: 25%;
	padding: 15px;
	position: absolute;
	background: #000000;
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)";
	filter: alpha(opacity=70);
	-moz-opacity: 0.7;
	-khtml-opacity: 0.7;
	opacity: 0.7;
	height: 370px;
	top: 50%;
	margin-top: -200px;
	margin-left: 10px;
}

#home_image_caption_text {
	background-color: transparent;
}

<div id="home_image">
    <div id="home_image_caption">
    	<div style="border-bottom: 1px solid gold; text-align: center;">THIS NEVER CHANGES</div>
    	<div id="home_image_caption_text"></div>
    </div>
</div>

Next, I have one containing DIV that holds four DIVs; the container DIV is set to display:none; and the four DIVs are dynamically loading images from a database. I’m using another page that has CFHEADER and CFCONTENT to display the image. Works great! :smile:

<div id="picrange" style="display:none;">
<div id="pic1" style="background-image: url('display.cfml?t=i&section=home&id=86037244-AC72-5285-49ED0901FBF7A2F1');"></div>
<div id="pic2" style="background-image: url('display.cfml?t=i&section=home&id=8627C2BB-C575-5275-55ACDB774AEBDC8E');"></div>
<div id="pic3" style="background-image: url('display.cfml?t=i&section=home&id=863FE57A-E172-01D9-E47F8D34365B415A');"></div>
<div id="pic4" style="background-image: url('display.cfml?t=i&section=home&id=8642214F-B157-9BDD-A4511DFF7F708591');"></div>
</div>

Now for the JavaScript. This will set the background image of the ‘home_image’ DIV and the text for the caption. Then I set an interval so it will change every 9 seconds.

var currentPic = "pic1", bgstl, biURL, overlayText = document.getElementById('home_image_caption_text');
	function homeCarousel(objDiv){
		currentPic = objDiv.id;
		bgstl = objDiv.currentStyle || window.getComputedStyle(objDiv,false);
		biURL =  bgstl.backgroundImage.slice(4,-1);
		imgholder = document.getElementById('home_image');
		imgholder.style.backgroundImage = "url(" + biURL + ")";
		switch(currentPic){
			case "pic1":
				currentPic = "pic2";
				overlayText.innerHTML = "IMAGE 1: This is just some random text to be used as copy placement. Lorem ipsum and all that other stuff.";
			break;
			case "pic2":
				currentPic = "pic3";
				overlayText.innerHTML = "IMAGE 2: The quick brown fox jumped over the lazy dogs. Then the dogs gave chase and ate the fox. The quick brown fox jumped over the lazy dogs. Then the dogs gave chase and ate the fox.";
			break;
			case "pic3":
				currentPic = "pic4";
				overlayText.innerHTML = "IMAGE 3: This is just some random text to be used as copy placement. Why are you reading all of this text? It is nonsense. Just for showing what it can look like.";
			break;
			case "pic4":
				currentPic = "pic1";
				overlayText.innerHTML = "IMAGE 4: This is just some random text to be used as copy placement. This is just some random text to be used as copy placement. This is just some random text to be used as copy placement.";
			break;
			}
		}

Then, I use document ready to start the function and set the interval.

	homeCarousel(document.getElementById(currentPic));
	setInterval(function(){homeCarousel(document.getElementById(currentPic));},9000);

How can I implement a fading transition with this? Either strictly CSS, or a JS/CSS combo, or just plain JS. Is it possible?

V/r,

:slight_smile:

Is it possible to get some sort of demo?

It’s sort of hard to follow your post. Is there any sort of identifying class (yet) on each carousel div that is being displayed? I can’t read JS all that well (well I’ve been coding all day so I’m exhausted).

Basically, every 9 seconds, home_image div changes background images?

CSS animations can fade this in (dunno what sort of specific transition you want). You’ll just need to add a class to the home_image div. Since CSS animations only work the first time around, you can then just remove/reapply the class to reset the animation. I am letting you know of this route, because IIRC, you cannot use jQuery.

There might be a simpler way, but my brain is fried. Time for taco bell to recover.

Ah… I can try to get something going. If I can upload images to it, for the demo. There is not currently any class on the DIVs, just IDs.

Yes. Every nine seconds, the JS will get the background image of one of the DIVs that are within the hidden DIV and set the large, image displaying DIV (“home_image”) to that background image, and change the caption text.

Och… I love Taco Bell, too… but not when brain is fried… that’s more of a call for White Castle. :smile:

V/r,

:slight_smile:

Okay… for some reason the caption text isn’t changing along with the images. BUT, at least the images are changing every 9 seconds!

Here it is. MY VERY FIRST CODEPEN! (I can access it, now, because I’m not at work.)

What ya think?

EDIT: FIXED THE TEXT. :slight_smile:

V/r,

:slight_smile:

Well CSS won’t be able to do any sort of animation for the background image by default, unless you want some sort of opacity effect (CSS can only animate certain properties.)
http://codepen.io/ryanreese09/pen/KpaEZa

In this example, I add a class to home_image (.animate) and use a CSS animation to transition from 0 opacity, to full opacity.

CSS has an issue where the animation will only run once. The only way to get around this is to remove and reapply the class. Not as simple as doing that though because if you do this in the course of a single block function, it appears to the rendering engine that nothing has changed. This means wrapping the add class in a timeout. I see it working around 25ms, but any lower than that and it appears that it’s not enough time for the rendering engine to process this, and doesn’t get applied (consistently.)

This seems to work nicely for me though. Thoughts? Sorry for not responding all weekend. I buried my head in my personal code project :slight_smile: .

I just made a change (not sure if you’ve looked yet.)

I moved the opacity from .animation to #home_image due to when I remove the class, and the background changes, the image will show. Then after 25ms, the animation will go to work which results in a flash where it shows (doesn’t have the animate class, and thus nothing to hide it), hides (from the opacity:0; on .animate), then shows again (animation taking effect). Looks bad.

Fixing this is easy. Have the 0 opacity state by default. That means moving the opacity.

NOW it works pretty much perfectly.

Thanks for your wisdom, @RyanReese.

I’ll play around with this at home, tonight. And it was a busy weekend all around, I guess. I didn’t get much sleep, myself. Did the Taco Bell help? :smile:

V/r,

:slight_smile:

This senior developer at my work is leaving (keep fingers crossed I get it…) and she got about 50 free Chick Fil A giftcards. Each good for a single chicken sandwich. They can only be used in our geographical location, and she’s moving to Ohio. As a result, she gave about 20 of them to me. I ended up driving a couple more minutes and got myself a couple sandwiches for free :slight_smile: . That was my fuel.

Anytime :slight_smile: . It works pretty nicely so you shouldn’t have to do much of any fixing or playing. Let me know if you need additional help.

1 Like

Keeping fingers duly crossed. Best of luck!

V/r,

:slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.