Jump to content
Search Community

The object in Draggable does not listen to the mousedown event, but the click and touch events are fine

lzy100p test
Moderator Tag

Recommended Posts

Hello @lzy100p

 

This here from the docs could help:

 

allowEventDefault Boolean - If true, preventDefault() won't be called on the original mouse/pointer/touch event. This can be useful if you want to permit the default behavior like touch-scrolling. Typically, however, it's best to let Draggable call preventDefault() on the events in order to deliver the best usability with dragging.

 

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

  • Like 2
Link to comment
Share on other sites

19 minutes ago, Cassie said:

Just jumping in with a fork of @akapowl's pen - he's correct but you hadn't defined the d2 variable anywhere so nothing was firing.

 

Interesting. It does fire for me in Chrome on Windows.

 

Actually I remember Tom Miller and Alex Trost discussing this in the recent TrostCodes live-stream where both built a 3D-ScrollTrigger image-gallery - I think I saw you in the chat there, too @Cassie - but I also think they talked about it rather close to the beginning, so probably before you entered the chat :) 

 

When you do not define the variable in JS, some browsers will automatically target IDs of HTML elements with that respective name - apparently some browsers do not do that. So definitely defining the variable to target the element in JS will be more secure.

  • Like 2
Link to comment
Share on other sites

Yes, I'm totally on board with that - especially since it does not appear to be consistant across browsers.

 

Here is where they had mentioned it in that live-stream

 

https://www.twitch.tv/videos/1486428887?t=22m18s

 

 

 

And here is another reference - I love the 'When should you use this?' part in there - TLDR: "Never, ever, ever, ever in your actual code base." 😅

 

https://gomakethings.com/your-browser-automatically-creates-javascript-variables-for-elements-with-an-id/

 

https://dev.to/buntine/dom-elements-with-ids-are-global-variables

 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Wow I'm late -just seeing this thread now :)

I 100% agree, one shouldn't rely on IDs to become global vars in any actual project. That said, it's a cheap shortcut for making concise demos.

 

If you really wanted that behavior (I've worked for a few agencies that do this), you could loop over all the elements with an ID and make them global vars, like so:
 

[].forEach.call(document.querySelectorAll('*'), function(el){ if (el.id) window[el.id] = el; }); // IDs to vars

However, for future readers/editors of the code, it's kind of nice to see clearer variable definitions...sort of an intro to the cast of characters.

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