当前位置:网站首页>JS 获取鼠标选中的文字并处于选中状态
JS 获取鼠标选中的文字并处于选中状态
2022-07-25 06:08:00 【青颜的天空】
项目中需要实现这么个效果
简而言之:鼠标选中一部分文字之后,停留在此状态,并添加前后标志
网上搜了没有此场景的实现方法,只能自己写了;
<div class="d-inline" ng-mousedown="cutMouseDown()" ng-mouseup="cutMouseUp($event)" ng-bind-html="cutedSample"></div>
class CutDemo {
constructor() {
this.cutedSample = ''; // 等待被裁剪的字段
this.selection = null; // 裁剪字段选中的section
this.isParsed = false; // 是否已解析预览
this.maxLenSample = 'bbb'; // 未被裁剪的原字段
}
cutMouseDown() {
if (this.isParsed) return;
this.cutedSample = this.maxLenSample; // 恢复成纯字符串, 等待被再次裁剪
}
cutMouseUp(event) {
if (this.isParsed) return;
this.selection = this.getSelectText(event.target);
}
getSelectText() {
const selection = window.getSelection();
if (selection.type === 'Range') {
//Caret和null不是选中状态
const start = Math.min(selection.anchorOffset, selection.focusOffset);
const end = Math.max(selection.anchorOffset, selection.focusOffset);
if (start === end) return null;
const first = this.maxLenSample;
const startHtml = first.substring(0, start);
const middleHtml = first.substring(start, end);
const endHtml = first.substring(end);
const prefix = '<div class="selection-tag"><div class="st-dot"></div><div class="st-line"></div></div>'; // 标记前缀
const suffix = '<div class="selection-tag"><div class="st-line"></div><div class="st-dot"></div></div>'; // 标记后缀
this.cutedSample = startHtml + '<span class="selection-text">' + prefix + middleHtml + suffix + '</span>' + endHtml;
return {
text: selection.toString(),
end,
start,
focusNode: selection.focusNode,
anchorNode: selection.anchorNode,
rangeCount: selection.rangeCount,
};
} else {
return null;
}
}
}
样式:
.selection-text {
background-color: fade(#4ad3a3, 20);
position: relative;
.selection-tag {
position: absolute;
display: inline-flex;
flex-direction: column;
align-items: center;
width: 0;
.st-dot {
width: 5px;
height: 5px;
border-radius: 5px;
background-color: #4ad3a3;
}
.st-line {
width: 1px;
height: 10px;
background-color: #4ad3a3;
}
}
}
以上代码如有需要,可根据自己项目中的框架进行替换~
边栏推荐
猜你喜欢

剑指 Offer 45. 把数组排成最小的数

Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络

Ffmpeg notes (I) fundamentals of audio and video

Brief introduction of acoustic filter Market

Amazoncaptcha bypasses Amazon IP verification code with 95% success rate

【C语言】指针和数组的深入理解(第一期)

HTB-Beep
![Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure](/img/78/2a4e9d49bee8ef839d9d86fc7c3c83.png)
Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure

"Everyday Mathematics" serial 61: March 1

EOL offline sequence based on iso13209 (Otx)
随机推荐
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法
Codeforces Round #809 (Div. 2)
(15) [driver development] over written copy
Idea commonly used 10 shortcut keys
Mechanism and principle of multihead attention and masked attention
Pdf snapshot artifact
msys2常用配置
sqlilabs less-29
What projects can make money online? Is it reliable to be we media?
(2022 Niuke multi School II) l-link with level editor I (dynamic planning)
Sword finger offer 36. binary search tree and bidirectional linked list
HTB-Beep
JS how to delete an element without deleting its child elements
Memory memory operation function
基于ISO13209(OTX)实现EOL下线序列
阻塞队列分析
新时代生产力工具——FlowUs 息流全方位评测
Evolution of coupon architecture under C2B mode
对于von Mises distribution(冯·米塞斯分布)的一点心得
剑指 Offer 54. 二叉搜索树的第k大节点