-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
147 lines (117 loc) · 3.79 KB
/
index.html
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
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang='en'>
<!-- https://www.w3schools.com/js/js_graphics_chartjs.asp
https://www.w3schools.com/js/js_graphics_google_chart.asp
-->
<head>
<title>Charts Test</title>
<meta charset='utf-8'>
<meta name='description' content='chart js library test Graphics'>
<meta name='keywords' content=''>
<meta name='author' content='Odisseo'>
<meta name='robots' content='all'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#8B0000">
<link rel="preconnect" href="https://cdnjs.cloudflare.com">
<style>
body{
color:#000;
background-color:#fff;text-align:center;
}
canvas, div, .lastdiv{
width:100%;max-width:1200px;margin:30px auto 0;
height:auto;
}
h1{
margin:150px auto;
width:100%;
max-width:600px;
}
.chartjs-size-monitor, .chartjs-size-monitor-expand, .chartjs-size-monitor-shrink {
display: none;
}
</style>
</head>
<body>
<h1>JAVASCRIPT LAZY LOAD DINAMICALLY EVENT SCROLL</h1>
<p>CHART 1</p>
<canvas id="myChart" ></canvas>
<p>CHART 2</p>
<canvas id="myChart2" ></canvas>
<p>CHART 3</p>
<canvas id="myChart3" ></canvas>
<p>CHART 4</p>
<canvas id="myChart4" ></canvas>
<p>CHART 5</p>
<canvas id="myChart5" ></canvas>
<p>CHART 6</p>
<canvas id="myChart6" ></canvas>
<p>CHART 7</p>
<canvas id="myChart7" ></canvas>
<p>CHART 8</p>
<canvas id="myChart8" ></canvas>
<script>
function isInViewport(el) {
const rect = el.getBoundingClientRect();
const height = el.offsetHeight - 50;
return (rect.bottom <= (window.innerHeight + height || document.documentElement.clientHeight + height ) );
}
function d(i){
/* first script*/
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src = 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js';
/* second script*/
var secondscript = document.createElement('script');
secondscript.type = 'text/javascript';
secondscript.async = true;
secondscript.src = './js/myscript.js';
/* callback js second script after the first(newscript) */
const p = document.getElementsByTagName('body')[0];
newscript.onload = () => {
p.appendChild(secondscript)
}
document.addEventListener('scroll', function () {
const q = document.getElementById(i);
const r = document.getElementsByTagName('head')[0];
if (isInViewport(q)){
r.appendChild(newscript)}
else if (r.appendChild(newscript)&& !isInViewport(q)){
r.removeChild(newscript)
}else{
return false
}
},{passive: true, capture: true});
}
d("myChart");
</script>
<!-- <script>
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src = 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js';
/* second script*/
var secondscript = document.createElement('script');
secondscript.type = 'text/javascript';
secondscript.async = true;
secondscript.src = './js/myscript.js';
window.onload = () => {
const myTimeout = setTimeout(function () {
(document.getElementsByTagName('body')[0]).appendChild(newscript);
clearTimeout(myTimeout);
}, 3);
}
newscript.onload = () => {
(document.getElementsByTagName('body')[0]).appendChild(secondscript)
}
/*window.addEventListener('load', function () {
const myTimeout = setTimeout(function () {
(document.getElementsByTagName('body')[0]).appendChild(newscript);
clearTimeout(myTimeout);
}, 10);
},{passive: true, capture: true}); */
</script> -->
</body>
</html>