Skip to content

Instantly share code, notes, and snippets.

@joecue
Created March 1, 2017 00:29
Show Gist options
  • Save joecue/a2b02ce86fb6dc7c51388106524c7a3f to your computer and use it in GitHub Desktop.
Save joecue/a2b02ce86fb6dc7c51388106524c7a3f to your computer and use it in GitHub Desktop.
Web Design 1 Javascript Slider Code
#slider {
margin: 2em auto;
width: 960px;
overflow: hidden;
}
#slider-wrapper {
width: 9999px;
height: 300px;
position: relative;
transition: left 400ms linear;
}
.slide {
float: left;
width: 960px;
height: 300px;
position: relative;
overflow: hidden;
}
.slide img {
position: absolute;
top: 0;
left: 0;
}
.caption {
margin: 0;
position: absolute;
z-index: 100;
bottom: -2em;
left: 0;
width: 100%;
height: 2em;
line-height: 2;
text-align: center;
background: rgba( 0, 0, 0, 0.6 );
color: #fff;
transition: bottom 500ms ease-in;
}
.caption.visible {
bottom: 0;
}
#slider-nav {
margin: 1em 0;
text-align: center;
}
#slider-nav a {
width: 2em;
height: 2em;
border: 1px solid #ccc;
text-align: center;
text-decoration: none;
color: #000;
display: inline-block;
line-height: 2;
margin-right: 0.5em;
}
#slider-nav a.current {
border-color: #000;
}
<!DOCTYPE>
<html>
<head>
<script type="text/javascript" src="slider.js"></script>
<link rel="stylesheet" type="text/css" href="slider.css"/>
</head>
<body>
<div id="slider">
<div id="slider-wrapper">
<div class="slide">
<img src="http://lorempixel.com/960/300/sports" alt="" />
<p class="caption">Caption 1</p>
</div>
<div class="slide">
<img src="http://lorempixel.com/960/300/business" alt="" />
<p class="caption">Caption 2</p>
</div>
<div class="slide">
<img src="http://lorempixel.com/960/300/animals" alt="" />
<p class="caption">Caption 3</p>
</div>
<div class="slide">
<img src="http://lorempixel.com/960/300/food" alt="" />
<p class="caption">Caption 4</p>
</div>
<div class="slide">
<img src="http://lorempixel.com/960/300/nature" alt="" />
<p class="caption">Caption 5</p>
</div>
</div>
<div id="slider-nav">
<a href="#" data-slide="0">1</a>
<a href="#" data-slide="1">2</a>
<a href="#" data-slide="2">3</a>
<a href="#" data-slide="3">4</a>
<a href="#" data-slide="4">5</a>
</div>
</div>
</body>
</html>
function Slider( element ) {
this.el = document.querySelector( element );
this.init();
}
Slider.prototype = {
init: function() {
this.links = this.el.querySelectorAll( "#slider-nav a" );
this.wrapper = this.el.querySelector( "#slider-wrapper" );
this.navigate();
},
navigate: function() {
for( var i = 0; i < this.links.length; ++i ) {
var link = this.links[i];
this.slide( link );
}
},
animate: function( slide ) {
var parent = slide.parentNode;
var caption = slide.querySelector( ".caption" );
var captions = parent.querySelectorAll( ".caption" );
for( var k = 0; k < captions.length; ++k ) {
var cap = captions[k];
if( cap !== caption ) {
cap.classList.remove( "visible" );
}
}
caption.classList.add( "visible" );
},
slide: function( element ) {
var self = this;
element.addEventListener( "click", function( e ) {
e.preventDefault();
var a = this;
self.setCurrentLink( a );
var index = parseInt( a.getAttribute( "data-slide" ), 10 ) + 1;
var currentSlide = self.el.querySelector( ".slide:nth-child(" + index + ")" );
self.wrapper.style.left = "-" + currentSlide.offsetLeft + "px";
self.animate( currentSlide );
}, false);
},
setCurrentLink: function( link ) {
var parent = link.parentNode;
var a = parent.querySelectorAll( "a" );
link.className = "current";
for( var j = 0; j < a.length; ++j ) {
var cur = a[j];
if( cur !== link ) {
cur.className = "";
}
}
}
};
document.addEventListener( "DOMContentLoaded", function() {
var aSlider = new Slider( "#slider" );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment