Wesley
Wesley

Reputation: 5621

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?

Upvotes: 5

Views: 6107

Answers (3)

icchanobot
icchanobot

Reputation: 3343

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.

Upvotes: 6

Supersharp
Supersharp

Reputation: 31171

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

-ms-user-select : none

To make it work with Internet Explorer 11/-

Upvotes: 1

user1139002
user1139002

Reputation: 81

document.onmousemove = function(ev)
{
    ev.preventDefault();
    /*
    move it
    */
}

Upvotes: 2

Related Questions