Skip to content

Instantly share code, notes, and snippets.

@ehanderson
Forked from ksolo/carousel.js
Last active December 25, 2015 19:59
Show Gist options
  • Save ehanderson/7031878 to your computer and use it in GitHub Desktop.
Save ehanderson/7031878 to your computer and use it in GitHub Desktop.
Image Carousel
// Shorthand for $(document).ready();
$(function(){
var ni = 1;
$('#next_frame').on('click',function(event){
event.preventDefault();
console.log(event)
console.log($('.frames li'));
console.log($('.frames li:nth-child('+ ni+ ')'))
$('.frames li:nth-child('+ ni+ ')').hide().animate({left: '-200px' }, ni++);
// console.log($('.frames li:nth-child('+ ni+ ')').prev().show())
if (ni > 3){
(ni = 1)
};
})
});
// console.log($(.frames li:first))
// $('.frames li:first').animate({'left': '500px'});
// $('.frames li:first').before($('frames_ul li:last'));
// $('.carousel_controls').click(function(){
// $(".carousel ul").animate({marginLeft:-480},1000,function(){
// $(this).find("li:last").after($(this).find("li:first"));
// $(this).css({marginLeft:0});
// })
// },5000);
// Your code goes here...
$(function(){
// var ni = 1;
$('.frames li:first').before($('frames.li:last'));
$('#next_frame').click(function(){
// event.preventDefault();
// console.log(event)
console.log($('.frames li'));
$('.frames li').animate({'left': '-500px' })
// console.log($('.frames li:nth-child('+ ni+ ')'))
// $('.frames li:nth-child('+ ni+ ')').animate({'left': '-500px' }, ni++),
$('.frames li:last').after($('frames.li:first'));
$('.frames li').css({'left': '0px' })
// console.log($('.frames li:nth-child('+ ni+ ')').prev().show())
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
<title>Image Carousel</title>
</head>
<body>
<div class="container">
<div class="carousel_controls">
<a href="#" id="previous_frame">&lt;</a>
<a href="#" id="next_frame">&gt;</a>
</div>
<div class="carousel">
<ul class="frames">
<li>
<h2>Zip</h2>
<img src="http://f.cl.ly/items/3e332b2e0B3d311a1U1X/wargames.jpg" />
</li>
<li>
<h2>Zap</h2>
<img src="http://f.cl.ly/items/2h0Q3P0t0B3Q0N370u43/businessdude.jpg" />
</li>
<li>
<h2>Zop</h2>
<img src="http://f.cl.ly/items/3i2V2V0D3n293d0y2y0U/tron.jpg" />
</li>
</ul>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="carousel.js"></script>
</body>
</html>
/*
* Attribution: http://csswizardry.com/2011/10/fully-fluid-responsive-css-carousel/
*
* DON'T VISIT THE ABOVE SITE FOR ANSWERS UNLESS YOU'RE
* COMPLETELY STUMPED
*
* ...also, it will not help you solve the JavaScript :)
*/
.container {
width: 360px;
height: 228px;
margin: 0 auto;
}
.carousel{
overflow:hidden;
width:100%;
}
.frames{
list-style:none;
/* position: What value should position have?; */
width:300%;
overflow:hidden;
padding:0;
margin:0;
}
.frames > li{
/* position: What value should position have?; */
float:left;
width:33.33333%; /* 100 / number of frames */
}
.carousel img{
display:block;
width:100%;
max-width:100%;
}
.carousel h2{
font-size:1em;
padding:0.5em;
position:absolute;
bottom:10px;
left:10px;
text-align:left;
margin: 0;
color:#fff;
background-color:rgba(99,0,0,0.75);
}
.carousel_controls a {
font-family: monospace;
text-decoration: none;
position: absolute;
margin-top: 99px;
width: 30px;
line-height: 30px;
background-color: #eee;
border: 1px solid #ddd;
border-radius: 30px;
text-align: center;
font-size: 22px;
font-weight: bold;
}
a#previous_frame {
margin-left: -50px;
}
a#next_frame {
margin-left: 380px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment