How can I turn my logo into an animated preloader image?

Solved

How can I turn my logo into an animated preloader image?

usershop2024
Excursionist
19 0 1

Dear shopify,

 

I want to have a preloading image when open the pages in the website.

 

MY Theme is dawn theme.

 

Kindly share code on how to add the preload 

 

Thank you

Accepted Solution (1)

Xipirons
Shopify Partner
136 25 32

This is an accepted solution.

Hi @usershop2024 

 

To add a preloader image when opening pages on your Dawn theme Shopify website, you can follow these steps:

 

  • Create a new file named preloader.liquid in your theme's sections folder. Add the following code to the file:

 

<div class="preloader">
  <img src="{{ 'preloader.gif' | asset_url }}" alt="Preloader">
</div>

<style>
  .preloader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

<script>
  window.addEventListener('load', function() {
    var preloader = document.querySelector('.preloader');
    preloader.style.display = 'none';
  });
</script>

 

 

 

  • Add your preloader image file (e.g., preloader.gif) to your theme's assets folder.
  • Open your theme's theme.liquid file and include the preloader.liquid section just after the opening <body> tag:

 

<body>
  {% section 'preloader' %}
  ...
</body>

 

 

  • Save the changes.

Here's how it works:

  • The preloader.liquid section contains the HTML structure for the preloader, which consists of an image wrapped in a <div> element.
  • The CSS styles position the preloader to cover the entire screen and center the preloader image vertically and horizontally.
  • The JavaScript code listens for the load event of the window object. Once the page is fully loaded, it sets the display property of the preloader to none, effectively hiding it.

By including the preloader.liquid section in your theme.liquid file, the preloader will be displayed on all pages of your website until the page is fully loaded.

 

Make sure to replace 'preloader.gif' with the actual filename of your preloader image. With these steps, you should have a preloader image displayed when opening pages on your Dawn theme Shopify website.

 

I hope this helps.

 

Was this helpful? Like or Accept solution - Buy me a coffee
- Contact me: Xipirons | WhatsApp
- Shopify Developer based in France, helping online merchants succeed

View solution in original post

Replies 6 (6)

Xipirons
Shopify Partner
136 25 32

This is an accepted solution.

Hi @usershop2024 

 

To add a preloader image when opening pages on your Dawn theme Shopify website, you can follow these steps:

 

  • Create a new file named preloader.liquid in your theme's sections folder. Add the following code to the file:

 

<div class="preloader">
  <img src="{{ 'preloader.gif' | asset_url }}" alt="Preloader">
</div>

<style>
  .preloader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

<script>
  window.addEventListener('load', function() {
    var preloader = document.querySelector('.preloader');
    preloader.style.display = 'none';
  });
</script>

 

 

 

  • Add your preloader image file (e.g., preloader.gif) to your theme's assets folder.
  • Open your theme's theme.liquid file and include the preloader.liquid section just after the opening <body> tag:

 

<body>
  {% section 'preloader' %}
  ...
</body>

 

 

  • Save the changes.

Here's how it works:

  • The preloader.liquid section contains the HTML structure for the preloader, which consists of an image wrapped in a <div> element.
  • The CSS styles position the preloader to cover the entire screen and center the preloader image vertically and horizontally.
  • The JavaScript code listens for the load event of the window object. Once the page is fully loaded, it sets the display property of the preloader to none, effectively hiding it.

By including the preloader.liquid section in your theme.liquid file, the preloader will be displayed on all pages of your website until the page is fully loaded.

 

Make sure to replace 'preloader.gif' with the actual filename of your preloader image. With these steps, you should have a preloader image displayed when opening pages on your Dawn theme Shopify website.

 

I hope this helps.

 

Was this helpful? Like or Accept solution - Buy me a coffee
- Contact me: Xipirons | WhatsApp
- Shopify Developer based in France, helping online merchants succeed

usershop2024
Excursionist
19 0 1

@Xipirons 

 

Thank you very much for your response. You explained very well.

 

This the code which i added to liquid section
 
<div class="preloader">
 
    <img  id="loader" height="50px" width="50px" src="{{ 'preloader.gif' | asset_url }}" alt="Preloader">
 
</div>
 
<style>
  .preloader {
    pointer-events: none;
    cursor: default;
    position: relative;
    top: 0;
    left: 0;
   width: 100%;
height: 100vh;
    background-color: #fff;
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
  }
 
  #loader{
position: absolute;
width: 100px;
height: 100px;
z-index: 999;
}
 
</style>
 
<script>
  
  window.addEventListener('load', function() {
    var preloader = document.querySelector('.preloader');
    preloader.style.display = 'none';
  });
 
</script>
 
 
 
Now it shows before the page load and every time logo is showing.
 
However sometimes i feel logo is appearing for longer because shopify is loading very fast. 
 
Can we hide the logo faster once is content partially rendered on the screen instead of waiting for whole page or window to be displayed ?
 
kindly advise.
Thank you
Xipirons
Shopify Partner
136 25 32

Yes, you can hide the preloader once the main content is partially rendered on the screen instead of waiting for the entire page to load. Here's how you can modify the code to achieve that:

 

<div class="preloader">
<img id="loader" height="50px" width="50px" src="{{ 'preloader.gif' | asset_url }}" alt="Preloader">
</div>

<style>
.preloader {
pointer-events: none;
cursor: default;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: #fff;
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}

#loader {
position: absolute;
width: 100px;
height: 100px;
z-index: 999;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
var preloader = document.querySelector('.preloader');
preloader.style.display = 'none';
});
</script>

 

Here are the changes made:

1. Changed the position property of the .preloader class to fixed instead of relative. This ensures that the preloader covers the entire viewport regardless of the page's scroll position.

2. Changed the event listener from window.addEventListener('load', ...) to document.addEventListener('DOMContentLoaded', ...). The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. This means the preloader will be hidden as soon as the main content is ready, even if some resources are still loading.

 

With these modifications, the preloader will be displayed until the main content is loaded and parsed, and then it will be hidden, allowing the user to see the partially rendered content while the remaining resources continue to load in the background.

 

Keep in mind that the exact timing of when the preloader is hidden may vary depending on the complexity and size of your page's content. If you have a lot of content that needs to be loaded and parsed, there might still be a slight delay before the preloader is hidden.

However, this approach should generally provide a faster perceived loading experience compared to waiting for the entire page to load.

 

Was this helpful? Like or Accept solution - Buy me a coffee
- Contact me: Xipirons | WhatsApp
- Shopify Developer based in France, helping online merchants succeed

usershop2024
Excursionist
19 0 1

Thanks a lot , your solution worked very well.

LeoCameron
Visitor
1 0 0

can i show only once time when site load and show only home page not all pages when i go to another page loader showing but i dont want loader on other pages 

ehtisham13
Shopify Partner
1 0 0

make a preloader.liquid file  and add the below code, replace the image file path and

then in theme.liquid add bellow lines before closing body tag.
{% section 'preloader' %}

 

<!-- Preloader starts -->
<div class="preloader">
<img id="loader" height="100px" width="100px" src="https://cdn.shopify.com/s/files/1/0580/2881/8526/files/Loading_animation.gif?v=1726142050" alt="Preloader">
</div>
<!-- Preloader ends -->

<style>
.preloader {
pointer-events: none;
cursor: default;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: #fff;
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}

#loader {
position: absolute;
width: 100px;
height: 100px;
z-index: 999;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
var preloader = document.querySelector('.preloader');

// Increase the showing time (e.g., 3 seconds delay)
setTimeout(function() {
preloader.style.display = 'none';
}, 3000); // 3000 milliseconds = 3 seconds
});
</script>