-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
359 lines (260 loc) · 13.1 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>When in viewport</title>
<meta name="description" content="Handle elements as they enter viewport with this simple and lightweight javascript library.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="https://github.com/dbrekalo/whenInViewport.git/"/>
<link href='https://fonts.googleapis.com/css?family=Lato:400,300,700,900&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://rawgit.com/dbrekalo/attire/master/dist/css/build.min.css">
<script>attireQueue = [];</script>
</head>
<body>
<section class="attireBlock">
<div class="inner">
<h1 data-nav-title="About" class="attireTitleType1">When in viewport</h1>
<p class="attireTextType1">
Handle elements as they enter viewport with this simple and lightweight javascript library.
Useful for content lazy loading and applying timed animations.
Has no dependencies and weighs less than 2KB.
</p>
<nav class="attireAuthor">
<a class="imageElement" href="https://github.com/dbrekalo">
<img src="https://s.gravatar.com/avatar/32754a476fb3db1c5a1f9ad80c65d89d?s=80" alt="Damir Brekalo">
</a>
<a class="name" href="https://github.com/dbrekalo">Damir Brekalo</a>
<p class="socialBox">
<a href="mailto:[email protected]" class="iconMail" title="Contact me by email">Contact me by email</a>
<a href="https://github.com/dbrekalo" class="iconGithub" title="Find me on Github">Find me on Github</a>
<a href="https://twitter.com/damirbr" class="iconTwitter" title="Reach me on Twitter">Reach me on Twitter</a>
</p>
</nav>
<p class="attireTextType2">
Is is often useful to transform elements in some way as users scroll your webpage.
Sometimes you will do as little as apply a class to html element which just entered viewport.
Other times you will load all module resources just as it is displayed to user.
Lazy loading can help with your performance budget.
</p>
</div>
</section>
<section class="attireBlock mod1 testCase">
<div class="inner">
<h2 class="attireTitleType2">Examples</h2>
<p class="attireTextType2">
Next few dummy paragraphs should display and fade in as we scroll down the web page.
</p>
<hr class="attireSeparator mod1">
<p class="attireTextType1 fadeInParagraph">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Voluptas amet a accusamus eos numquam sint rerum, ipsum aperiam tempore, dolor, sed ex?
Corrupti obcaecati, atque ab ullam cum et nam!
</p>
<p class="attireTextType3 fadeInParagraph">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<hr class="attireSeparator mod1 fadeInParagraph">
<p class="attireTextType3 fadeInParagraph">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</p>
<style>
.fadeInParagraph {
position: relative; left: -100px; opacity: 0; transition: opacity 2s, left 2s;
}
.fadeInParagraph.inViewport {
opacity: 1; top: 0; left: 0;
}
</style>
<script>
attireQueue.push(function($) {
var throttle = function(fn, threshhold, scope) {
threshhold || (threshhold = 100);
var last, deferTimer;
return function() {
var context = scope || this,
now = +new Date();
if (last && now < last + threshhold) {
clearTimeout(deferTimer);
deferTimer = setTimeout(function() {
last = now;
fn.apply(context, arguments);
}, threshhold);
} else {
last = now;
fn.apply(context, arguments);
}
};
};
WhenInViewport.setRateLimiter(throttle, 150);
$(document).ready(function() {
$('.fadeInParagraph').whenInViewport(function($paragraph) {
$paragraph.addClass('inViewport');
});
// var elements = Array.prototype.slice.call(document.getElementsByClassName('fadeInParagraph'));
// elements.forEach(function(element) {
// new WhenInViewport(element, function(elementInViewport) {
// elementInViewport.classList.add('inViewport');
// });
// });
});
});
</script>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
WhenInViewport comes with jQuery adapter included - for this small slick animation all we had to write was following code.
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
$('.fadeInParagraph').whenInViewport(function($paragraph) {
$paragraph.addClass('inViewport');
});
</code></pre>
<p class="attireTextType2">
Same result in plain javascript (without jQuery) is achieved like so:
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
var elements = Array.prototype.slice.call(
document.getElementsByClassName('fadeInParagraph')
);
elements.forEach(function(element) {
new WhenInViewport(element, function(elementInViewport) {
elementInViewport.classList.add('inViewport');
});
});
</code></pre>
<p class="attireTextType2">
Styling was done as simple css transition that was trigged when new class was applied to element.
</p>
</div>
</section>
<section class="attireBlock">
<div class="inner">
<h2 class="attireTitleType2">Api and options</h2>
<p class="attireTextType2">
Different ways and options of running plugin are examined bellow.
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
// As in example above run plugin with callback only
$('.fadeInParagraph').whenInViewport(function($paragraph) {
$paragraph.addClass('inViewport');
});
// With adjusted options
$('.fadeInParagraph').whenInViewport(function($paragraph) {
$paragraph.addClass('inViewport');
}, {
threshold: 100 // difference in pixels from user scroll position
});
// get instance
var footerInViewport = $('footer').whenInViewport(function($footer) {
$footer.addClass('inViewport');
}).data('whenInViewport');
// get instance via constructor
var footerInViewport = new WhenInViewport($('.footer').get(0), function(footer) {
$(footer).addClass('inViewport');
});
// stop whenInViewport listener
footerInViewport.stopListening();
</code></pre>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
Plugin options / defaults are exposed in WhenInViewport.defaults namespace so you can easily adjust them globally.
List of options is bellow.
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
WhenInViewport.defaults = {
threshold: 0, // difference in pixels from user scroll position
context: null // callback context
};
</code></pre>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
If you want to set delaying / rate-limiting engine for scroll and resize events (for example delegate to underscores throttle or debounce functions)
a method for doing so is exposed:
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
WhenInViewport.setRateLimiter(_.throttle, 250);
</code></pre>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
If you want to kill all whenInViewport listeners:
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
WhenInViewport.destroy();
</code></pre>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
When page layout changes and no resize events are fired you might want to tell WhenInViewport to check all of its registered items:
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
WhenInViewport.checkAll();
</code></pre>
<hr class="attireSeparator mod1">
<p class="attireTextType2">
If you are running jQuery that is is not exposed on global window variable and want to use WhenInViewport via plugin facade:
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
var $ = require('jquery');
WhenInViewport.registerAsJqueryPlugin($);
</code></pre>
<script>
attireQueue.push(function($) {
$(document).ready(function() {
var footerInViewport1 = $('.attireFooter').whenInViewport(function($footer) {
$footer.addClass('inViewport1');
}, {
threshold: 300
}).data('whenInViewport');
var footerInViewport2 = new WhenInViewport($('.attireFooter').get(0), {
callback: function(footer) {
$(footer).addClass('inViewport2');
},
threshold: 300
});
setTimeout(function() {
WhenInViewport.checkAll();
footerInViewport1.stopListening();
footerInViewport2.stopListening();
}, 1*5000);
});
});
</script>
</div>
</section>
<section class="attireBlock">
<div class="inner">
<h2 class="attireTitleType2">Installation</h2>
<p class="attireTextType2">
WhenInViewport is packaged as UMD library so you can use it in CommonJS and AMD environment or with browser globals.
</p>
<pre class="attireCodeHighlight"><code class="language-javascript">
// install via npm
npm install when-in-viewport --save
// if you use bundler
var WhenInViewport = require('when-in-viewport');
// or just using browser globals
var WhenInViewport = window.WhenInViewport;
</code></pre>
<p class="attireTextType2">
For browser usage browse dist folder - if working with build tools go with src folder.
Download library files from <a href="https://github.com/dbrekalo/whenInViewport.git">github repo</a>,
get them via bower (bower install whenInViewport)
or via npm (npm install when-in-viewport)
</p>
</div>
</section>
<a class="githubRibbon" href="https://github.com/dbrekalo/whenInViewport.git">
<img src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png">
</a>
<section class="attireUserRepositories" data-only-with-pages="true" data-user="dbrekalo"></section>
<footer class="attireFooter">
<p>This page is built with <a href="http://dbrekalo.github.io/attire/">Attire</a>.</p>
</footer>
<script src="https://rawgit.com/dbrekalo/attire/master/dist/js/build.min.js"></script>
<script src="dist/whenInViewport.js"></script>
</body>
</html>