Jump to content
Search Community

Help with old school image carousel

retropunk test
Moderator Tag

Recommended Posts

Hey guys, I am building a quick carousel demo and I got stuck.

I am attaching the FLA

imageCarousel.zip

 

When you click the right button the container scrolls infinitely.

However when you start with the left button first you can see how the infinite loop takes a couple of button clicks for the container to reset properly.

 

Carl I used the infinite scroll loop you created a while back.

Same idea but I am adding a direction flag.

 

Thanks for the help everyone

- Patrick

 

 

Link to comment
Share on other sites

I figured it out! :)

Here is the code 

import com.greensock.*;

var bufferDistance:Number = 10;
var startX:Number = imgContainer.x;
var distanceToScroll:Number = imgContainer.mc1.width + bufferDistance;
var lastItemX : Number = imgContainer.width + bufferDistance;
var directionIs : String;

function resetContainer():void
{
	for each (var mc in imgContainer)
	{
		if (directionIs == "right")
		{
			mc.x += distanceToScroll;
			if (mc.x >= lastItemX) mc.x -= lastItemX;
		}
		else
		{
			mc.x -= distanceToScroll;
			if (mc.x < 0) mc.x += lastItemX;
		}
	}
	
	imgContainer.x = directionIs == "right" ? startX : startX;
	
	enableButtons();
}

function enableButtons():void
{
	btnPrev.addEventListener(MouseEvent.CLICK, buttonClicked);
	btnNext.addEventListener(MouseEvent.CLICK, buttonClicked);
}
enableButtons();

function disableButtons():void
{
	btnPrev.removeEventListener(MouseEvent.CLICK, buttonClicked);
	btnNext.removeEventListener(MouseEvent.CLICK, buttonClicked);
}

function buttonClicked(e:MouseEvent):void
{
	disableButtons();
	var nameIs:String = e.target.name;
	var scrollTo:Number;
	
	switch (nameIs)
	{
		case "btnPrev" :
			directionIs = "left";
			scrollTo = -distanceToScroll;
			break;
		case "btnNext" :
			directionIs = "right";
			scrollTo = distanceToScroll;
			break;
	}
	TweenMax.to(imgContainer, 0.5, {x:String(scrollTo), onComplete:resetContainer});
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...