-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSwapper.js
More file actions
130 lines (118 loc) · 3.35 KB
/
imageSwapper.js
File metadata and controls
130 lines (118 loc) · 3.35 KB
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
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
addLoadEvent(bgSlide);
multiplier = 55; timeout = 8; timeout2 = 3000; count = 1;
function bgSlide(containerDiv,e,x,xi,io)
{
var xc = e.style.backgroundPosition;
xc = parseInt(xc.substring(0,xc.indexOf('p')));
targetX = parseInt(xi) + parseInt(x);
if(io == "o")
{
// we are easing out
if(x < 0)
{
// we are moving negatively along the x-axis
var deltaX = xi - xc;
var percentMoved = (-1)*(deltaX/x);
var posX = xc - Math.ceil((1.0-percentMoved) * multiplier);
if(posX < targetX) { posX = targetX; }
}
else if(x == 0)
{
var posX = xc;
}
else
{
var deltaX = xc - xi;
var percentMoved = (deltaX/x);
var posX = xc + Math.ceil((1.0-percentMoved) * multiplier);
if(posX > targetX) { posX = targetX; }
}
}
else
{
// we are easing in
if(x < 0)
{
// we are moving negatively along the x-axis
var deltaX = xi - xc;
var percentMoved = (-1)*(deltaX/x);
var posX = xc - Math.ceil((percentMoved + .01) * multiplier);
if(posX < targetX) { posX = targetX; }
}
else if(x==0)
{
var posX = xc;
}
else
{
var deltaX = xc - xi;
var percentMoved = (deltaX/x);
var posX = xc + Math.ceil((percentMoved + .01) * multiplier);
if(posX > targetX) { posX = targetX; }
}
}
e.style.backgroundPosition = posX+"px";
e.style.opacity = percentMoved;
if(posX == targetX)
{
e.style.opacity='1.0';
var swapperDiv = document.getElementById(containerDiv);
if(count == 0) var lastCount = imagesArray.length-1; else var lastCount = count-1;
swapperDiv.style.backgroundImage="url('"+imagesArray[lastCount]+"')";
swapperDiv.style.backgroundPosition="0px";
if(!document.getElementById('theSwapper'))
{
var newDiv = document.createElement('div');
newDiv.className="swapper";
newDiv.id="theSwapper";
newDiv.style.position="absolute";
e.appendChild(newDiv);
}
else
{
var newDiv = document.getElementById('theSwapper');
}
var newImage = new Image();
newImage.src = imagesArray[count];
newDiv.style.backgroundImage="url('"+imagesArray[count]+"')";
newDiv.style.backgroundPosition=xi+"px";
newDiv.style.backgroundRepeat="no-repeat";
if(count < imagesArray.length-1) { ++count; } else { count = 0; }
timer = setTimeout(function() { bgSlide(newDiv,x,xi,io); }, timeout2);
}
else
{
timer = setTimeout(function() { bgSlide(e,x,xi,io); }, timeout);
}
}
function imageSwapper(width)
{
var allDivs = document.getElementsByTagName('div');
for(var i=0; i < allDivs.length; ++i)
{
var divElement = allDivs[i];
if(divElement.getAttribute('class') == "swapper")
{
var swapperDiv = divElement;
}
}
swapperDiv.style.backgroundRepeat="no-repeat";
// pre-load first image to get things tarted
var newImage = new Image();
newImage.src = imagesArray[0];
// no assign it to the far right as the background image
swapperDiv.style.backgroundImage="url('"+imagesArray[0]+"')";
swapperDiv.style.backgroundPosition=width+"px";
bgSlide("featSliderShortcode",swapperDiv,(-1*width),width,"o");
}