Last active
August 29, 2015 14:03
-
-
Save findyourmagic/eaab375a9b01cfb6d754 to your computer and use it in GitHub Desktop.
JQuery Gist
This file contains hidden or 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
| // ********************* | |
| // Carousel plugin for jQuery | |
| // 2014.07.17 by Daniel.Liu | |
| // Example: | |
| // $('.box').carousel({'ad' :'.boxItem' }); // just fadeIn mode | |
| // $('.box').carousel({'ad' :'.boxItem' , time : 8000 }); // change the carousel time | |
| // $('.box').carousel({'ad' : '.boxItem' , 'ads' : '.boxInner'}); // vertical animation mode | |
| // ********************* | |
| ;(function($) { | |
| 'use strict' | |
| $.fn.carousel = function(option) { | |
| if (!option.ad) { | |
| return false; | |
| }; | |
| $(this).each(function() { | |
| var $banners = $(this), | |
| $ad = $banners.find(option.ad), | |
| step = 0, | |
| length = $ad.length, | |
| carouselTimeout, | |
| btnHtml; | |
| if (length == 0) { | |
| return false; | |
| }; | |
| if (option.ads && $.trim(option.ads)) { | |
| var $ads = $banners.find(option.ads), | |
| height = $ad.eq(0).outerHeight(); | |
| } | |
| var appendBtns = function() { | |
| btnHtml = '<div class="carousel-btns">'; | |
| btnHtml += '<i class="carousel-btn on"></i>'; | |
| for (var i = 1; i < length; i++) { | |
| btnHtml += '<i class="carousel-btn"></i>'; | |
| } | |
| btnHtml += '</div>'; | |
| $banners.append(btnHtml); | |
| } | |
| appendBtns(); | |
| var $btn = $banners.find('.carousel-btn'); | |
| if ($ads && $ads.length) { | |
| var animate = function() { | |
| var $onProduct = $ad.eq(step).addClass('on'); | |
| $ads.animate({ | |
| 'top': (-height * step) | |
| }, function() { | |
| $ad.not($onProduct).removeClass('on'); | |
| }); | |
| $btn.removeClass('on').eq(step).addClass('on'); | |
| } | |
| } else { | |
| var animate = function() { | |
| $btn.removeClass('on').eq(step).addClass('on'); | |
| $ad.fadeOut().removeClass('on').eq(step).fadeIn().addClass('on'); | |
| } | |
| } | |
| var run = function(num) { | |
| if (num || num == 0) { | |
| step = num; | |
| } | |
| animate(); | |
| if (step < length - 1) { | |
| step++; | |
| } else { | |
| step = 0; | |
| } | |
| carouselTimeout = setTimeout(run, option.time || 4000); | |
| } | |
| run(); | |
| $btn.click(function() { | |
| var $this = $(this), | |
| index = $btn.index(this); | |
| if ($this.hasClass('on')) { | |
| return false; | |
| }; | |
| clearTimeout(carouselTimeout); | |
| run(index); | |
| }); | |
| }); | |
| } | |
| })(jQuery); |
This file contains hidden or 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
| $("body,html").animate({"scrollTop":0},500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment