For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Saturday, November 30, 2024

Lazy load iframe video using IntersectionObserver

Lazy load iframe video using IntersectionObserver

 Here is the below code that will load the iframe video when it iframe will be in the viewport.
It is important that to fill the page i have used image tag with width and height.
You can use your own data.


 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Lazy load iframe video using IntersectionObserver</title>
</head>
<body>
    <br/><img width="500" height="500" src="https://imgd.aeplcdn.com/1056x594/n/cw/ec/185315/jupiter-right-side-view-16.jpeg?isig=0&q=80" alt="Sample Image">
    <br/><img width="500" height="500" src="https://imgd.aeplcdn.com/1056x594/n/cw/ec/185315/jupiter-right-side-view-16.jpeg?isig=0&q=80" alt="Sample Image">
    <br/><img width="500" height="500" src="https://imgd.aeplcdn.com/1056x594/n/cw/ec/185315/jupiter-right-side-view-16.jpeg?isig=0&q=80" alt="Sample Image">
    <br/><img width="500" height="500" src="https://imgd.aeplcdn.com/1056x594/n/cw/ec/185315/jupiter-right-side-view-16.jpeg?isig=0&q=80" alt="Sample Image">
    <br/><img width="500" height="500" src="https://imgd.aeplcdn.com/1056x594/n/cw/ec/185315/jupiter-right-side-view-16.jpeg?isig=0&q=80" alt="Sample Image">
    
  <iframe allowfullscreen="" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
  referrerpolicy="strict-origin-when-cross-origin" title="Title" url-src="PUT-YOUYUBE-VIDEO-LINK-HERE"
  id="widget2" data-gtm-yt-inspected-5="true" width="640" height="360" frameborder="0"></iframe>

  <script>
  const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const iframe = entry.target;
      //iframe.src = iframe.dataset.src; // Assume if you use data-src for youtube link
      iframe.src=iframe.getAttribute('url-src');
      observer.unobserve(iframe);
    }
  });
});
const iframes = document.querySelectorAll('iframe[url-src]');
iframes.forEach(iframe => {
  observer.observe(iframe);
});
</script>
</body>
</html>

 

 

 

 

 

 

 

Share:

Auto execute code in javascript

How to write auto execute javascript code ?


We can achieve it by 2 method.

1- Standard auto execute function.

2- Arrow function.


Standard auto execute function.


<script>
(function() {
  const iamvar1= 'Iam var 1';
  console.log('Auto Execute 1 function');
})();
</script>


Arrow function.

<script>
(()=> {
  const iamvar2= 'Iam var 2';
  console.log('Auto Execute 2 function');
})();

</script>

Share:

Passing value in Shopify snippets and use it

Passing value in Shopify snippets and use it

You are working on Shopify project with a snippet. In that snippet you want pass value as per your need. So how can you achieve it ?

Let assume that you will use a snippet "rimg-a.liquid" in other liquid file and want to use as below. You want to pass imgurl, class and fetch value.

================================================

content of any liquid file or theme.liquid file in where you want to use rimg-a.liquid snippet file

include 'rimg-a',
        imgurl: 'URl-IMAGE',
        class: 'feature-mobile',
        fetch: 'yes'

================================================

content of rimg-a.liquid file

  <img  {% if fetch != blank %} fetchpriority="high" decoding="async" {% endif %} 
    {% if imgurl != blank %}
        src="{{ imgurl | split:'?' | first | replace: '2078x2233','390x419' }}"
    {% endif %}
    {% if class != blank %}class="{{ class }}"{% endif %}
  >

 

 

 

 

 

Share:

Change video tag source url using javascript

Change video tag source url using Javascript


You want change <video> tag's <source></source> src when click on button in HTML and Javascript.
Here is the code with example



<video autoplay="autoplay" control="false" id="howto" loop="loop" muted="muted" playsinline="playsinline" poster="POSTER IMAGE URL" preload="metadata">
  <source asrc="MP4 URL" id="howtosource" type="video/mp4"></source>
  <img src="PSTER IMAGE URL" />
</video>

<a class="button" href="#" onclick="dook();">▷ Play Video Now</a>
 
<script>
  const dook = () => {
    const howto = document.getElementById("howto");
    const howtosource = document.getElementById("howtosource");
    const newSource = howtosource.getAttribute("asrc");
    howtosource.setAttribute("src", newSource); h
    owto.load();
    howto.play();
  };
</script>  
Share:

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip