Coder Social home page Coder Social logo

Viewer throttling about haruneko HOT 3 CLOSED

manga-download avatar manga-download commented on July 23, 2024
Viewer throttling

from haruneko.

Comments (3)

Pierre-Monier avatar Pierre-Monier commented on July 23, 2024

I can't see the link, I got a 403 error in the console. I have a non-premium account on CodePen, not sure if it's related

from haruneko.

Sheepux avatar Sheepux commented on July 23, 2024

I don't have any account, it's open to everyone.

here is the codepen:

<div class="container">
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
  
  <img src="http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png" alt="" data-src="https://instagram.ftpe8-4.fna.fbcdn.net/vp/858878e86ee732def268b2b33fadb5bc/5B7554EE/t51.2885-15/e35/30078878_134250340653099_1383951987346767872_n.jpg" />
</div>
body {
  padding: 0;
  padding-bottom: 30px;
  margin: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.container {
  width: 50%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

img {
  margin-top: 30px;
  display: block;
  height: 500px;  
}
(function() {
  
  function throttle(fn, time) {
    let start = new Date();
    return function() {
      let context = this;
      let args = arguments;
      let current = new Date();
      // reach to a time, trigger the function
      if (current - start >= time) {
        fn.apply(context, args);
        start = current;
      }
    }
  }
  
  function lazyLoad() {
    const loadingImage = 'http://cumbrianrun.co.uk/wp-content/uploads/2014/02/default-placeholder-300x300.png';
    const images = document.getElementsByTagName('img');
    let count = 0;
    return function() {
      const clientHeight = document.documentElement.clientHeight;
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            
      for (let i = count; i < images.length; i++) {
        if (images[i].offsetTop < clientHeight + scrollTop) {
          if (images[i].getAttribute('src') === loadingImage) {
            images[i].src = images[i].getAttribute('data-src');
          }
          count = count + 1;
        }
      }
    }
  }
  

  const loadImages = lazyLoad();
  // load first image
  loadImages();
	window.addEventListener('scroll', throttle(loadImages, 1000), false);
})();

from haruneko.

ronny1982 avatar ronny1982 commented on July 23, 2024

Provided with commit 7ac892d

from haruneko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.