-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallaxScroll.js
87 lines (73 loc) · 2.3 KB
/
parallaxScroll.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
// Options object must contain: containerId, backgroundImgUrl
// it may also contain an optional fadeElementId and scrollSpeed (1 to 5)
(function () {
"use strict";
if (window.jQPS !== void 0) {
return;
} else {
window.jQPS = $.fn.ParallaxScroll = function (opts) {
this.opts = opts;
this.setScrollSpeed();
this.$container = $("#" + this.opts.containerId);
this.setupContentDiv();
this.setupBackgroundDiv();
this.$container
.html(this.$backgroundDiv)
.append(this.$contentDiv);
this.setupFadeElement();
// these need to occur after all other DOM manipulations
this.height = $(document).height();
this.viewport = $(window).height();
this.$backgroundDiv.css("height", this.height + "px");
this.scroll();
};
jQPS.prototype.setScrollSpeed = function () {
if (this.opts.scrollSpeed !== void 0) {
this.scrollSpeed = this.opts.scrollSpeed;
} else {
this.scrollSpeed = 2;
}
};
jQPS.prototype.setupBackgroundDiv = function () {
this.$backgroundDiv = $("<div>")
.addClass("background")
.css({
"background": "url(" + this.opts.backgroundImgUrl + ")",
"background-size": "cover",
"background-position": "center",
"position": "fixed",
"top": "0",
"width": "100%",
"z-index": "-1"
});
};
jQPS.prototype.setupContentDiv = function () {
this.$contentDiv = $("<div>")
.addClass("content")
.html(this.$container.html());
};
jQPS.prototype.setupFadeElement = function () {
if (this.opts.fadeElementId !== void 0) {
this.$fadeElement = $("#" + this.opts.fadeElementId)
.css({
"font-size": "100%"
});
} else {
this.$fadeElement = null;
}
};
jQPS.prototype.scroll = function () {
var that = this;
$(window).scroll(function () {
var st = $(this).scrollTop();
var yDelta = -st / that.scrollSpeed;
that.$backgroundDiv.css("transform", "translateY(" + yDelta + "px)");
if (that.$fadeElement) {
var perc = st / that.viewport * 2;
var opacity = 1 - perc;
that.$fadeElement.css("opacity", opacity);
}
});
};
}
})();