Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

generate a html page that loads images using api that takes in custom post request body…

  1. User

    generate a html page that loads images using api that takes in custom post request body with a property in incremental manner. Images should load lazily.

  2. Assistant

    Sure! Below is an example of an HTML page that uses JavaScript and the Fetch API to load images lazily. Each image is requested by sending a custom POST request with a property value in an incremental manner.

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>Lazy Image Loading</title>
      <style>
        .lazy-image {
          width: 200px;
          height: 200px;
          margin: 10px;
          background-color: #eee;
          display: flex;
          justify-content: center;
          align-items: center;
          font-weight: bold;
          font-size: 20px;
        }
      </style>
    </head>
    <body>
      <div id="images-container"></div>
      
      <script>
        document.addEventListener("DOMContentLoaded", () => {
          const imagesContainer = document.getElementById("images-container");
          let imageCounter = 1;
    
          // Function to lazy load an image
          const loadImage = (url) => {
            const image = new Image();
            image.src = url;
            image.classList.add("lazy-image");
            imagesContainer.appendChild(image);
          };
    
          // Function to perform a POST request to load the image
          const loadNextImage = () => {
            const payload = {
              property: imageCounter
            };
    
            fetch("https://example.com/images", {
              method: "POST",
              headers: {
                "Content-Type": "application/json"
              },
              body: JSON.stringify(payload)
            })
            .then(response => response.json())
            .then(data => {
              loadImage(data.url);
              imageCounter++;
            });
          };
    
          // Load initial images by calling loadNextImage multiple times
          for (let i = 0; i < 10; i++) {
            loadNextImage();
          }
    
          // Trigger loading of new images when scroll nears bottom
          window.addEventListener("scroll", () => {
            if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
              loadNextImage();
            }
          });
        });
      </script>
    </body>
    </html>
    

    Note: You'll need to replace "https://example.com/images" with the actual API endpoint that provides the images. The endpoint should expect a POST request with a JSON body containing the property field.