Javascript Carousel

I am trying to create a auto-rotating carousel that performs several events with each change. I thought that I might be on the right track with where I am now but it isn't working. Any ideas? Thanks!
function changeSlide1() {
	document.querySelector('#carousel-img').src = "images/slide1.jpg";
	document.querySelector('#description').style.visibility = "visible";
	document.querySelector('#img-1').style.opacity = "1";
	document.querySelector('#img-2').style.opacity = "0.3";
	document.querySelector('#img-3').style.opacity = "0.3";
}

function changeSlide2() {
	document.querySelector('#carousel-img').src = "images/slide-2.jpg";
	document.querySelector('#description').style.visibility = "hidden";
	document.querySelector('#img-2').style.opacity = "1";
	document.querySelector('#img-1').style.opacity = "0.3";
	document.querySelector('#img-3').style.opacity = "0.3";
}

function changeSlide3() {
	document.querySelector('#carousel-img').src = "images/slide-3.jpg";
	document.querySelector('#description').style.visibility = "hidden";
	document.querySelector('#img-3').style.opacity = "1";
	document.querySelector('#img-1').style.opacity = "0.3";
	document.querySelector('#img-2').style.opacity = "0.3";
}

var changeArray = [
	changeSlide1,
	changeSlide2,
	changeSlide3
];

var count = 1;
window.setInterval(function() {
	changeArray[count];
	count++
	if (count > 2) {
		count = 0;	
	}
}, 4000)

Open in new window

LVL 2
Courtney WhitingConversion Lead / Information ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Why not make your array an array of images rather. Have one instance of change slide and have it swap the image for the next one in the array

function changeSlide(img, description) {
  document.querySelector('#carousel-img').src = img;
  document.querySelector('#description').style.visibility = description;
  document.querySelector('#img-3').style.opacity = "1";
  document.querySelector('#img-1').style.opacity = "0.3";
  document.querySelector('#img-2').style.opacity = "0.3";
}

var changeArray = [
	'images/slide1.jpg',
	'images/slide2.jpg',
	'images/slide1.jpg'
];
var count = 1;
window.setInterval(function() {
  // SET DESCRIPTION TO VISIBLE IF SLIDE 1 ELSE HIDDEN
  var description = (count % 3 == 0) ? 'visible' : ' hidden';
  // USE THE MOD (%) OPERATOR TO GENERATE INDEX 
  // INTO IMAGE ARRAY - REMOVES REQUIREMENT TO
  // CHECK AND SET TO 0
  changeSlide(changeArray[count %3], description);
  count++;
}, 4000);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Or make your visibility part of the array as well
var changeArray = [
  ['images/slide1.jpg','visible'],
  ['images/slide2.jpg', 'hidden'],
  ['images/slide1.jpg', 'hidden']
];
var count = 1;
window.setInterval(function() {
  // USE THE MOD (%) OPERATOR TO GET NEXT ITEM 
  // IN IMAGE ARRAY
  var item = changeArray[count % 0];
  // USE ITEM[0] FOR IMAGE AND ITEM[1] FOR VISIBILITY
  changeSlide(item[0]. item[1]);
  count++;
}, 4000);

Open in new window

0
Courtney WhitingConversion Lead / Information ArchitectAuthor Commented:
This code was great. As I implemented it though I realized that I would require a lot of follow up functionality (making the carousel stop when somethings clicked on, etc) and so I decided to keep it simple and not have the carousel auto rotate.

Thank you so much though Julian for helping out with that late night project!! You really came through when I just couldn't find the information I was looking for elsewhere.
0
Julian HansenCommented:
You are most welcome Courtney.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.