Skip to content

How to manually stop dragging? #177

@subversivo58

Description

@subversivo58

How to leave the event if a given condition is reached?

example:

$('#element').draggabilly({
   containment: true,
   axis: 'x'
}).on('dragMove', function() {
   let containerWidth = $(this).parent().width()
   let buttonWidth = $(this).outerWidth()
   let max = (containerWidth - buttonWidth) - 1 // count border right (1px)
   // condiction
   if ( $(this).position().left >= max ) {
       console.log('limit reached')
       return false // escape here!!! don't escaped!
   }
}).on('dragEnd', function() {
   let containerWidth = $(this).parent().width()
   let buttonWidth = $(this).outerWidth()
   let max = (containerWidth - buttonWidth) - 1 // count border right (1px)
   // confirm condiction
   if ( $(this).position().left >= max ) {
       // do stuff...
   } else {
       // do stuff...
   }
}).on('click', function() {
   //...
})

I'm assuming that return on "move" would trigger "end"? Is there a correct method?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions