我正在编写日志消息框/与聊天功能相同的功能,一旦有人在聊天中选择文本(例如,如果用户想粘贴某个内容),我希望将我的AutoScrollDown布尔值设置为false。
我使用ul和lis实现了日志消息/聊天框,如下所示:
<ul id="1" class="logbox">
<li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li>blabla</li>
</ul>我尝试了以下事件来检查我的ul中的内容是否被选中,这是不起作用的:
$("#1").on("focus", function() {
autoScrollEnabled = 0;
});
$("#1").off("focus", function() {
autoScrollEnabled = 1;
});我的问题:如果有人选择/高亮显示我ul中的文本,我如何将autoScrollEnabled设置为0,以及如何检查用户是否停止了选择/高举任何文本。
发布于 2016-09-07 16:02:35
要检测用户的文本是否高亮显示在日志框中,您应该首先侦听mousedown事件,然后查看鼠标仍然按下时是否存在mousemove事件。您可以在鼠标向下或鼠标移动时将您的标志设置为false,然后在mouseup事件中重置它。
document.querySelector('#one').addEventListener('mousedown', function (event) {
window.onmousemove = function (event) {
console.log('the user is selecting text');
}
window.onmouseup = function (event) {
console.log('the user mousedup');
console.log(window.getSelection());
autoScrollEnabled = 1;
window.onmousemove = null;
window.onmouseup = null;
}
console.log('user mouseddown in ' + event.target.nodeName);
autoScrollEnabled = 0;
}, false);<ul id="one" class="logbox">
<li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li>blabla</li>
</ul>
发布于 2016-09-05 02:21:57
这个片段演示了:
它可以检测有或没有表单输入的文本的选择。 将触发
mouseup事件,并充分处理误报,如单击文本,但没有突出显示。
mouseup事件。event.target (即点击、鼠标操作、放屁等),选择都是正确的。b.条件2(不需要折叠):选择不只是对文本的一次单击(即没有突出显示)
*通过使用属性.isCollapsed,我们知道如果选择的起点与选择的结束点相同,则.isCollapsed为真,因此没有突出显示文本。
片段
/*
This Snippet demonstrates that:
A. It can detect the selection of text with or without form inputs.
B. Will trigger on mouseup event and adequately handle false positives like text clicked but nothing highlighted.
*/
// Reference a selection object
var selection = window.getSelection();
/*
1. Delegate the whole document to listen for a mouseup event.
2. Next define the boolean state with:
a. Condition 1 (needs to be true): That a selection object exists
* selection is true as soon as any text is the event.target (i.e. clicked, mouseup, farted on, etc.)
b. Condition 2 (needs to be not collapsed): That the selection is not just a single click on text (i.e. nothing highlighted)
* By using the property .isCollapsed we know that if the starting point of a selection is the same as the ending point of a selection, then .isCollapsed is true and therefore no text was highlighted.
3. Since autoscroll was never explained, I have simplified it as an alert.
a. If autoScroll is disabled, the alert will also contain the text that's selected.
*/
$(document).on('mouseup', function(e) {
var state = (selection && !selection.isCollapsed) ? alert('autoScroll is disabled {{' + selection + '}} is selected') : alert('autoScroll is still enabled');
return state;
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list1" class="logbox">
<li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li>blabla</li>
</ul>
参考文献
- [window.getSelection()](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection)
- [.isCollapsed](https://developer.mozilla.org/en-US/docs/Web/API/Selection/isCollapsed)
发布于 2016-09-05 02:42:19
li元素没有focus事件,但是可以绕过它。看下面的片段。
var autoScrollOn = 1; //declare globally
var timeoutHandle = 0; //for additional service
$('#ul1').on('click', 'li', function() { //delegate click event
clearTimeout(timeoutHandle); //clear timeout if any
if ($(this).hasClass('selected')) {
//remove selection on the second click
$('#ul1 li').removeClass('selected');
autoScrollOn = 0;
} else {
//remove selection from siblings
$('#ul1 li').removeClass('selected');
//and add to the clicked element
$(this).addClass('selected');
autoScrollOn = 1;
//restore autoscroll after 10s
timeoutHandle = setTimeout(function() {
$('#ul1 li').removeClass('selected');
autoScrollOn = 0;
console.log(autoScrollOn);
}, 10000);
}
//do whatever you want with autoscroll
console.log(autoScrollOn);
});.debug {
background: #aaf;
}
.error {
background: #faa;
}
.warning {
background: #ffa;
}
.selected {
border: solid 1px #f00;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="ul1" class="logbox">
<li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
<li>blabla</li>
</ul>
https://stackoverflow.com/questions/39295256
复制相似问题