Simple GSAP Slider

Developer
Size
2,839 Kb
Views
101,200

How do I make an simple gsap slider?

For the user AncientWarrior from GSAP forums. How to create very simple slider with TweenMax. Just prev/next buttons and autoplay, customisable transitions. . What is a simple gsap slider? How do you make a simple gsap slider? This script and codes were developed by Chrysto on 16 July 2022, Saturday.

Simple GSAP Slider Previews

Simple GSAP Slider - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simple GSAP Slider</title> <script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="demo"> <div class="slider-holder"> <div class="slider"> <div id="slider-item-1" class="slider-item">1</div> <div id="slider-item-2" class="slider-item">2</div> <div id="slider-item-3" class="slider-item">3</div> <div id="slider-item-4" class="slider-item">4</div> <div id="slider-item-5" class="slider-item">5</div> <div id="slider-item-6" class="slider-item">6</div> </div> <div class="slider-nav"> <div class="slider-nav-btn go-prev">Prev</div> <div class="slider-nav-btn go-next">Next</div> <div class="slider-nav-btn start-autoplay">Start autoplay</div> <div class="slider-nav-btn stop-autoplay">Stop autoplay</div> </div> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/gsap/1.11.8/TweenMax.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Simple GSAP Slider - Script Codes CSS Codes

div {	margin:0;	padding:0;	outline:none;
}
.demo {	position:relative;	margin-top:50px; margin-bottom:50px;
}
.slider-holder {	position:relative;	width:600px;	margin:0 auto;
}
.slider {	position:relative;	width:600px;	height:400px;	background-color:#000;	overflow:hidden;
}
.slider-item {	position:absolute;	top:0;	left:0;	width:100%;	height:100%;	background-color:red;
}
/* Demo styles */
#slider-item-1 {	background-color:#b6e3ff;
}
#slider-item-2 {	background-color:#55a93b;
}
#slider-item-3 {	background-color:#665cff;
}
#slider-item-4 {	background-color:#2d0bff;
}
#slider-item-5 {	background-color:#ffc725;
}
#slider-item-6 {	background-color:#ff4ff1;
}
/* Navigation */
.slider-nav {	margin-top:20px;
}
.slider-nav-btn {	display:inline-block;	padding:4px;	margin-right:10px;	cursor:pointer;	background-color:#add8e6;	border-radius:3px;	-webkit-transition:all .2s;	transition:all .2s;
}
.slider-nav-btn:hover {	background-color:#6495ed;
}
.slider-nav-btn:last-child {	margin-right:0;
}

Simple GSAP Slider - Script Codes JS Codes

var $slider = $(".slider");
var $slides = $slider.find(".slider-item");
var $navPrev = $(".go-prev");
var $navNext = $(".go-next");
var $startAutoplay = $(".start-autoplay");
var $stopAutoplay = $(".stop-autoplay");
var slidesNum = $slides.length;
var prevSlideID = null;
var currentSlideID = 0;
var isAnimating = false;
var isAutoPlay = false;
function init() {	TweenLite.set($slides, {	left: "-100%"	});	$navPrev.on("click", gotoPrevSlide);	$navNext.on("click", gotoNextSlide);	$startAutoplay.on("click", startAutoPlay);	$stopAutoplay.on("click", stopAutoPlay);	gotoSlide(0, 0);
}
function gotoPrevSlide() {	var slideToGo = currentSlideID - 1;	if (slideToGo <= -1) {	slideToGo = slidesNum - 1;	}	stopAutoPlay();	gotoSlide(slideToGo, 1, "prev");
}
function gotoNextSlide() {	var slideToGo = currentSlideID + 1;	if (slideToGo >= slidesNum) {	slideToGo = 0;	}	stopAutoPlay();	gotoSlide(slideToGo, 1, "next");
}
function gotoSlide(slideID, _time, _direction) {	if (!isAnimating) {	isAnimating = true;	prevSlideID = currentSlideID;	currentSlideID = slideID;	var $prevSlide = $slides.eq(prevSlideID);	var $currentSlide = $slides.eq(currentSlideID);	var time = 1;	if (_time !== null) {	time = _time;	}	var direction = "next";	if (_direction != null) {	direction = _direction;	}	if (direction == "next") {	TweenLite.to($prevSlide, time, {	left: "-100%"	});	TweenLite.fromTo($currentSlide, time, {	left: "100%"	}, {	left: "0"	});	} else {	TweenLite.to($prevSlide, time, {	left: "100%"	});	TweenLite.fromTo($currentSlide, time, {	left: "-100%"	}, {	left: "0"	});	}	TweenLite.delayedCall(time, function() {	isAnimating = false;	});	}
}
function play(){ gotoNextSlide(); TweenLite.delayedCall(4, play);
}
function startAutoPlay(immediate) {	if (immediate != null) {	immediate = false;	}	if (immediate) {	gotoNextSlide();	}	TweenLite.delayedCall(4, play);
}
function stopAutoPlay() { isAutoPlay = false;	TweenLite.killDelayedCallsTo(play);
}
init();
Simple GSAP Slider - Script Codes
Simple GSAP Slider - Script Codes
Home Page Home
Developer Chrysto
Username bassta
Uploaded July 16, 2022
Rating 3.5
Size 2,839 Kb
Views 101,200
Do you need developer help for Simple GSAP Slider?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Chrysto (bassta) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!