- commit
- 274189bc8a423c478feca236412f2ed8e6bade7e
- parent
- 7fdb94678ccfe54e8a9566fd5953b3e413cea606
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-07-24 18:51
pause video on scroll
Diffstat
| A | static/video.js | 27 | +++++++++++++++++++++++++++ |
| M | templates/base.html | 1 | + |
2 files changed, 28 insertions, 0 deletions
diff --git a/static/video.js b/static/video.js
@@ -0,0 +1,27 @@
-1 1 (function() {
-1 2 var thottle = function(fn, timeout) {
-1 3 var blocked = false;
-1 4
-1 5 return function() {
-1 6 if (!blocked) {
-1 7 blocked = true;
-1 8 setTimeout(function() {
-1 9 blocked = false;
-1 10 }, timeout);
-1 11 fn();
-1 12 }
-1 13 };
-1 14 };
-1 15
-1 16 window.addEventListener('scroll', thottle(function() {
-1 17 document.querySelectorAll('video').forEach(function(video) {
-1 18 if (video.paused) {
-1 19 return;
-1 20 }
-1 21 var rect = video.getBoundingClientRect();
-1 22 if (rect.bottom < 0 || window.innerHeight - rect.top < 0) {
-1 23 video.pause();
-1 24 }
-1 25 });
-1 26 }, 100), {passive: true});
-1 27 })();
diff --git a/templates/base.html b/templates/base.html
@@ -16,5 +16,6 @@ 16 16 </form> 17 17 </header> 18 18 {% block content %}{% endblock %} -1 19 <script src="/static/video.js"></script> 19 20 </body> 20 21 </html>