Jump to content
Search Community

Using Draggable on window?

mstudio test
Moderator Tag

Go to solution Solved by Jonathan,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I'm assuming the answer is no, but wanted to confirm:

Is it possible to apply Draggable to the window object?

 

I've tried the code below with no luck:

Draggable.create(window, {type:"scrollLeft", edgeResistance:0.2, throwProps:false});

I'm assuming this doesn't work because "window" isn't an actual element. 

 

I've also tried using "html" as the element, but get the error: 

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.

The effect I'm going for is trying to create is using click+drag to scroll the window horizontally. I believe I can do this with standard JS but I love the effect that ThrowPropsPlugin adds.

 

Thanks!

See the Pen mWBrxa by mstudio (@mstudio) on CodePen

Link to comment
Share on other sites

  • Solution

Hello mstudio and welcome to the GreenSock Forum!
 
The reason you get that error is due to the window not being an allowed HTML interface element. That is why getComputedStyle fails:
 
See allowed HTML interface elements in the spec: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model#HTML_element_interfaces
 
As a rule of thumb to prevent errors cross browser you should only drag a child of the body element. You could drag the body element and that wont throw an error. But its best practice to leave the body element alone for dragging and drag a child instead. Since when you apply Draggable on the body tag it will apply either transforms or position offsets like top and left. And then you have to set position absolute on the body tag which is also bad practice due to cross browser issues.
 
You can set a child <div> tag inside your body that acts a container wrapper, and use that as your main drag area of the page.
 
So you can do it this way using the body element:
 

See the Pen EWwXRj by jonathan (@jonathan) on CodePen


 
Or drag a main wrapper container instead to prevent buggy behavior from applying transforms on the body element:
 

See the Pen evGRVw by jonathan (@jonathan) on CodePen


 
I hope this helps!

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