-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathjquery.threesixty.js
173 lines (147 loc) · 5.88 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*!
* jQuery.threesixty Image rotation plug-in for jQuery version 0.6
* http://www.mathieusavard.info/threesixty/
*
* Copyright 2008-2010 Mathieu Dumais-Savard
* Licensed under the MIT lice
* http://www.opensource.org/licenses/mit-license.php
* *
* Date: Tue Aug 9
*/
jQuery.fn.threesixty = function(options){
options = options || {};
options.images = options.images || [];
options.method = options.method || "click" //can be click, mouse move or auto
options.cycle = options.cycle || 1;
options.resetMargin = options.resetMargin || 0;
options.direction = options.direction || "forward";
options.sensibility = options.sensibility || options.cycle * 0.35;
options.autoscrollspeed = options.autoscrollspeed || 500;
if (options.direction == "backward")
options.images.reverse();
return this.each(function(){
var imgArr = [];
var pic = $(this);
$(function() {
var cache = [];
var parent = $("<div>");
parent.css({height:pic.height(), width:pic.width(), overflow:"hidden", position:"relative"});
pic.wrap(parent).css({position:"relative",top:0,left:0});
parent = pic.parent();
//Binding the progress bar
var progressBg = $("<div></div>").css({width:parent.width()-200, height:10, backgroundColor:"black", position:"absolute","bottom":60,left:100 }).addClass("progressBg");
var progressBar = $("<div></div>").css({width:0, height:10, backgroundColor:"white", position:"absolute","bottom":60,left:100 }).data("progress",0).addClass("progressBar");
var overlay;
try {
overlay = $("<div></div>").css({cursor:"wait", width:pic.width(), background:"RGBA(0,0,0,0.7)", height:pic.height(), position:"absolute","top":0,left:0 }).addClass("overlay");
}
catch (e)
{
overlay = $("<div></div>").css({cursor:"wait", width:pic.width(), height:pic.height(), backgroundColor:"black", filter:"alpha(opacity=70)", position:"absolute","top":0,left:0 }).addClass("overlay");
}
//Nasty overlay capturing all the events :P
overlay.click(function(e) { e.preventDefault(); e.stopPropagation(); });
overlay.mousedown(function(e) { e.preventDefault(); e.stopPropagation(); });
parent.append(overlay).append(progressBg).append(progressBar);
pic.css({cursor:"all-scroll"});
var totalProgress = 0;
var loaded=false;
//ask browser to load all images.. I know this could be better but is just a POC
$.each(options.images, function(index, record) {
var o =$("<img>").attr("src",record).load(function() {
if (index>pic.data("tempIndex"))
{
pic.data("tempIndex", index)
pic.attr("src", $(this).attr("src"))
}
var progress = pic.parent().find(".progressBar");
totalProgress++;
var maxsize = pic.parent().find(".progressBg").width();
var newWidth = (totalProgress/options.images.length)*maxsize;
progress.stop(true,true).animate({width:newWidth},250);
if (totalProgress == options.images.length-1)
{ loaded=true;
pic.parent().find(".overlay, .progressBar, .progressBg").remove();
}
});
cache.push(o);
});
})
for (var x=1; x<=options.cycle; x++)
for (var y=0; y<options.images.length; y++)
imgArr.push(options.images[y]);
pic.data("currentIndex",0).data("tempIndex",0);
pic.data("scaled",false);
pic.data("touchCount",0);
var originalHeight = pic.height();
var originalWidth = pic.width();
function determineIndex(e) //e represent the event for newIndex
{
return Math.floor((e.pageX - pic.offset().left) / (pic.width()/imgArr.length))
}
function moveInViewport(e) //e represents the finger in question
{ $("#debug").text("left:" + e.pageX);
var newTop = pic.data("refLocY") - pic.data("refTouchY") + e.pageY;
var newLeft = pic.data("refLocX") - pic.data("refTouchX") + e.pageX;
if (newLeft>0) newLeft=0;
if (pic.parent().width() + Math.abs(newLeft) > pic.width())
newLeft = -1*pic.width()+pic.parent().width();
if (newTop>0) newTop=0;
if (pic.parent().height() + Math.abs(newTop) > pic.height())
newTop = -1*pic.height()+pic.parent().height();
pic.css({left:newLeft, top:newTop});
}
pic.mousemove(function(evt) {
if (!!pic.data("refTouchX") === false)
{
pic.data("refTouchX",evt.pageX);
pic.data("refTouchY",evt.pageY);
pic.data("refLocX",parseInt(pic.css("left")));
pic.data("refLocY",parseInt(pic.css("top")));
}
evt.preventDefault();
if (pic.data("enabled")=="1" || options.method == "mousemove")
{
if (evt.preventDefault) evt.preventDefault();
var e = evt;
if (pic.data("scaled") == false)
{
var distance = e.pageX - pic.data("refTouchX"); //distance hold the distance traveled with the finger so far..
stripeSize = Math.floor(originalWidth / imgArr.length);
var newIndex = pic.data("currentIndex") + Math.floor(distance*options.sensibility/stripeSize)
if (newIndex < 0)
{
newIndex = imgArr.length-1;
pic.data("currentIndex",newIndex);
}
newIndex = newIndex % imgArr.length;
if (newIndex == pic.data("currentIndex"))
return;
pic.attr("src",imgArr[newIndex]);
pic.data("tempIndex",newIndex);
}
else { //The image needs to be moved in its viewport..
moveInViewport(e);
}
return;
}
})
if (options.method == "click")
{ //Certain binding will be done if and only if the method is "click" instead of "mousemove"
pic.mousedown(function(e) {
e.preventDefault();
pic.data("enabled","1");
});
$("body").mouseup(function(e) {
e.preventDefault();
pic.data("enabled","0");
pic.data("currentIndex",pic.data("tempIndex"));
});
}
if (options.method == "auto") {
var speed = options.autoscrollspeed;
var newIndex=0;
window.setInterval(function() { pic.attr("src", imgArr[++newIndex % imgArr.length])} , speed);
}
});
};