Created
January 8, 2011 21:38
-
-
Save Wilto/771170 to your computer and use it in GitHub Desktop.
I know I'm reinventing the wheel here, but this is a simple carousel I whipped up for the sake of flexible layouts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
$.fn.carousel = function(config) { | |
var defaults = { | |
slider: '.slider', | |
slide: '.slide', | |
prevSlide: '.prev', | |
nextSlide: '.next', | |
speed: 500 | |
}, | |
opt = $.extend(defaults, config); | |
transitionSupport = document.body.style.webkitTransition !== undefined || | |
document.body.style.MozTransition !== undefined || | |
document.body.style.msTransition !== undefined || | |
document.body.style.OTransition !== undefined || | |
document.body.style.transition !== undefined; | |
$(opt.prevSlide).addClass('disabled'); | |
function moveNext($slider) { | |
var leftmargin = $slider.attr('style').match(/margin\-left: ?(.*)%;/) && parseFloat(RegExp.$1), | |
$slide = $slider.find(opt.slide); | |
if (!$slider.is(":animated") && (-leftmargin) != (($slide.length - 1) * 100)) { | |
leftmargin -= 100; | |
if(transitionSupport) { | |
$slider.css('marginLeft', leftmargin + "%"); | |
} else { | |
$slider.animate({ marginLeft: leftmargin + "%" }, opt.speed); | |
} | |
if((-leftmargin) == ($slide.length - 1) * 100) { | |
return false; | |
} | |
} | |
} | |
function movePrev($slider) { | |
var leftmargin = $slider.attr('style').match(/margin\-left: ?(.*)%;/) && parseFloat(RegExp.$1); | |
if(!$slider.is(":animated") && (leftmargin != 0)) { | |
leftmargin += 100; | |
if(transitionSupport) { | |
$slider.css('marginLeft', leftmargin + "%"); | |
} else { | |
$slider.animate({ marginLeft: leftmargin + "%" }, opt.speed); | |
} | |
if(leftmargin == 0) { | |
return false; | |
} | |
} | |
} | |
$(opt.nextSlide + ',' + opt.prevSlide).click(function(e) { | |
var $el = $(this), | |
link = $el.attr('href'), | |
$target = $(opt.slider).filter(link); | |
// $target = $(opt.slider).filter("[data-slider='" + $el.attr('data-slider') + "']"); | |
$(opt.nextSlide).each(function() { | |
if($(this)[0] == $el[0]) { | |
if(moveNext($target) === false) { | |
$el.addClass('disabled'); | |
}; | |
$(opt.prevSlide).filter(function() { | |
return this.getAttribute('href') === link; | |
}).removeClass('disabled'); | |
} | |
}); | |
$(opt.prevSlide).each(function() { | |
if($(this)[0] == $el[0]) { | |
if(movePrev($target) === false) { | |
$el.addClass('disabled'); | |
}; | |
$(opt.nextSlide).filter(function() { | |
return this.getAttribute('href') === link; | |
}).removeClass('disabled'); | |
} | |
}); | |
e.preventDefault(); | |
}); | |
return this.each(function() { | |
var $wrap = $(this), | |
$slider = $wrap.find(opt.slider), | |
$slide = $wrap.find(opt.slide), | |
slidenum = $slide.length, | |
speed = opt.speed / 1000; | |
$wrap.css({ | |
float: "left", | |
overflow: "hidden" | |
}); | |
$slider.css({ | |
marginLeft: "0px", | |
styleFloat: "left", | |
width: 100 * slidenum + "%", | |
"-webkit-transition": "margin-left " + speed + "s ease", | |
"-moz-transition": "margin-left " + speed + "s ease", | |
"-ms-transition": "margin-left " + speed + "s ease", | |
"-o-transition": "margin-left " + speed + "s ease", | |
"transition": "margin-left " + speed + "s ease" | |
}); | |
$slide.css({ | |
float: "left", | |
width: (100 / slidenum) + "%" | |
}); | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" id="home" class="somethingElse"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Carousel Plugin</title> | |
<meta name="description" content="There are many like it, but this one is mine." /> | |
<link rel="stylesheet" href="css/style.css" media="screen" /> | |
<style type="text/css" media="screen"> | |
ul { margin: 0; padding: 0; list-style: none; } | |
a.disabled { color: #aaa; } | |
</style> | |
</head> | |
<!--[if lt IE 7 ]> <body class="ie6"> <![endif]--> | |
<!--[if IE 7 ]> <body class="ie7"> <![endif]--> | |
<!--[if IE 8 ]> <body class="ie8"> <![endif]--> | |
<!--[if gt IE 8]> <body> <![endif]--> | |
<!--[if !IE]><!--> <body> <!--<![endif]--> | |
<div class="slidewrap"> | |
<ul class="slidecontrols"> | |
<li><a href="#sliderName" class="next">Next</a></li> | |
<li><a href="#sliderName" class="prev">Prev</a></li> | |
</ul> | |
<ul class="slider" id="sliderName"> | |
<li class="slide"> | |
<h2>First Slide</h2> | |
<p>In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien. Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget, scelerisque a libero.</p> | |
</li> | |
<li class="slide"> | |
<h2>Second Slide</h2> | |
<p>In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien.</p> | |
</li> | |
<li class="slide"> | |
<h2>Third Slide</h2> | |
<p>Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget.</p> | |
</li> | |
<li class="slide"> | |
<h2>Fourth Slide</h2> | |
<p>In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien. Phasellus quis est sed turpis sollicitudin venenatis sed eu odio.</p> | |
</li> | |
<li class="slide"> | |
<h2>Fif' Slide</h2> | |
<p>In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien.</p> | |
</li> | |
</ul> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript" charset="utf-8"></script> | |
<script type="text/javascript" src="plugins.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
$(document).ready(function() { | |
$('.slidewrap').carousel({ | |
slider: '.slider', | |
slide: '.slide', | |
nextSlide: '.next', | |
prevSlide: '.prev', | |
speed: 300 // ms. | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment