How do I install a loader?

Joined
Sep 20, 2024
Messages
2
Reaction score
0
I've got a loading gif that I want to install on my website. This is the code I have so far. I tried adding in a url of the file to the tag #loader in the css and it didn't work. How do I do it?

HTML:
<span id="loader">Loading...</span>
<script>
      window.onload = () => document.querySelector("#loader").remove();
    </script>
 
Joined
Jul 4, 2023
Messages
503
Reaction score
63
Have you tried something like this?
HTML:
<div id="loader">Loading...</div>
<div id="content" style="display: none;">Content loaded!</div>

<script>
  // Call the function simulating loading after the page is loaded
  window.onload = simulateLoading;

  // Function simulating file loading (delay)
  function simulateLoading() {
    const loader = document.querySelector('#loader');
    const content = document.querySelector('#content');

    // Simulate loading for 3 seconds
    setTimeout(() => {
      loader.style.display = 'none'; // loader.remove();
      content.style.display = 'block';
    }, 3000); // 3 seconds
  }
</script>

or
HTML:
<style>
  #loader {
    display: flex;
    gap: .5rem;
  }
  #loader img {
    width: 20px;
  }
</style>

<div id="loader">
  <span>Loading</span>
  <span>
    <img src="https://icons8.com/preloaders/preloaders/1497/Spinning%20arrows.gif">
  </span>
</div>
<div id="content" style="display: none;">Content loaded!</div>

<script>
  window.onload = simulateLoading;

  function simulateLoading() {
    const loader = document.querySelector('#loader');
    const content = document.querySelector('#content');

    setTimeout(() => {
      loader.style.display = 'none'; // loader.remove();
      content.style.display = 'block';
    }, 5000);
  }
</script>

or
HTML:
<style>
  #loader {
    display: flex;
    gap: .5rem;
  }
  #loader img {
    width: 20px;
  }
  #content {
    opacity: 0;
    transition: opacity 1s;
  }
  .show {
    opacity: 1 !important;
  }
</style>

<div id="loader">
  <span>Loading</span>
  <span>
    <img src="https://icons8.com/preloaders/preloaders/1497/Spinning%20arrows.gif">
  </span>
</div>
<div id="content"></div>

<script>
  window.onload = simulateLoading;

  async function simulateLoading() {
    const loader = document.querySelector('#loader');
    const content = document.querySelector('#content');

    const response = await fetch('https://slowfil.es/file?payload=dom&delay=7000');
    const data = await response.text();

    if (data)
      content.textContent = data + ' * Lorem ipsum.'.repeat(200);

      loader.style.display = 'none'; // loader.remove();
      content.classList.add('show');
    }
</script>

or
HTML:
<style>
  #loader {
    display: flex;
    gap: .5rem;
  }
  #loader img {
    width: 20px;
  }
  #content {
    opacity: 0;
    transition: opacity 1s;
  }
  .show {
    opacity: 1 !important;
  }
  .error {
    color: red;
  }
</style>

<div id="loader">
  <span>Loading</span>
  <span>
    <img src="https://icons8.com/preloaders/preloaders/1497/Spinning%20arrows.gif">
  </span>
</div>
<div id="content"></div>

<script>
  window.onload = simulateLoading;

  async function simulateLoading() {
    const loader = document.querySelector('#loader');
    const content = document.querySelector('#content');

    // Simulation of error, file not found (status=404)
    const response = await fetch('https://slowfil.es/file?payload=dom&delay=7000&status=404');
    //const response = await fetch('https://slowfil.es/file?payload=dom&delay=7000'); // Default status 200

    if (response.ok) {
      const data = await response.text();
      if (data)
        content.textContent = data + ' * Lorem ipsum.'.repeat(200);
    } else {
      content.textContent = `HTTP error! Status: ${response.status} file not found!`;
      content.classList.add('error');
    }

    loader.style.display = 'none'; // loader.remove();
    content.classList.add('show');
  }
</script>
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,032
Latest member
OdellBerg4

Latest Threads

Top