Can the progress of a background video in Webflow be controlled based on the x and y position of the mouse?

Published on
September 22, 2023

Yes, the progress of a background video in Webflow can be controlled based on the x and y position of the mouse. This can be achieved using custom code and the interaction feature in Webflow.

Here's how you can do it:

  1. Add a background video element to your Webflow project by dragging and dropping a "Video" component onto the desired section or container.

  2. With the video selected, go to the "Settings" panel on the right-hand side of the Webflow Designer.

  3. Upload your desired video file and set the video settings such as loop, autoplay, and controls as needed.

  4. Next, we'll need to add custom code to control the video's progress based on the mouse position. Click on the globe icon in the top-right corner of the Webflow Designer to access the "Custom Code" section.

  5. In the "Head Code" section, paste the following JavaScript code:

<script>  $(document).ready(function() {    var video = document.querySelector("#YOUR_VIDEO_ID"); // Replace with your video element's ID    document.addEventListener("mousemove", function(event) {      var xPos = event.clientX / window.innerWidth;      var yPos = event.clientY / window.innerHeight;      video.currentTime = video.duration * xPos * yPos; // Adjust this formula as needed    });  });</script>
  1. Replace "YOUR_VIDEO_ID" in the code snippet with the actual ID of your video element. You can find the ID by selecting the video element in the Webflow Designer and checking the "Element Settings" panel on the right-hand side.

  2. Save the custom code and exit the "Custom Code" section.

  3. Now, let's create an interaction to trigger the video playback. Select the video element and go to the "Interactions" panel on the right-hand side.

  4. Add a new interaction and choose the trigger that suits your needs (e.g., "On Page Load" or "On Scroll Into View").

  5. In the interaction, choose the action "Start Animation" and select the desired video animation to play when the trigger is activated.

  6. Save the interaction and publish your Webflow project.

With this setup, as the user moves their mouse across the page, the video's progress will change based on the x and y position of the mouse. You can adjust the formula in the custom code to control the speed and direction of the video based on the mouse position.

Note that custom code is required to achieve this effect, and it's recommended to have a basic understanding of JavaScript. Additionally, the video element in Webflow supports a limited set of events and properties, so there might be some limitations to this approach.

Additional questions:

  1. How do I add a background video to a section in Webflow?
  2. Can Webflow animations be triggered by mouse movement?
  3. Is it possible to control a video's volume using Webflow interactions?