Home / Attachments / Lazy Load
Duplicate Snippet

Embed Snippet on Your Site

Lazy Load

Code Preview
php
<?php
add_action('wp_head', 'custom_lazyload_script');
function custom_lazyload_script(){
  ?>
  <script>
    window.onload = function() {
      document.querySelectorAll("img[data-src]").forEach(function(image) {
        image.setAttribute('src', image.getAttribute('data-src'));
        image.onload = function() {image.removeAttribute('data-src');};
      });
    };
  </script>
  <?php
}

Comments

Add a Comment