Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Infinite Scroll</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <main>
    <h1>🍦 Vanilla JS Infinite Scroll</h1>
    <p>
      Scroll down and get more images from <a href="https://picsum.photos/" target="_blank">picsum.photos</a>.
      <br>For more JS and web stuff on my personal blog: <a href="https://yossiabramov.com/" target="_blank">yossiabramov.com</a> 💻
    <br>
      Start scrolling down 👇
    </p>
    <div id="images-container" class="images-container">
      <div class="col"></div>
      <div class="col"></div>
      <div class="col"></div>
      <div class="col"></div>
    </div>
    <div id="loader" class="loader"></div>
  </main>
  <script src="script.js"></script>
</body>

</html>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap');

*{
    margin: 0;
    padding: 0;
} 

body{
    font-family: 'Lato', sans-serif;
}

a{
    color: dodgerblue;   
}

a:visited{
    color: dodgerblue;  
}

main{
    min-height: 110vh;
    max-width: 1000px;
    margin: auto;
    padding: 10px;
}

main h1{
    margin: 15px 0;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

main p{
    margin: 15px 0;
    font-size: 18px;
    letter-spacing: 0.5px;
}

main .images-container{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-column-gap: 10px;
}

main .images-container .image-wrapper{
    margin-bottom: 10px;
    box-shadow: 0 5px 8px rgba(0,0,0,0.6);
    border-radius: 5px;
    overflow: hidden;
}

main .images-container .image-wrapper img{
    width: 100%;
    max-height: 250px;
    object-fit: cover;
}

main .images-container .image-wrapper.loading{
  position: relative;
}

main .images-container .image-wrapper.loading::after{
  content: '';
  position: absolute;
  background: #fefefe;
  display: block;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 4;
  height: 100%;
  width: 100%:
}

main .loader{
    text-align: center;
    visibility: hidden;
    opacity: 0;
    transition: all 0.1s ease-in;
    border: 2px solid dodgerblue;
    border-radius: 50%;
    width: 25px;
    height: 25px;
    margin: auto;
    margin-top: 10px;
    animation: loader 0.5s infinite alternate;
}

main .loader.show{
    visibility: visible;
    opacity: 1;
}

@keyframes loader {
    from{
        transform: scale(1);
    }
    to{
        transform: scale(0.8);
    }
}

@media(max-width: 700px){
    main .images-container{
        grid-template-columns: repeat(2, 1fr);
    }
}
              
            
!

JS

              
                // Globals
let isFetching = false;
let currentPage = 1;
let currentCol = 1;

const gridColumnCount = 4;
const imgContainer = document.getElementById("images-container");
const loader = document.getElementById("loader");

// Functions
const fetchImages = async () => {
  loader.classList.add("show");
  isFetching = true;
  const response = await fetch(
    `https://picsum.photos/v2/list?limit=12&page=${currentPage}`
  );
  const images = await response.json();
  updateDom(images);
  currentPage++;
  isFetching = false;
  loader.classList.remove("show");
};

const genRandomInt = (min, max) => {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min);
};

const updateDom = (images) => {
  images.forEach((img) => {
    const imageContainer = document.createElement("div");
    imageContainer.classList.add("image-wrapper");
    imageContainer.classList.add("loading");

    imageContainer.innerHTML = `
        <img 
            alt="a random image by picsum.photos" 
            src="${img.download_url}"
            style="max-height:${genRandomInt(
              200,
              350
            )}px;height: 100%;display: block;"
            class="loading"
        />`;

    if (currentCol === 5) {
      currentCol = 1;
    }

    imgContainer
      .querySelector(`.col:nth-child(${currentCol})`)
      .appendChild(imageContainer);

    currentCol++;
    
    setTimeout(() => {
      imageContainer.classList.remove('loading');
    }, 500);
  });
};

// // Event Listeners & triggers

// document.addEventListener("DOMContentLoaded", async () => {
//   await fetchImages();
// });

window.addEventListener("scroll", async () => {
  // Do not run if currently fetching
  if (isFetching) return;

  // Scrolled to bottom
  if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
    await fetchImages();
  }
});

              
            
!
999px

Console