Have you been able to find a solution in Webflow that stops embedded YouTube videos from playing when transitioning between slides?

Published on
September 22, 2023

Yes, there is a solution in Webflow to stop embedded YouTube videos from playing when transitioning between slides. You can achieve this by using custom code and adding an event listener to detect when a slide transition occurs. Here's how you can do it:

  1. Add a custom code embed to your Webflow project:
  • In the Webflow Designer, navigate to the page where you have the slideshow with embedded YouTube videos.

  • Click on the settings gear icon in the top right corner of the Designer.

  • Select "Custom Code" from the dropdown menu.

  • In the Header Code section, add the following code:

      \`\`\`html  <script>  var videos = [];  function stopVideos() {    for (var i = 0; i < videos.length; i++) {      videos[i].contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '", "args":""}', '\*');    }  }  window.addEventListener('message', function(e) {    var data = JSON.parse(e.data);    if (data.event === 'ready') {      videos.push(e.source);    }  });  </script>  \`\`\`
  1. Add unique IDs to your YouTube video elements:
  • In the Webflow Designer, select each YouTube video element in your slides.
  • In the Settings panel on the right, give each video element a unique ID. For example, you can use "youtube-video-1", "youtube-video-2", and so on.
  1. Add a slide change event listener:
  • In the Webflow Designer, navigate to the page with the slideshow.

  • Select the slideshow element.

  • In the Interactions panel, click on the "+" button beside the "Mouse Interactions" section to add a new interaction.

  • Select the "While page is scrolling" trigger.

  • Click on the "+" button beside "While page is scrolling" to add a new action.

  • Select "Run JavaScript" as the action type.

  • In the JavaScript code box, add the following code:

      \`\`\`javascript  stopVideos();  \`\`\`

    This code will run the stopVideos() function every time a slide transition occurs, as you scroll through the page.

  1. Publish your site:
  • Once you have added the custom code and configured the interaction, you can publish your site to see the changes in action.

By following these steps, you will be able to stop embedded YouTube videos from playing when transitioning between slides in Webflow. This solution utilizes custom code and event listeners to detect and pause the videos when necessary.

Additional Questions:

  1. How can I add a custom code embed in Webflow?
  2. Can I stop autoplay on embedded videos in Webflow?
  3. Are there any other ways to enhance the interaction between embedded videos and slides in Webflow?