在JQuery插件中拖动元素时禁用悬停,文本选择等

Wes*_*ley 5 javascript jquery

我有一个基于以下的旋转木马:

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

当您处于抓取和拖动过程中时,您很容易选择文本.如果我在面板中有链接,我会收到悬停消息等...

我想禁用所有这些,因此当您处于拖动过程中时,其余的交互将被禁用.

想法?

icc*_*bot 6

创建一个这样的样式类:

.unselectable {
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -ms-user-select : none
}
Run Code Online (Sandbox Code Playgroud)

然后稍微更改Javascript以在mousedown上分配此类.所以从那个未经改动的脚本看起来就像这样.

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
    });
});
Run Code Online (Sandbox Code Playgroud)

要禁用悬停,您需要取消绑定事件,mousedown如下所示:

$(this).unbind('mouseenter mouseleave');
Run Code Online (Sandbox Code Playgroud)

然后mouseup如上所述再次重新绑定它们.