当前位置:网站首页>JS gets the text selected by the mouse and is in the selected state
JS gets the text selected by the mouse and is in the selected state
2022-07-25 06:12:00 【Blue sky】
This effect needs to be achieved in the project 
In short : After the mouse selects part of the text , Stay in this state , And add front and rear marks
There is no implementation method of this scenario found on the Internet , I can only write by myself ;
<div class="d-inline" ng-mousedown="cutMouseDown()" ng-mouseup="cutMouseUp($event)" ng-bind-html="cutedSample"></div>
class CutDemo {
constructor() {
this.cutedSample = ''; // Fields waiting to be cropped
this.selection = null; // Crop the selected section
this.isParsed = false; // Whether the preview has been parsed
this.maxLenSample = 'bbb'; // The original field that has not been cropped
}
cutMouseDown() {
if (this.isParsed) return;
this.cutedSample = this.maxLenSample; // Restore to pure string , Waiting to be cut again
}
cutMouseUp(event) {
if (this.isParsed) return;
this.selection = this.getSelectText(event.target);
}
getSelectText() {
const selection = window.getSelection();
if (selection.type === 'Range') {
//Caret and null Not selected
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>'; // Tag prefix
const suffix = '<div class="selection-tag"><div class="st-line"></div><div class="st-dot"></div></div>'; // Mark suffix
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;
}
}
}
style :
.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;
}
}
}
If necessary, the above code , It can be replaced according to the framework in your own project ~
边栏推荐
- [C language] in depth understanding of pointers and arrays (phase I)
- 剑指 Offer 32 - I. 从上到下打印二叉树
- Memory memory operation function
- (牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
- MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
- Solve the problem of invalid modification of QT 5 interface. Solve the problem of invalid modification of qtdesigner
- Ceres solver version 1.14 and eigen3.2.9
- 对于von Mises distribution(冯·米塞斯分布)的一点心得
- Openzeppelin scalable template library contract initialization details
- [node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
猜你喜欢

How to play a data mining game entry Edition

Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)

Difference between NPX and NPM

Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network

Sword finger offer 54. the k-th node of the binary search tree
![(14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动](/img/90/0d94d26be8128d77de65919763fda5.png)
(14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动

(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)

Siggraph 2022 -- rendering iridescent rock dove neck feathers

(Niuke multi School II) j-link with arithmetic progress (least square method / three points)

JTAG debugging source level debugging of arm bare board debugging
随机推荐
Era5 dataset description
Unity animator animation and state machine
Req.body in node.express is always undefind
VBS common built-in functions (2)
Use abp Zero builds a third-party login module (4): wechat applet development
Unity model simplification / consolidation one click plug-in
What determines the "personality" of AI robots?
leetcode/二进制加法
leetcode/ 前 n 个数字二进制中 1 的个数
What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
MFC IniFile Unicode mode reading method
Design of automatic machine dot drawing script based on C language
动态规划学习笔记
mysql数据库备份和恢复
leetcode/整数除法
Mechanism and principle of multihead attention and masked attention
VSCode 如何开启多个终端?如何横向显示?
SAP FICO 第三节 BDC和LTMC导入S4财务科目
机器学习 Keras拟合正弦函数