-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.html
More file actions
124 lines (109 loc) · 3.68 KB
/
example.html
File metadata and controls
124 lines (109 loc) · 3.68 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
<style>
/* TEST ELEMENT CSS */
#dynamic_content_1{
width: 100px;
height: 100px;
background-color: #555;
}
#dynamic_content_2{
width: 100px;
height: 100px;
background-color: red;
}
/*-------------------------------------------------
FUNCTIONAL JS
creates an animation which is triggered when an
item loads.
-------------------------------------------------*/
/* Used to detect when an element has loaded */
@keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
@-moz-keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
@-webkit-keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
@-ms-keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
@-o-keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
</style>
<div id="container"></div>
<script>
/*------------------------
RUN CODE WHEN ELEMENT EXISTS
used for dynamically added content
Based on the work by David Walsh and Daniel Buchner https://davidwalsh.name/detect-node-insertion
------------------------*/
var On_Element_Ready = function(id, callback){
if ( document.getElementById(id) ){
callback(); //if element is already loaded, don't set a listener
}else{
//use existing stylesheet or create a new one if one doesn't exist already
var style = document.getElementById("dynamic-node-insert-detection-css") ||
(function(){
var style = document.createElement("style");
style.id = "dynamic-node-insert-detection-css";
style.setAttribute( "media", "screen" );
style.setAttribute("data-dynamic-origin-comment", "Item dynamically added with javascript");
style.appendChild( document.createTextNode("") );
document.head.appendChild(style);
return style;
})();
//add all prefixes to the animation rules
var Prefix = function(str){
var returnStr = "";
var prefixList = ["", "-o-", "-ms-", "-webkit-"];
for ( x in prefixList ){
returnStr += prefixList[x] + str;
}
return returnStr
};
//add our new rules to this stylesheet
style.sheet.insertRule("#" + id + "{"+
Prefix("animation-duration: 0.001s;")+
Prefix("animation-name: nodeInserted;")+
"}", 0
);
//set up the callback for when the animation runs
insertListener = function(event){
if (event.animationName == "nodeInserted") {
if (event.target.id == id){
callback();
}
}
}
//wait for 'nodeInserted' animation to run
document.addEventListener("animationstart", insertListener, false); // standard + firefox
document.addEventListener("MSAnimationStart", insertListener, false); // IE
document.addEventListener("webkitAnimationStart", insertListener, false); // Chrome + Safari
}
}
/*------------------------
TEST SCRIPTS
------------------------*/
//set up 'On_Element_Ready' calls
On_Element_Ready("dynamic_content_1", function(){console.log("dynamic_content_1 loaded")});
On_Element_Ready("dynamic_content_2", function(){console.log("dynamic_content_2 loaded")});
//Make those items 1s and 2s after the page has loaded
var container = document.getElementById("container");
window.setTimeout(function(){
var dynamic_content_1 = document.createElement("div");
dynamic_content_1.id = "dynamic_content_1";
container.appendChild( dynamic_content_1 );
}, 1000);
window.setTimeout(function(){
var dynamic_content_2 = document.createElement("div");
dynamic_content_2.id = "dynamic_content_2";
container.appendChild( dynamic_content_2 );
}, 2000);
</script>