forked from weblinc/jquery-threesixty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.threesixty.js
86 lines (71 loc) · 2.45 KB
/
jquery.threesixty.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// jquery.threesixty.js
// @weblinc, @jsantell, (c) 2012
;(function( $ ) {
$.fn.threesixty = function ( settings ) {
var
options = $.extend( {}, $.fn.threesixty.defaults, settings ),
mouseDown = false,
axis = options.spriteSheetDim.x > options.spriteSheetDim.y ? 'x' : 'y';
dragAxis = options.dragAxis.toUpperCase();
this.each(function () {
$(this)
.mousemove( eMouseMove )
.mousedown( eMouseDown )
.data( 'lastPos', 0 );
if ( this.addEventListener ) {
this.addEventListener( 'touchmove', eMouseMove );
this.addEventListener( 'touchstart', eMouseDown );
}
});
$( document ).bind( 'mouseup', eMouseUp );
if ( document.addEventListener ) {
document.addEventListener( 'touchend', eMouseUp );
}
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 = false; }
function eMouseDown ( e ) {
$( this ).data( 'lastPos', normalizePosition( e ));
mouseDown = true;
e.preventDefault();
}
function changeFrame ( dir ) {
var
bgPos = getBackgroundPos.call( this );
newPos = bgPos[ axis ] + ( options.spriteDim[ axis ] * dir );
if ( newPos >= options.spriteSheetDim[ axis ] ) {
newPos = 0;
} else if ( 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 ];
}
};
$.fn.threesixty.defaults = {
dragAxis: 'x',
sensitivity: 3,
spriteSheetDim: { x: 0, y: 0 },
spriteDim: { x: 0, y: 0 }
};
})( jQuery );