我有一个可以拖动的文本区域,其中的文本仍然可以通过拖动来选择。为了区分“文本拖动”和“移动拖动”,我必须知道用户开始拖动的点(即鼠标按下位置)是空白或文本。
我唯一能想到的就是使用字符宽度和行高来计算,我想避免这种情况。
我有一个可以拖动的文本区域,其中的文本仍然可以通过拖动来选择。为了区分“文本拖动”和“移动拖动”,我必须知道用户开始拖动的点(即鼠标按下位置)是空白或文本。
我唯一能想到的就是使用字符宽度和行高来计算,我想避免这种情况。
不太确定你在问什么,但这是我将精神错乱转化为工作代码的尝试:
let doc, htm, bod, I; // for use on other loads
addEventListener('load', ()=>{
doc = document; htm = doc.documentElement; bod = doc.body; I = id=>doc.getElementById(id);
// magic under here
const test = I('test');
function whiteStart(string){
if(string.match(/^\s+/)){
return true;
}
return false;
}
test.oncopy = test.ondragstart = function(){
let s = getSelection().toString();
if(whiteStart(s)){
console.log('"'+s+'" starts with one or more white spaces');
}
else{
console.log('"'+s+'" starts without a white space');
}
}
}); // end load
<div id='test'>I'm just going to put some text in here so we can run a very basic test. I hope this helps!</div>