This repository has been archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
divPeek.js
47 lines (41 loc) · 1.85 KB
/
divPeek.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
//DivPeek - Custom jQuery CSS3 Animation Trigger Script by David Halford
//(see https://github.com/davidhalford/DivPeek)
//===============================================================================
//CONFIG:
var elementsToTrack = ["#scrollfx1","#scrollfx2","#scrollfx3","#scrollfx4","#scrollfx5","#scrollfx6","#scrollfx7"];
var pixelOffset = -24;
var inClassName = "inViewPort";
var outClassName = "outViewPort";
//===============================================================================
//add function to see of elements exist (otherwise removing an elemnet from DOM but not from array breaks everything)
jQuery.fn.exists = function(){return this.length>0;}
//define vars out of scope
var viewPortHeight = $(window).height();
var scrollFromTop = $(window).scrollTop();
var scrollFromBottom = (parseInt(scrollFromTop)+parseInt(viewPortHeight));
//function for recalculating all positioning vars in scroll/resize
function recalcVars(){
viewPortHeight = $(window).height();
scrollFromTop = $(window).scrollTop();
scrollFromBottom = (parseInt(scrollFromTop)+parseInt(viewPortHeight));
for (var i = 0; i < elementsToTrack.length; i++) {
if ($(elementsToTrack[i]).exists()) {
checkInViewport(scrollFromBottom, elementsToTrack[i]);
}
}
}
//catch window events
$(window).resize(function(e){recalcVars();});
document.addEventListener("touchmove", ScrollStart, false);
document.addEventListener("scroll", Scroll, false);
function ScrollStart(){recalcVars();}
function Scroll(){recalcVars();}
//function that handles if an element is in the viewport or not
function checkInViewport(scrollBottom, domElement){
var elementPos = $(domElement).offset().top;
if((parseInt(scrollBottom)+parseInt(pixelOffset)) > elementPos){
$(domElement).addClass(inClassName).removeClass(outClassName);
} else {
$(domElement).removeClass(inClassName).addClass(outClassName);
}
}