Jump to content
Search Community

Add Next and Prev buttons to a draggable slider

FLYPILOT test
Moderator Tag

Recommended Posts

That error almost always means you forgot to register a plugin and you're in a module environment. Make sure you're running the code when the window and document are defined. For example, if you're in an SSR environment and try to run anything GSAP-related when the window/document aren't defined, you'll have problems. So you'd just tuck that code into a block that only runs after the window/document are defined. 

 

gsap.registerPlugin(Draggable, InertiaPlugin); // remove InertiaPlugin if you don't have it.

 

Link to comment
Share on other sites

Hi guys, thanks for all the help, however I've found a simple solution using the first example:
 

prevButton.addEventListener("click", function(e) {
    if(slideIndex >= 2) {
      slideIndex = 1
    }
    baseTl.tweenFromTo(slideIndex, slideIndex += 0.25, {immediateRender: true, duration: 1, ease: "power1.inOut"})
  })
  
  nextButton.addEventListener("click", function(e) {
    if(slideIndex <= 0.75) {
      slideIndex = 1.75
    }
    baseTl.tweenFromTo(slideIndex, slideIndex -= 0.25, {immediateRender: true, duration: 1, ease: "power1.inOut"})
  })

this maybe not the optimized solution, but is currently working for 4 items, I think I do need to find a better solution though.

But about the error, I've registered all plugins, and I just did a simple copy and paste code.

Link to comment
Share on other sites

2 hours ago, FLYPILOT said:

this maybe not the optimized solution, but is currently working for 4 items, I think I do need to find a better solution though.

Yeah, that's probably not the best long-term or flexible solution. You really need to make sure things are synchronized across the various interaction possibilities, like if the proxy's x value doesn't get updated according to your new positioning, then the next time you drag you'd probably see things jump. And you'd need to keep track of the index (as it looks like you were) in order to accommodate multiple fast clicks where the user wants to jump ahead mid-tween. It's all just logic stuff that's entirely doable but not exactly simple for us to do for you. We're happy to answer GSAP-specific questions here; we just don't have the resources to wade into building customized logic stuff for your particular implementation for free. We do offer paid consulting services, so if you need some help you can feel free to reach out to us directly for that. 

 

2 hours ago, FLYPILOT said:

But about the error, I've registered all plugins, and I just did a simple copy and paste code.

I'd be willing to bet that you didn't register Draggable before you ran that code (or maybe you did, but it was at a time when window/document were not defined). :) It's pretty much impossible to know without seeing a minimal demo where that error is being thrown. 

 

Thanks for being a Club GreenSock member, @FLYPILOT💚

Link to comment
Share on other sites

  • 9 months later...
On 3/29/2023 at 8:01 PM, FLYPILOT said:

Hi guys, thanks for all the help, however I've found a simple solution using the first example:
 

prevButton.addEventListener("click", function(e) {
    if(slideIndex >= 2) {
      slideIndex = 1
    }
    baseTl.tweenFromTo(slideIndex, slideIndex += 0.25, {immediateRender: true, duration: 1, ease: "power1.inOut"})
  })
  
  nextButton.addEventListener("click", function(e) {
    if(slideIndex <= 0.75) {
      slideIndex = 1.75
    }
    baseTl.tweenFromTo(slideIndex, slideIndex -= 0.25, {immediateRender: true, duration: 1, ease: "power1.inOut"})
  })

this maybe not the optimized solution, but is currently working for 4 items, I think I do need to find a better solution though.

But about the error, I've registered all plugins, and I just did a simple copy and paste code.

Hello Flypilot, can I ask you where and how you defined the variable "slideIndex"?

 

Thanks!

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...