-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.imageparallax.js
140 lines (109 loc) · 4.21 KB
/
jquery.imageparallax.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
(function($)
{
// This script was written by Steve Fenton
// http://www.stevefenton.co.uk/Content/Jquery-Image-Parallax/
// Feel free to use this jQuery Plugin
// Version: 3.0.2
// Contributions by:
var parallaxCount = 0;
var imageWidths = new Array();
var imageHeights = new Array();
var parallaxImages = new Array();
function GetSize(number, factor, intensity) {
if (factor > 0) {
var increment = parseFloat("1." + (factor * intensity), 10);
number = parseInt(number * increment);
}
return number;
}
$.fn.imageparallax = function (settings) {
var config = {
classmodifier: "ip",
intensity: 1,
images: "",
numberoflayers: 5,
allowHorizontal: true,
allowVertical: true
};
if (settings) {
$.extend(config, settings);
}
return this.each(function () {
$This = $(this);
// Ensure intensity remains sensible
var intensity = parseFloat(config.intensity);
if (intensity < 1) {
intensity = 1;
} else if (intensity > 2) {
intensity = 2;
}
var imageArray = new Array();
var w = $This.width();
var h = $This.height();
$This.replaceWith("<div id=\"" + config.classmodifier + "_" + parallaxCount + "\" rel=\"" + parallaxCount + "\"></div>");
$("#" + config.classmodifier + "_" + parallaxCount).css({ width: w+"px", height: h+"px", overflow: "hidden", position: "relative", cursor: "crosshair" });
// Set up images
if (config.images.length > 0) {
// Use supplied images
for (var i = 0; i < config.images.length; i++) {
imageArray[i] = config.images[i];
}
} else {
// Create duplicates and make them different sizes
imageSource = $(this).attr("src");
for (var i = 0; i < config.numberoflayers; i++) {
imageArray[i] = imageSource;
}
}
// Iterate over images and append them to the parallax set
for (var i = 0; i < imageArray.length; i++) {
var imageWidth = GetSize(w, i, config.intensity);
var imageHeight = GetSize(h, i, config.intensity);
var imageTop = parseInt((imageHeight - h) / 2, 10) * -1;
var imageLeft = parseInt((imageWidth - w) / 2, 10) * -1;
$("#" + config.classmodifier + "_" + parallaxCount).append("<img id=\"" + config.classmodifier + "_" + parallaxCount + "_" + i + "\" src=\"" + imageArray[i] + "\" width=\"" + imageWidth + "\" height=\"" + imageHeight + "\" style=\"position: absolute; top: " + imageTop + "px; left: " + imageLeft + "px;\">");
}
parallaxImages[parallaxCount] = imageArray;
imageWidths[parallaxCount] = w;
imageHeights[parallaxCount] = h;
// When the mouse moves over the parallax, follow its movement
$("#" + config.classmodifier + "_" + parallaxCount).mousemove( function (e) {
var offset = $(this).offset();
// Mouse co-ordinates
var x = parseInt(e.pageX - offset.left, 10);
var y = parseInt(e.pageY - offset.top, 10);
// Current element rel (tells us which collection to use)
var thisElement = $(this).attr("rel");
var imageArray = parallaxImages[thisElement];
// Original image dimensions
var originalWidth = imageWidths[thisElement];
var originalHeight = imageHeights[thisElement];
// Gets the center position
var w = parseInt(originalWidth / 2, 10);
var h = parseInt(originalHeight / 2, 10);
// Treat the mouse position relative to the middle of the image
x = (x - w);
y = (y - h);
// Move each layer
for (var i = 1; i < imageArray.length; i++) {
var $Img = $("#" + config.classmodifier + "_" + thisElement + "_" + i);
var myWidth = $Img.width();
var myHeight = $Img.height();
// Gets offset position based on the amount this is bigger than the original image
var imageLeft = ((myWidth - originalWidth) / 2) * -1;
var imageTop = ((myHeight - originalHeight) / 2) * -1;
// Calculate movement, arbitrarily divided by 12 to reduce the speed
var myX = parseInt(imageLeft + ((x * i) / 10), 10);
var myY = parseInt(imageTop + ((y * i) / 10), 10);
if (config.allowHorizontal) {
$Img.css({ left: myX + "px" });
}
if (config.allowVertical) {
$Img.css({ top: myY + "px" });
}
}
});
parallaxCount++;
});
};
})(jQuery);