Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable hover, text select, etc when dragging an element in JQuery plug in

I have a carousel based off of:

http://nooshu.com/explore/jquery-iphone-animation/

When you are in the process of grabbing and dragging, you're apt to select text. If I have links in the panels, I get the hover message, etc...

I would like to disable all that, so when you are in the process of dragging, the rest of the interaction is disabled.

Ideas?

like image 694
Wesley Avatar asked Sep 07 '11 00:09

Wesley


People also ask

How can I prevent text element selection with cursor drag?

You'll need to call event. preventDefault() in both the down and move events in order to keep the browser from selecting text. Save this answer.

How do I turn off drag and drop in CSS?

Another way to disable dragging an image is to set the pointer-event CSS property to none .


3 Answers

Create a style class like this:

.unselectable {
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -ms-user-select : none
}

Then alter the Javascript slightly to assign this class on mousedown. So from that unaltered script it would look like this.

jQuery(function($){
    //Initialise
    iPhoneAnimation.init();

    //Mouse down bind move event
    $(".content-box").bind("mousedown", function(e){
            $(this).bind("mousemove", iPhoneAnimation.moveInfo);
            $(this).addClass("unselectable");  // <-- ADD THIS
    });

    //Unbind mouse event
    $(".content-box").bind("mouseup", function(e){
        var $this = $(this);

        $this.unbind("mousemove", iPhoneAnimation.moveInfo);
        //Animate the panel
        iPhoneAnimation.panelAnimate($this);
        //Reset the private var
        iPhoneAnimation.resetVars();

        $(this).removeClass("unselectable"); // <-- AND ADD THIS
    });
});

For disabling hovers you need to unbind the events on mousedown like this:

$(this).unbind('mouseenter mouseleave');

Then rebind them again on mouseup as above.

like image 77
icchanobot Avatar answered Sep 30 '22 17:09

icchanobot


document.onmousemove = function(ev)
{
    ev.preventDefault();
    /*
    move it
    */
}
like image 44
user1139002 Avatar answered Sep 30 '22 17:09

user1139002


I would complete Icchanobot's answer with one more line:

-ms-user-select : none

To make it work with Internet Explorer 11/-

like image 26
Supersharp Avatar answered Sep 30 '22 16:09

Supersharp