-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathswipy.min.js
57 lines (57 loc) · 13.3 KB
/
swipy.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;(function($,window,document,undefined){'use strict';var Swipy=function(options){this.defaults={master:'html',page:'body',path:{swipylib:'/scripts/swipy/lib',css:true,fastclick:true,hammer:true,showtouches:true,transit:true,waypoints:true,appcache:'/appcache.manifest'},animate:true,animate_onload:true,speed:200,scale:0.85,drag_timeout:3000,drag_distance:0.5,drag_min_distance:20,drag_min_deltaTime:20,edge_buffer:10,intercept:'a',ignore:'a[rel=external], a[rel=nofollow], a[href$=".pdf"], a[id^=fancybox]',swipynav:true,swiypynav_prependto:'body',showtouches:false,forceshowtouches:false,overflowHTML:false,velocity:false,velocity_trigger:0.65,webkitsticky:false,appcache:false,parallax:false,parallax_distance:150,parallax_offset:50,parallax_smoothing:.1,parallax_throttle:0,debug:false,debug_parallax:false};$.extend(true,this.defaults,options);this.statechanged=false;this.parallax_lastTimestamp=0;this.parallax_lastValues=[];this.parallax_motionmin=-this.defaults.parallax_offset;this.parallax_motionmax=0;};Swipy.prototype={swipyNav:function(options){var swipynav=$('\
<nav id="swipy-nav">\
<a href="#" id="swipy-left" class="swipy-icon"><i class="icon-chevron-left icon-left icon-left-open"></i></a>\
<a href="#" id="swipy-right" class="swipy-icon"><i class="icon-chevron-right icon-right icon-right-open"></i></a>\
<a href="#" id="swipy-clear" class="swipy-icon"><i class="icon-remove icon-cancel"></i></a>\
<a href="#" id="swipy-refresh" class="swipy-icon"><i class="icon-repeat icon-reload icon-refresh icon-cw"></i></a>\
<span id="swipy-secure" class="swipy-icon"><i class="icon-lock"></i></span>\
</nav>').prependTo($(options.swipynav_prependto));var height=swipynav.height();if(options.webkitsticky){swipynav.css('position','-webkit-sticky');}
else{var offset=height+22;$(options.page).children().first().css({y:offset});}
if(options.debug){console.log('Back available? '+self.Swipy.isBackAvailable());console.log('Forward available? '+self.Swipy.isForwardAvailable());console.log('Back available? '+self.Swipy.isBackAvailable());}
if(self.Swipy.isForwardAvailable()){swipynav.find('#swipy-right').on('click',function(e){e.preventDefault();e.stopPropagation();swipynav.find('#swipy-refresh i').addClass('icon-spin animate-spin');window.history.forward();});}
else{$('#swipy-right').addClass('swipy-disabled');}
if(self.Swipy.isBackAvailable()){swipynav.find('#swipy-left').on('click',function(e){e.preventDefault();e.stopPropagation();window.history.back();swipynav.find('#swipy-refresh i').addClass('icon-spin animate-spin');});}
else{swipynav.find('#swipy-left').addClass('swipy-disabled');}
$('#swipy-refresh').on('click',function(e){e.preventDefault();e.stopPropagation();swipynav.find('#swipy-refresh i').addClass('icon-spin animate-spin');window.location.reload();});if(window.location.protocol=='https:'){swipynav.find('#swipy-secure i').css({color:'#fff','text-shadow':'0 0 5px #fff'});}
return swipynav;},waypoints:function(options){Waypoints.ignore(options.ignore).intercept(options.intercept);if(options.debug){console.log('Current page: '+self.Waypoints.route());console.log('window.history.length: '+window.history.length);console.log('Waypoints.history(): '+self.Waypoints.history());console.log('Waypoints.target(): '+self.Waypoints.target());console.log(self.Waypoints);}
return Waypoints;},hammer:function(options){var hammerPage=$(options.master);var hammered=$(options.page);var hammertime=Hammer(hammerPage,{drag_min_distance:options.drag_min_distance,correct_for_drag_min_distance:true,drag_max_touches:1,drag_block_horizontal:true,drag_block_vertical:false,drag_lock_to_axis:true,drag_lock_min_distance:options.drag_min_distance+options.edge_buffer,hold:false,responsive:true,stop_browser_behavior:true,tap:false,transform:false});var scale=1,last_scale=1,MIN_ZOOM=0.5,MAX_ZOOM=3,cssOrigin;if(options.debug){var hammerDebug=$('<div id="hammer-debug"></div>').appendTo('body');}
hammerPage.on('drag',function(e){if(self.Swipy.isEdgeDrag(e,options)||!Modernizr.touch){var scale=Math.max(options.scale,1-(Math.tan(Math.abs(e.gesture.deltaX)/$(window).width())));hammered.css({x:(e.gesture.deltaX+(e.gesture.direction=='left'?-options.edge_buffer:options.edge_buffer))*scale,scale:scale});}});hammerPage.on('dragstart dragend',function(e){if(options.debug)
console.log('Event: '+e.type);switch(e.type){case'dragstart':if(options.debug){console.log('Drag started from x: '+e.gesture.startEvent.center.pageX+', y: '+e.gesture.startEvent.center.pageY);}
if(typeof(e.gesture)!=='undefined'){if(self.Swipy.isEdgeDrag(e,options)||!Modernizr.touch){hammered.addClass('dragging');}}
break;case'dragend':if(options.debug){console.log(e);if(typeof(e.gesture)!=='undefined'){console.log('Current deltaTime: '+e.gesture.deltaTime);console.log('Current direction: '+e.gesture.direction);}}
if(typeof(e.gesture!=='undefined')&&!window.Swipy.statechanged&&(self.Swipy.isEdgeDrag(e,options)||!Modernizr.touch)&&window.history.length>1&&((self.Swipy.isForwardAvailable()&&e.gesture.direction=='left')||(self.Swipy.isBackAvailable()&&e.gesture.direction=='right'))&&Math.abs(e.gesture.deltaX)>($(window).width()-($(window).width()*options.drag_distance))&&e.gesture.deltaTime>options.drag_min_deltaTime&&e.gesture.deltaTime<options.drag_timeout&&!hammered.hasClass('animating')){switch(e.gesture.direction){case'left':if(options.debug){console.log('Going left (forward)!');}
if(options.swipynav)
$('#swipy-refresh i').addClass('icon-spin animate-spin');window.history.forward();$(window).trigger('statechange');hammered.addClass('animating').transition({scale:options.scale},options.speed/1.5).transition({x:'-300%',scale:scale},options.speed/1.5,'in',function(){window.Swipy.statechanged=false;setTimeout(function(){hammered.addClass('animating').transition({x:0,scale:1},options.speed,'out',function(){$(this).removeClass('animating');});},options.drag_timeout/2);});break;case'right':if(options.debug)
console.log('Going right (back)!');if(options.swipynav)
$('#swipy-refresh i').addClass('icon-spin animate-spin');window.history.back();$(window).trigger('statechange');hammered.addClass('animating').transition({scale:options.scale},options.speed/1.5).transition({x:'200%',scale:scale},options.speed/1.5,'in',function(){window.Swipy.statechanged=false;setTimeout(function(){hammered.addClass('animating').transition({x:0,scale:1},options.speed,'out',function(){$(this).removeClass('animating');});},options.drag_timeout/2);});break;}}
else{setTimeout(function(){if(!hammered.hasClass('animating')&&!self.Swipy.statechanged){hammered.addClass('animating').transition({x:0,scale:1},options.speed,'out',function(){$(this).removeClass('animating').removeClass('dragging');});}
else{setTimeout(function(){hammerPage.trigger('dragend');},options.speed);}},options.drag_min_deltaTime);}
break;case'pinch':if(options.debug)
hammerDebug.text('Scaling at '+e.gesture.scale);scale=Math.max(MIN_ZOOM,Math.min(last_scale*e.gesture.scale,MAX_ZOOM));if((scale>1.10||scale<0.85)&&!hammered.hasClass('animating')){hammered.css({transformOrigin:e.gesture.center.pageX+', '+e.gesture.center.pageY,scale:scale,queue:false});}
else{hammered.css({transformOrigin:e.gesture.center.pageX+'px, '+e.gesture.center.pageY+'px',scale:1});}
break;case'touch':scale=Math.max(MIN_ZOOM,Math.min(last_scale*e.gesture.scale,MAX_ZOOM));last_scale=scale;break;}});return hammertime;},showtouches:function(options){yepnope.injectJs([(options.path.showtouches!==true)?options.path.showtouches:options.path.swipylib+'/hammer.showtouches'+(options.debug?'':'.min')+'.js'],function(){if(typeof(Hammer)!=='undefined'){var hammer=new Hammer($(options.page));Hammer.plugins.showTouches(options.forceshowtouches);}});},orientationChanged:function(e){var timestamp=e.timeStamp;var lastTimestamp=self.Swipy.parallax_lastTimestamp;var elapsedTime=timestamp-lastTimestamp;if(timestamp<lastTimestamp+self.Swipy.options.parallax_throttle)return false;var beta=e.accelerationIncludingGravity.x*2;var gamma=e.accelerationIncludingGravity.y*2;if(window.orientation===90){var tmpBeta=beta;beta=gamma;gamma=tmpBeta;}
else if(window.orientation===-90){var tmpBeta=beta;beta=-gamma;gamma=-tmpBeta;}
var xTilt=beta*90/self.Swipy.options.parallax_offset*((1/self.Swipy.options.parallax_distance)*100);var yTilt=gamma*90/self.Swipy.options.parallax_offset*((1/self.Swipy.options.parallax_distance)*100);if(elapsedTime>10&&elapsedTime<=150){xTilt=self.Swipy.lowpass(xTilt,0,elapsedTime);yTilt=self.Swipy.lowpass(yTilt,1,elapsedTime);}
window.Swipy.parallax_lastValues=[xTilt,yTilt];window.Swipy.parallax_lastTimestamp=timestamp;xTilt=xTilt<self.parallax_motionmin?self.parallax_motionmin:(xTilt>self.parallax_motionmax?self.parallax_motionmax:xTilt);yTilt=yTilt<self.parallax_motionmin?self.parallax_motionmin:(yTilt>self.parallax_motionmax?self.parallax_motionmax:yTilt);self.Swipy.setBackground(xTilt,yTilt);if(self.Swipy.options.debug_parallax){console.log('Last: '+elapsedTime+' ago - '+'Current alpha: '+Math.round(alpha)+' - '+'beta: '+Math.round(beta)+' - '+'gamma: '+Math.round(gamma)+' - '+'x tilt: '+xTilt+' - '+'y tilt: '+yTilt);console.log(e);}},setBackground:function(x,y){$(self.Swipy.options.master).css({'background-position':(-x-self.Swipy.options.parallax_offset/2)+'px '+(y-self.Swipy.options.parallax_offset/2)+'px'});},lowpass:function(value,index,elapsedTime){var smoothed=window.Swipy.parallax_lastValues[index];var add=(value-smoothed)/(self.Swipy.options.parallax_smoothing*elapsedTime);smoothed+=add;if(self.Swipy.options.debug){console.log(elapsedTime+': '+value);console.log('Adding: '+add);}
return smoothed;},isEdgeDrag:function(e,options){if(typeof(e.gesture)!=='undefined'){return((e.gesture.startEvent.center.pageX<(options.drag_min_distance+options.edge_buffer)&&e.gesture.direction=='right')||(e.gesture.startEvent.center.pageX>($(window).width()-options.drag_min_distance-options.edge_buffer)&&e.gesture.direction=='left'))?true:false;}
else{return false;}},isForwardAvailable:function(){if(typeof(self.Swipy.Waypoints)!=='undefined'){return((window.history.length>1&&window.location.href!==self.Swipy.Waypoints.target())?true:false);}
else{return true;}},isBackAvailable:function(){return(window.history.length>1?true:false);},swipe:function(options){var self=this;this.options={};var options=this.options=$.extend(true,this.defaults,options);this.drag_buffer=options.drag_min_distance+options.edge_buffer;if(options.path.css!==false){yepnope.injectCss([(options.path.css!==true)?options.path.css:options.path.swipylib+'/swipy.css']);}
if(options.debug){console.log('Swipin\' with options:');console.log(options);console.log('Swipy:')
console.log(self);console.log(navigator);console.log(window);}
if(options.path.fastclick!==false){yepnope.injectJs([(options.path.fastclick!==true)?options.path.fastclick:options.path.swipylib+'/fastclick'+(options.debug?'':'.min')+'.js'],function(){FastClick.attach(document.body);});}
if(options.path.transit!==false){if(options.debug)
console.log('Loading transit.js...');yepnope.injectJs([(options.path.transit!==true)?options.path.transit:options.path.swipylib+'/jquery.transit'+(options.debug?'':'.min')+'.js']);}
if(options.appcache)
$('html').attr('manifest',options.path.appcache+'/appcache.manifest');if(typeof(navigator.standalone)!=='undefined'&&navigator.standalone){if(options.overflowHTML)
$('html').css({overflow:'hidden'});if(typeof(Waypoints)!=='undefined'){self.Waypoints=self.waypoints(options);}
else{if(options.debug)
console.log('Loading Waypoints.js...');yepnope.injectJs([(options.path.waypoints!==true)?options.path.waypoints:options.path.swipylib+'/waypoints'+(options.debug?'':'.min')+'.js'],function(){self.Waypoints=self.waypoints(options);if(options.swipynav){if(options.debug)
console.log('Adding SwipyNav...');self.SwipyNav=self.swipyNav(options);}});}
if(options.animate){var links=$(options.intercept).not(options.ignore);links.on('click',function(e){var href=$(this).attr('href');if(typeof(href)!=='undefined'){if((href.indexOf('http')===-1||href.indexOf(document.location.host)>=0)&&href.indexOf('#')===-1&&e.defaultPrevented!==true){$(options.page).transition({scale:options.scale},options.speed,'inout').transition({x:'-300%',},options.speed,'in');}}});}
if(options.animate_onload&&screen.availHeight>=548){$(document).on('ready',function(e){$(options.page).css({x:'100%',scale:options.scale});});$(window).on('load',function(e){$(options.page).transition({x:0},options.speed,'out').transition({scale:1,},options.speed,'in');});}}
if((Modernizr.touch&&(typeof(navigator.standalone)!=='undefined'&&navigator.standalone))||options.showtouches){if(typeof(Hammer)!=='undefined'){self.Hammer=self.hammer(options);if(options.showtouches){self.showtouches(options);}}
else{if(options.debug)
console.log('Loading Hammer.js...');yepnope.injectJs([(options.path.hammer!==true)?options.path.hammer:options.path.swipylib+'/jquery.hammer'+(options.debug?'':'.min')+'.js'],function(){self.Hammer=self.hammer(options);if(options.showtouches){self.showtouches(options);}
$(window).on('statechange',function(e){window.Swipy.statechanged=true;});});}
if(options.parallax&&window.DeviceOrientationEvent&&screen.availHeight>=548){window.Swipy.parallax_lastTimestamp=0;window.Swipy.parallax_lastValues=[-1,-(self.options.parallax_offset*0.4)];self.setBackground(window.Swipy.parallax_lastValues[0],window.Swipy.parallax_lastValues[1]);window.addEventListener("devicemotion",self.orientationChanged,false);}}
return this;}};window.Swipy=new Swipy();})(window.jQuery||window.Zepto,window,document);