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


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();
 }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM