Jump to content
Search Community

Change direction of timeline based on mouse position

BalkanBrothers test
Moderator Tag

Recommended Posts

Hello everyone, I would like to change the timeline direction from left to right and right to left based on the position of the mouse but I can't seem to get how to do it. I saw this Codepen: 

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

 and it works but mine doesn't.

 

Any help would be great. thank you in advance

See the Pen ZEQyaJY by dongepulango (@dongepulango) on CodePen

Link to comment
Share on other sites

Hey BalkanBrothers and welcome. 

 

How are you expecting the hover effect to work? The slides are animating to start. What happens when it gets hovered? Does the original animation stop? Are you wanting to hover the left side and have it go left and the right side go right? Some sort of scale where it's always moving in one directly or another unless the mouse is perfectly in the center? More details would be appreciated.

Link to comment
Share on other sites

@BalkanBrothers

 

Is this the sort of effect you wanna achieve?

 

See the Pen 86230786f141d29e61b37aeabf17433b by akapowl (@akapowl) on CodePen

 

Note that it is not clean at all - especially, when moving from one side to the other, there is a huge jump. 

Made this just for clarification of some sort for the effect you might want.

 

There probably is a way better way to to this with stronger use of GSAP's Utility Methods, I guess.

  • Like 1
Link to comment
Share on other sites

  • 11 months later...

Hi all,

 

Sorry to bump this old post up. @akapowl's answer is exactly what I'm looking for a slider I need to build. There's this jump between sides that I can't seem to fix. Anyone willing to have a go at it?

 

Thanks in advance

Link to comment
Share on other sites

I'd probably take a very different approach:

  1. Create an infinitely repeating timeline. There's a helper function in the docs for this, but you can do it however you want. 
  2. Hook that up to a simple mousemove/pointermove event handler that alters its timeScale based on the position in the viewport. Negative timeScale goes in the reverse direction. 

See the Pen poeppvM?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Is that what you're looking for? 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 3 weeks later...

Hello to all.
I have an issue and I can't resolve, it's basically to do interactive with "text"class under of .left and .right. Somebody have any cool idea for fix this stuff?

Thanks in advance. ☺️

Link to comment
Share on other sites

Hey @Aday

 

With Jack's approach there is no more need for the overlaying left and right divs, so you can just remove those and access the "text" divs however you like (here with a simple hover).

 

One more reason, I recommend using his approach.

Welcome to the forums :) 

 

See the Pen ee0f82179345755d26dd7c4b09fe3997 by akapowl (@akapowl) on CodePen

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

  • 5 months later...

What if each of the .text (in my case .map) width is set to auto with an image child element for each? How do we trigger the animation to move the panels left or right?

If I set the width of .map to 100%, the animation works but if I set the width to auto, the animation is not triggered. Each of the image maps span more than the device's width.

 

<div id="maps-wrapper" class="h-screen overflow-hidden">
  <div id="maps" class="relative flex items-start">
    <div class="map relative h-screen w-auto flex-shrink-0 box-border">
      <img class="object-cover h-screen w-full" src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/images/map-1.jpg" alt=""/>
    </div>
    <div class="map relative h-screen w-auto flex-shrink-0 box-border">
      <img class="object-cover h-screen w-auto " src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/images/map-2.jpg" alt=""/>
    </div>
    <div class="map relative h-screen w-auto flex-shrink-0 box-border">
      <img class="object-cover h-screen w-full" src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/images/map-3.jpg" alt=""/>
    </div>
    <div class="map relative h-screen w-auto flex-shrink-0 box-border">
      <img class="object-cover h-screen w-full" src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/images/map-4.jpg" alt=""/>
    </div>
  </div>
</div>
Link to comment
Share on other sites

1 hour ago, akapowl said:

Hello @dplgngr

 

Event then it still appears to work just fine for me.

 

Maybe you just need to wait for the images to be loaded into your page properly before you run the code for the loop.

 

If it doesn't help, a minimal demo of yours would be really helpful for helping you.

 

This worked! Thanks!

Last one, how do I pause the animation when the mouse is not on the page or user shifts to another window/tab?

Link to comment
Share on other sites

 

You shouldn't need to worry about the changing of tabs, because by default browsers will throttle things anyways.

If you want to pause the animation when your cursor leaves  the window, you can add an eventListener of your own. Something like this maybe:

 

  document.addEventListener("mouseleave", (event) => {
    loop.pause();
  });

 

 

  • Like 2
Link to comment
Share on other sites

2 hours ago, akapowl said:

 

You shouldn't need to worry about the changing of tabs, because by default browsers will throttle things anyways.

If you want to pause the animation when your cursor leaves  the window, you can add an eventListener of your own. Something like this maybe:

 

  document.addEventListener("mouseleave", (event) => {
    loop.pause();
  });

 

 

Perfect! That worked, however it seems to be not working on Firefox.

See the Pen YzrXQQN by dplgngr (@dplgngr) on CodePen

 

 

Link to comment
Share on other sites

16 minutes ago, akapowl said:

Maybe try adding the eventListener to the body instead

 

  document.body.addEventListener("mouseleave", (event) => {
    loop.pause();
  });

 

What I tried and worked is to target the a specific parent element #page.

  const mouseTarget = document.getElementById( 'page' );
 
  mouseTarget.addEventListener( 'mouseleave', e => {
    loop.pause();
  } );

 

I tried your suggestion above and it also worked.

 

Thanks a lot @akapowl

  • Like 2
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...