Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if element is currently dragging

I want to be able to check whether or not an event is occurring at the time a function is called. I have a function being called when my custom scroll bar is being dragged using .draggable() up and down and I also have a function being called when my container is scrolling. The problem is that both run at the same time which makes it act buggy.

So my question is how do I do an "if" statement checking whether or not the scroll bar is currently being dragged so I can stop it from executing the rest of the function's code?

I am not asking if an element has an event "binded" to it or not, but rather if that event is being triggered at a particular moment.

Can I do this? Or do I need to take another approach?

Here's what my code looks like:

$('.container').scroll(function(){
    //get the heights of the container and it's contents and the difference
    //get the height of the scroll bar and it's container and the difference
    //declare the top property of scroll bar from this info
});
function scrolly() {
    //get the heights of the elements described above
    //declare the scrollTop of the container
}
$('.bar').draggable({
    axis: 'y',
    containment: 'parent',    
    drag: function() {
        scrolly();
    }
});
like image 482
Kevin Beal Avatar asked May 25 '12 05:05

Kevin Beal


People also ask

What is the functionality of ondragover() event?

The ondragover attribute fires when a draggable element or text selection is being dragged over a valid drop target. By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element. This is done by calling the event.

How to drag elements in js?

Introduction to JavaScript Drag and Drop API To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.

What is a valid drop target?

If the mouse is released over an element that is a valid drop target, that is, one that cancelled the last dragenter or dragover event, then the drop will be successful, and a drop event will fire at the target.


1 Answers

Update

You can use this condition to find out if your bar is being dragged:

if ($('.bar').is('.ui-draggable-dragging')) {
    return; // bar is being dragged
}
like image 182
Ja͢ck Avatar answered Sep 27 '22 19:09

Ja͢ck