-1

I found these two very simple draggable div blocks of code: link1 and link2

How to constrain movement in x-axis to screen width with JS/jQuery without jQuery UI?

I DID SEARCH but I there are only references to jQuery UI.. How to do it yourself?

Solo
  • 6,687
  • 7
  • 35
  • 67
  • Possible duplicate of [jQueryUI Draggable: Constrain draggability to a single axis?](http://stackoverflow.com/questions/3296182/jqueryui-draggable-constrain-draggability-to-a-single-axis) – Gregor Menih Feb 29 '16 at 22:43
  • @ItsGreg OP is not using jQueryUI. :) – Jack Guy Feb 29 '16 at 22:44
  • ***I DID SEARCH*** Did you code? **WHERE IS YOUR CODE?** – Frédéric Hamidi Feb 29 '16 at 22:48
  • Possible duplicate of [Moveable/draggable
    ](http://stackoverflow.com/questions/9334084/moveable-draggable-div)
    – stackErr Feb 29 '16 at 22:50
  • @FrédéricHamidi What code? I already told you that first 10 pages of Google search is only about jQuery UI, not **DIY** and we all know that there's endless void after 10 pages of Google. Very simple codes are linked to question. – Solo Feb 29 '16 at 22:51
  • @Solo, I don't follow. If you insist on not using jQuery UI (which has solved this problem for you) in the first place, why not at least try to learn from its code? Why ask us without posting anything? – Frédéric Hamidi Feb 29 '16 at 22:53
  • @FrédéricHamidi You've got a point there.. I've tried to learn things from source code but it's very confusing and timeconsuming to learn from a messy source code that is written by a pro who uses a lot of short variable names _etc_.. I couldn't found an answer here, so I thought why not.. Somebody else might me interested in this now or some day.. – Solo Feb 29 '16 at 22:58

1 Answers1

2

I'm going to base it off of the first code snippet you linked to.

In order to constrain the div to horizontal movement, all you need to do is remove the part of the code that sets the top offset!

After that, constraining the div to the boundary of the window takes some thought. The coordinates currently offseting the div (e.pageX + pos_x - drg_w) correspond to the top-left corner of the div. We never want that to go past the leftmost boundary of the window, or x=0. A nice way to do this mathematically is with the Math.max function, which will return the larger of two values.

left: Math.max(e.pageX + pos_x - drg_w, 0)

Now we need to check the right border of the window. We take the coordinate of the right boundary ($(window).width()) and subtract the width of the div since we're checking the right edge of the div against it. Correspondingly, Math.min can be used to great effect in this situation.

All together now,

$('.draggable').offset({
    left: Math.min($(window).width() - drg_w, Math.max(e.pageX + pos_x - drg_w, 0))
})

And a code pen of those minor changes. :)

Jack Guy
  • 8,346
  • 8
  • 55
  • 86
  • That's awesome. :-) I my question was probably set incorrectly, I didn't want to disable y-axis, only to constrain x-axis but still.. Good answer! – Solo Feb 29 '16 at 23:02
  • @Solo Oh, OK. Well you can use a similar tweak to constrain the boundaries of the y-axis as well, if you need that. :) – Jack Guy Feb 29 '16 at 23:03