/ Published in: JavaScript
Expand |
Embed | Plain Text
// JScript source code /* Créditos: Bases: Osiris Magro (http://innominepixel.wordpress.com) Creación de Plugin: Tonatiuh Núñez ([email protected]) */ jQuery.fn.slider = function (options) { var defaults = { speed: 500, duration: 5000, width: 'auto', height: 'auto' }; var options = jQuery.extend(defaults, options); var slides, timer; speed = options.speed; duration = options.duration; x = $(this).attr('id'); $('#slider', this).css({ 'width': options.width, 'height': options.height, 'overflow': 'hidden' }); //Styling the slider slides = $('#slider .sliderContainer > .slide', this); // Getting the slide's collection slides.each(function () { $(this).css({ 'width': options.width, 'height': options.height, 'float': 'left' }); //Styling each slide }); $('#slider .sliderContainer', this).css('width', slides[0].offsetWidth * slides.length); // Growing the slidesContainer which is gonna be moving var oThis = $(this); function sliderScroll(direction) { position = $('#slider', oThis).scrollLeft(); totalWidth = (slides.length * slides[0].offsetWidth) - slides[0].offsetWidth switch (direction) { case 'right': if (position + slides[0].offsetWidth > totalWidth) { $('#slider:not(:animated)', oThis).animate({ scrollLeft: 0 }, speed * (slides.length / 2)); } else { $('#slider:not(:animated)', oThis).animate({ scrollLeft: position + slides[0].offsetWidth }, speed); } break; case 'left': if (position - slides[0].offsetWidth < 0) { $('#slider:not(:animated)', oThis).animate({ scrollLeft: totalWidth }, speed * (slides.length / 2)); } else { $('#slider:not(:animated)', oThis).animate({ scrollLeft: position - slides[0].offsetWidth }, speed); } break; } } function initTimer() { timer = setInterval(function () { sliderScroll('right'); }, options.duration); } $('#next', this).click(function () { clearInterval(timer); sliderScroll('right'); initTimer(); return false; }); $('#prev', this).click(function () { clearInterval(timer); sliderScroll('left'); initTimer(); return false; }); initTimer(); }
You need to login to post a comment.
