Jump to content
Search Community

Scrolltrigger not restart the video

antonio michelon test
Moderator Tag

Recommended Posts

hey guys

I'm starting on Gasp, I'm trying to use the scrollingtrigger to control a video according to the page's scroll, with the examples I found here on the site I managed to make play and pause work, but I still couldn't make the video start from the beginning when I go back to the video, in the code I changed the pause option to restart, but nothing has changed in the behavior of the video, below is the code with the restart.
grateful for the help!

 

  
  let videoElem = videoDiv.querySelector('video')
  ScrollTrigger.create({
    trigger: videoElem,
    start: 'top 80%',
    end: 'top 20%',
    markers: true,
    onEnter: () => videoElem.play(),
    onEnterBack: () => videoElem.play(),
    onLeave: () => videoElem.restart(),
    onLeaveBack: () => videoElem.restart(),

See the Pen eYPLmzv by Antonio-Miguel-Michelon (@Antonio-Miguel-Michelon) on CodePen

Edited by antonio michelon
include codepen
Link to comment
Share on other sites

Hi @antonio michelon and welcome to the GreenSock forums!

 

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi,

 

Please do not create duplicate posts. We do our best to answer as soon as we we can. Unfortunately it might take more time than expected to actually give a user an answer, but an answer will be given, so please be patient.

 

First you have an error in your code. The HTMLMediaElement API doesn't have a restart method, just play and pause:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

 

Also on your example your second video was not wrapped in a div with the class "vid" so this returned an array with just one element:

let allVideoDivs = gsap.utils.toArray(".vid");

Maybe you're looking for something like this:

const allVideoDivs = gsap.utils.toArray(".vid");

const restartVideo = (video) => {
  video.pause();
  video.currentTime = 0;
}

allVideoDivs.forEach((videoDiv, i) => {
  let videoElem = videoDiv.querySelector("video");
  ScrollTrigger.create({
    trigger: videoElem,
    start: "bottom bottom",
    end: "bottom top",
    markers: true,
    onEnter: () => videoElem.play(),
    onEnterBack: () => videoElem.play(),
    onLeave: () => restartVideo(videoElem),
    onLeaveBack: () => restartVideo(videoElem),
  });
});

Here is a fork of your demo:

See the Pen YzJOOZO by GreenSock (@GreenSock) on CodePen

 

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

Apologies for the duplicate post, it won't happen again.

now the code works in codepen, but when i put it in visual studio code it doesn't work the part of restarting the video, just starting and pausing, i installed the greensock snippets extension, do i need anything else?

Link to comment
Share on other sites

Hi,

 

Honestly I don't know what to tell you. The code for the restart method pauses the video you pass as a parameter and then sets it's current time to zero, so it acts like the video will start from the beginning again the next time.

 

Make sure to check your console and the method you're using for restarting the video, that again, is not really a restart just a pause and setting the current time value, just that.

 

Happy Tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...