-
Notifications
You must be signed in to change notification settings - Fork 9
/
jquery.threesixty.min.js
4 lines (3 loc) · 1.7 KB
/
jquery.threesixty.min.js
1
2
3
4
// jquery.threesixty.js
// @weblinc, @jsantell, (c) 2012
(function($){$.fn.threesixty=function(settings){function eMouseMove(e){if(!mouseDown)return;var $this=$(this),lastPos=$this.data("lastPos"),curPos=normalizePosition(e);if(curPos>lastPos+options.sensitivity||curPos<lastPos-options.sensitivity)changeFrame.call($this,curPos>lastPos?1:-1),$this.data("lastPos",curPos);e.preventDefault()}function eMouseUp(){mouseDown=!1}function eMouseDown(e){$(this).data("lastPos",normalizePosition(e)),mouseDown=!0,e.preventDefault()}function changeFrame(dir){var bgPos=getBackgroundPos.call(this);newPos=bgPos[axis]+options.spriteDim[axis]*dir,newPos>=options.spriteSheetDim[axis]?newPos=0:newPos<0&&(newPos=options.spriteSheetDim[axis]-options.spriteDim[axis]),bgPos[axis]=newPos,this.css("background-position",bgPos.x+"px "+bgPos.y+"px")}function getBackgroundPos(){var pos=(this.css("background-position")||"").split(" ");return{x:parseInt(this.css("background-position-x")||pos[0],10),y:parseInt(this.css("background-position-y")||pos[1],10)}}function normalizePosition(e){return(e.touches&&e.touches.length?e.touches[0]:e)["page"+dragAxis]}var options=$.extend({},$.fn.threesixty.defaults,settings),mouseDown=!1,axis=options.spriteSheetDim.x>options.spriteSheetDim.y?"x":"y";dragAxis=options.dragAxis.toUpperCase(),this.each(function(){$(this).mousemove(eMouseMove).mousedown(eMouseDown).data("lastPos",0),this.addEventListener&&(this.addEventListener("touchmove",eMouseMove),this.addEventListener("touchstart",eMouseDown))}),$(document).bind("mouseup",eMouseUp),document.addEventListener&&document.addEventListener("touchend",eMouseUp)},$.fn.threesixty.defaults={dragAxis:"x",sensitivity:3,spriteSheetDim:{x:0,y:0},spriteDim:{x:0,y:0}}})(jQuery)