-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.joffset.js
156 lines (129 loc) · 6.39 KB
/
jquery.joffset.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
jOffset
-------
Version 1.1
Copyright (C) 2013 Milosz Falinski
Twitter: @miloszfalinski
Web: http://milosz.is
Github page: https://github.com/miloszfalinski/jOffset
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($) {
$.fn.extend({
jOffset: function (options) {
var defaults = {
offsetX: '15',
offsetY: '15',
snapToCenter: true,
triggerEvent: 'mousemove',
framerate: 60,
diag: false
};
// if user defined options, use them instead
options = $.extend(defaults, options);
// fire the functon for all matched elements
return this.each(function () {
var o = options;
var $this = $(this);
// define vars that don't need to be recalculated on frame
var eltWidth = $this.width();
var eltHeight = $this.height();
// find center of each panel - will only work for square things.
var centerX = (eltWidth/2);
var centerY = (eltHeight/2);
if (o.diag) {
console.log('eltWidth, centerX, centerY: ',
eltWidth, centerX, centerY);
}
// select direct descendant only, skips images in overlay
// center and set image size to 100% + 2*offset
var image = $this.find('> img');
image.width((100 + 2*o.offsetX + '%'))
.height((100 + 2*o.offsetY + '%'))
.css({
// (image width - frame width) / 2
'left': '-' + (image.width() - eltWidth) / 2 + 'px',
'top': '-' + (image.height() - eltHeight) / 2 + 'px'
});
if (o.snapToCenter) {
image.css({
'transition-property': 'left, top',
'transition-duration': '0.25s'
});
}
if (o.diag) {
console.log('Image - left, top: ',
image.css('left'), image.css('top'));
}
// set enableHandler to true every frame
var timeout = 1000/o.framerate;
var enableHandler;
timer = window.setInterval(function(){
enableHandler = true;
}, timeout);
// define main functions
function offsetHandler (event) {
// relative mouse position from top left corner of each panel
var mouseX = event.pageX - $this.offset().left;
var mouseY = event.pageY - $this.offset().top;
if (o.diag) {
console.log('mouseX, mouseY: ', mouseX, mouseY);
}
// absolute mouse distance from center -> between 0 and 1
var mouseDistX = (mouseX / centerX) * 100 - 100;
var mouseDistY = (mouseY / centerY) * 100 - 100;
if (o.diag) {
console.log('mouseDistX, mouseDistY: ', mouseX, mouseY);
}
// after all's defined do the actual position.
image.css({
// -(absolute_distance_from_center/ (100/offset -
// brings it down to 1:1 scale with positions))
// finally account for initial offset and add a unit.
'left': -(mouseDistX/(100/o.offsetX)) - o.offsetX + "%",
'top': -(mouseDistY/(100/o.offsetY)) - o.offsetY + "%"
});
}
function snapHandlerIn (argument) {
if (o.diag) {
console.log('mouseenter fired.');
}
setTimeout(function() {
image.css({
'transition-property': 'none',
'transition-duration': '0'
});
}, 200);
}
function snapHandlerOut (event) {
if (o.diag) {
console.log('mouseleave fired.');
}
//animate left & top css to right position
image.animate({
left: '-' + (image.width() - eltWidth) / 2 + 'px',
top: '-' + (image.height() - eltHeight) / 2 + 'px'
}, 200, 'swing').css({
'transition-property': 'left, top',
'transition-duration': '0.25s'
});
}
// set events to fire when necessary
$this.on(o.triggerEvent, function (event) {
// only fires if enableHandler is true, that is every frame
if (enableHandler) {
offsetHandler(event);
enableHandler = false;
}
});
// fires snapToCenter function, if enabled
if (o.snapToCenter) {
$this.on('mouseenter', snapHandlerIn);
$this.on('mouseleave', snapHandlerOut);
}
});
}
});
})(jQuery);