拖动效果,防止选中文字兼容代码

css 设置方法:
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit浏览器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期浏览器*/
 user-select: none;

 

js 设置方法:

window.getSelection? window.getSelection().removeAllRanges():document.selection.empty();

 对比不同:

css 的设置方法是禁止用户选中文字,无论是否是在拖拽效果下;

js 的设置方法是为了保证在拖拽时,禁止文字选中;

代码示例如下:

 document.onmousemove = function () {
    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
 }

这样设置后,当鼠标在拖拽的时候就不会选中文字了

posted @ 2018-12-18 15:14  Sky_Ice  阅读(1469)  评论(1编辑  收藏  举报