• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Autoplaying (and autopausing) video based on viewport positioning

Engaged ,
Apr 28, 2023 Apr 28, 2023

Copy link to clipboard

Copied

Greetings DW community!

What's the simplest 2023 method of autoplaying/autopausing a locally hosted video based on when it scrolls in or out of view?

Can this be done without javascript?

Views

293

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 28, 2023 Apr 28, 2023

If you're interested, here's a working example of autoplay on scroll.

https://codepen.io/jayshields/pen/VyEgOm

 

NOTE: Autoplay will only work with muted audio.

 

Votes

Translate

Translate
LEGEND , Apr 28, 2023 Apr 28, 2023

 


@Under S. wrote:

Greetings DW community!

What's the simplest 2023 method of autoplaying/autopausing a locally hosted video based on when it scrolls in or out of view?

Can this be done without javascript?



Modern way is using the javascript 'intersection observer' api.

 

For instance say you have a video with the class of myVideo:

 

<video muted loop class="myVideo">
<source src="video.mp4" type="video/mp4">
</video>

 

You could then use the 'intersection observer' to observer when the video comes in and out

...

Votes

Translate

Translate
Community Expert ,
Apr 28, 2023 Apr 28, 2023

Copy link to clipboard

Copied


@Under S. wrote:

Can this be done without javascript?


=========

In a word, NO.

 

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2023 Apr 28, 2023

Copy link to clipboard

Copied

If you're interested, here's a working example of autoplay on scroll.

https://codepen.io/jayshields/pen/VyEgOm

 

NOTE: Autoplay will only work with muted audio.

 

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2023 Apr 28, 2023

Copy link to clipboard

Copied

LATEST

 


@Under S. wrote:

Greetings DW community!

What's the simplest 2023 method of autoplaying/autopausing a locally hosted video based on when it scrolls in or out of view?

Can this be done without javascript?



Modern way is using the javascript 'intersection observer' api.

 

For instance say you have a video with the class of myVideo:

 

<video muted loop class="myVideo">
<source src="video.mp4" type="video/mp4">
</video>

 

You could then use the 'intersection observer' to observer when the video comes in and out of the browsers viewport by adding the block of javascript code below to your pages code directly before the closing </body> tag.

 

<script>

function playVideo(video) {
video.forEach(function(video) {
//if the video is visible play the video, else pause the video
if (video.isIntersecting) {
video.target.play();
}
else {
video.target.pause();
}
})
}

// Create the observer
let observer = new IntersectionObserver(playVideo, {
threshold: 0.3
});

// Get and observe all the items with the class of myVideo
let video = document.querySelectorAll('.myVideo');
video.forEach(function(video) {
observer.observe(video);
});

</script>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines