forked from abitgone/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabitgone-carousel-min.js
30 lines (23 loc) · 8.58 KB
/
abitgone-carousel-min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
abitgone-carousel.js
=======================
Carousel progressively enhances HTML- and CSS- only carousels.
For information on how to use this plugin and for code examples, visit
http://abitgone.github.com/jQuery-Plugins/Carousel
License
-------
Author: Anthony Williams
Web: http://abitgone.github.com/jQuery-Plugins/Carousel
Copyright (c) 2012 Anthony Williams
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/!function(){var e=function(e,t){this.element=$(e);this.options=$.extend({},$.fn.abg_carousel.defaults,t);this.options.parent&&(this.$parent=$(this.options.parent));this.options.abg_carousel&&this.abg_carousel()};e.prototype={constructor:e,initialise:function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v=this.element;this.$carousel=v;this.carousel=v[0];e=v.find("[data-carousel-item] [data-carousel-pager]"),r=e.length==0;if(!r){t=$(v.find("[data-carousel-item] .pager-prev")[0]).clone();n=$(v.find("[data-carousel-item] .pager-next")[0]).clone();t[0].href="#";n[0].href="#";t.on("click",{increment:-1},$.proxy(this.move,this));n.on("click",{increment:1},$.proxy(this.move,this));v.append(t);v.append(n);e.remove()}i=v.find("[data-carousel-item]");this.$items=i;s=i.length;this.items=s;o=v.find("[data-carousel-items]");this.$container=o;u=0,a=0;this.carousel_style=v.is("[data-carousel-style]")?v.attr("data-carousel-style"):"horizontal";switch(this.carousel_style){case"vertical":v.addClass("carousel-vertical");this.lock_x=1;this.lock_y=0;break;case"class-only":v.addClass("carousel-classonly");o.css("position","relative");i.each(function(){$(this).css("position","absolute").css("top","0").css("left","0").css("width","100%")});this.lock_x=0;this.lock_y=0;break;default:v.addClass("carousel-horizontal");o.height("100%");o.width(s*100+"%");i.width(100/s+"%");this.lock_x=0;this.lock_y=1}v.attr("data-carousel-items",s);v.attr("data-carousel-item",0);if(!v.is("[data-carousel-nokeyboard]")&&!r){v.attr("tabindex","0");v.on("keydown",$.proxy(this.keydown,this));v.on("keyup",$.proxy(this.keymove,this))}f=0,l=0,c=0;if(!v.is("[data-carousel-notouch]")&&!r){v.on("touchstart",$.proxy(this.touchstart,this));v.on("touchmove",$.proxy(this.touchmove,this));v.on("touchend",$.proxy(this.touchend,this))}if(v.is("[data-carousel-timer]")){if(!v.is("[data-carousel-timer-nopause]")){v.on("mouseenter touchstart focus",$.proxy(this.pause,this));v.on("mouseleave touchend blur",$.proxy(this.resume,this))}v.abg_carousel("resume")}if(v.is("[data-carousel-pagelist]")){h=$('<table class="carousel-pagelist"><tr><td><ul class="carousel-pages"></ul></td></tr></table>');for(p=0;p<s;p++){d=$('<li><a href="#"><span>'+(p+1)+"</span></a></li>");d.on("click",{item_index:p},$.proxy(this.move_to,this));h.find("ul").append(d)}v.append(h);$(v.find("ul.carousel-pages li")[0]).addClass("active-page")}$(i[0]).addClass("active-item");v.addClass("carousel-ready")},touchstart:function(e){var t=e.originalEvent.touches[0];this.touch_origin_x=t.clientX;this.touch_origin_y=t.clientY;this.touch_distance_x_px=0;this.touch_distance_y_px=0;this.touch_distance_x_pc=0;this.touch_distance_y_pc=0;this.touch_element_width=this.carousel.clientWidth;this.touch_element_height=this.carousel.clientHeight;this.touch_started_time=(new Date).getTime();this.touch_itemindex=parseInt(this.$carousel.attr("data-carousel-item"));this.touch_itemcount=parseInt(this.$carousel.attr("data-carousel-items"))-1},touchmove:function(e){var t=e.originalEvent.touches[0];this.touch_duration=((new Date).getTime()-this.touch_started_time)/1e3;this.touch_distance_x_px=t.clientX-this.touch_origin_x;this.touch_distance_y_px=t.clientY-this.touch_origin_y;this.touch_distance_x_pc=this.touch_distance_x_px/this.touch_element_width*Math.abs(this.lock_x-1);this.touch_distance_y_pc=this.touch_distance_y_px/this.touch_element_height*Math.abs(this.lock_y-1);e.preventDefault()},touchend:function(e){var t,n,r,i,s,o,u,a,f;this.touch_ended_time=(new Date).getTime();this.touch_duration=(this.touch_ended_time-this.touch_started_time)/1e3;t=!!this.touch_distance_x_pc||!!this.touch_distance_y_pc;n=Math.max(-1,Math.min(1,this.touch_distance_x_pc/Math.min(1,this.touch_duration))),r=Math.max(-1,Math.min(1,this.touch_distance_y_pc/Math.min(1,this.touch_duration))),i=n+r;s=Math.round(n),o=Math.round(r),u=Math.round(i);a=this.touch_itemindex;switch(this.carousel_style){case"horizontal":if(s!=0){a=(a+s*-1)%(this.touch_itemcount+1);a=a<0?this.touch_itemcount:a;this.change_position(this.$carousel,this.$items,a)}break;case"vertical":if(o!=0){a=(a+o*-1)%(this.touch_itemcount+1);a=a<0?this.touch_itemcount:a;this.change_position(this.$carousel,this.$items,a)}break;case"class-only":if(u!=0){a=(a+u*-1)%(this.touch_itemcount+1);a=a<0?this.touch_itemcount:a;this.change_position(this.$carousel,this.$items,a)}}f=$(e.target).parents("[data-carousel]")},change_position:function(e,t,n){var r=e.find("[data-carousel-items]");t.removeClass("active-item");$(t[n]).addClass("active-item");e.attr("data-carousel-item",n);switch(e.attr("data-carousel-style")){case"vertical":r.css("top","-"+n*100+"%");break;case"class-only":break;default:r.css("left","-"+n*100+"%")}if(e.is("[data-carousel-pagelist]")){$carousel_pages=e.find("ul.carousel-pages li");$carousel_pages.removeClass("active-page");$($carousel_pages[n]).addClass("active-page")}},keydown:function(e){if(e.keyCode!=37&&e.keyCode!=38&&e.keyCode!=39&&e.keyCode!=40)return;e.preventDefault()},keymove:function(e){var t,n=this.$carousel,r=this.$items,i=parseInt(n.attr("data-carousel-item")),s=parseInt(n.attr("data-carousel-items"));if(e.keyCode==37&&this.lock_x==0||e.keyCode==38&&this.lock_y==0)t=-1;else{if(!(e.keyCode==39&&this.lock_x==0||e.keyCode==40&&this.lock_y==0))return;t=1}e.preventDefault();i=(i+t)%s;i=i<0?s-1:i;this.change_position(n,r,i)},move:function(e){var t=$(e.target).parents("[data-carousel]"),n=t.find("[data-carousel-item]"),r=parseInt(t.attr("data-carousel-item")),i=parseInt(t.attr("data-carousel-items")),s=e.data.increment;r=(r+s)%i;r=r<0?i-1:r;this.change_position(t,n,r);e.preventDefault()},move_to:function(e){var t=$(e.target).parents("[data-carousel]"),n=t.find("[data-carousel-item]");this.change_position(t,n,e.data.item_index);e.preventDefault()},cycle:function(){var e,t=$(this.element),n=t.find("[data-carousel-item]"),r=parseInt(t.attr("data-carousel-item")),i=parseInt(t.attr("data-carousel-items"));if(t.attr("data-carousel-interval-id")!==undefined){intervalId=parseInt(t.attr("data-carousel-interval-id"));window.clearTimeout(parseInt(t.attr("data-carousel-interval-id")))}r=(r+1)%i;r=r<0?i-1:r;this.change_position(t,n,r);e=parseInt(t.attr("data-carousel-timer"));timeoutId=window.setTimeout(function(){t.abg_carousel("cycle")},e);t.attr("data-carousel-interval-id",timeoutId)},pause:function(){var e=$(this.element);e.removeClass("carousel-waiting");if(e.attr("data-carousel-interval-id")!==undefined){timeoutId=parseInt(e.attr("data-carousel-interval-id"));window.clearTimeout(parseInt(e.attr("data-carousel-interval-id")))}e.addClass("carousel-paused")},resume:function(){var e,t=$(this.element);t.removeClass("carousel-paused");e=parseInt(t.attr("data-carousel-timer"));if(t.is("[data-carousel-timer-delay]")){e+=parseInt(t.attr("data-carousel-timer-delay"));t.removeAttr("data-carousel-timer-delay")}timeoutId=window.setTimeout(function(){t.abg_carousel("cycle")},e);t.attr("data-carousel-interval-id",timeoutId)}};$.fn.abg_carousel=function(t,n){return this.each(function(){var r=$(this),i=r.data("carousel"),s=typeof t=="object"&&t;i||r.data("carousel",i=new e(this,s));typeof t=="string"&&i[t](n)})};$.fn.abg_carousel.defaults={carousel:!0};$.fn.abg_carousel.Constructor=e;$(function(){$("[data-carousel]").abg_carousel("initialise")})}(window.jQuery);