how to prevent text selection when dragging mouse in Firefox?

前端 未结 2 1780
误落风尘
误落风尘 2021-01-05 05:40

I am wondering how those drag and drop widgets cancel text selection in the dragging element and other elements in the page. I tried the following code which works in IE8 (c

2条回答
  •  囚心锁ツ
    2021-01-05 06:07

    There was a case where I had a horizontal scrollbar and text on the page was being selected while dragging the scroll bar. I used jQuery to add an "unselectable" class to the body when my scrolling start function fired off, and removed the class when my scrolling stop function executed. Like so:

    function startDrag(event){
        $('body').addClass('unselectable');
    
        // start drag code
    }
    
    function stopDrag(event){
        $('body').removeClass('unselectable');
    
        // stop drag code
    }
    

    And this is what the unselectable class looked like in my CSS doc.

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

提交回复
热议问题