【问题标题】:css text ::selection with round cornerscss文本::圆角选择
【发布时间】:2020-04-20 13:02:33
【问题描述】:

我在 VSCode 和 monaco-editor 中看到了这个选择,它看起来非常好:

所以我尝试重新创建它,这是我的努力:

#ta{
  font-size : 18px;
}
#ta::selection{
  background : rgba(173, 216, 130, 0.9);
  border : 1px solid transparent;
  border-radius : 15px;
}
<textarea id="ta"></textarea>

但这并没有像图片中显示的那样得到圆角效果(我希望你能得到它)。

请帮帮我。感谢您的回答。

【问题讨论】:

  • 查找::selection,只有非常小的CSS规则子集可以应用于它,border-radius不是其中之一。换句话说,以这种方式似乎是不可能的。您也许可以使用 JS 将 current selection 包装在 span 中,然后添加一些 display: inline-block 和所需的 border-radius,尽管这可能不适用于 textarea
  • @domsson 你能给我一个尝试吗?
  • 问题是什么?只在选择文本区域时要圆角吗?
  • 我需要 ::selection 圆角

标签: javascript html css selection


【解决方案1】:

正如一些人已经对你的问题发表了评论

::selection 只是可以应用到它的 CSS 规则的一个很小的子集,border-radius 不是其中之一......

那么,下一步是,如果你真的需要它,你怎么能绕过它?选项之一是: 您可以使用“选定”字符串,然后添加一些 HTML 标记。然后,当您选择文本并释放鼠标时,您的所有 CSS 样式都会被应用。

请记住:这可能不适用于“普通”文本框

希望下面的代码能给你一个入门的例子

var selectionElements = document.querySelectorAll('.selection');
selectionElements.forEach(function(element){
    element.setAttribute('original-content', element.innerHTML);  // this will be needed to reset to original after a selection has been made
    element.addEventListener('mouseup', function() {
        replaceContentWithSelectionWrapper(this)
    });
});

function replaceContentWithSelectionWrapper(element) {
    let selection = window.getSelection().toString();
    if(selection.length <= 0) { // if selection length is not bigger then 0 we can stop right here
        return;
    }
    // next lines should be self explanatory
    // get start of string until selection
    // get the end of string after selection
    // concatenate all strings back together
    let selObj = window.getSelection(); 
    let selRange = selObj.getRangeAt(0);
    let originalString = element.innerHTML;
    let start = originalString.substr(0, selRange.startOffset);
    let end = originalString.substr(selRange.endOffset);
    element.innerHTML = start + '<span class="mark-special-selected">' + selection + '</span>' + end;
    document.body.classList.add('selections-enabled');
}

function clearSelections() {
    var selections = document.querySelectorAll('[original-content]');
    selections.forEach(function(selection){
        selection.innerHTML = selection.getAttribute('original-content');
    });
}

document.body.addEventListener('mousedown', function(){
    if(document.body.classList.contains('selections-enabled')) {
        document.body.classList.remove('selections-enabled');
        clearSelections();
    }
});
.selection {
  font-size : 18px;
}
.mark-special-selected,
.selection::selection{
  background : rgba(173, 216, 130, 0.9);
  border : 1px solid transparent;
  border-radius : 15px;
  outline: 2px;
}
<h1 class="selection">Here is some text, you can select some of it with your mouse</h1>
<p class="selection">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>

【讨论】:

  • 我无法修复你在我的发现的错误,而你似乎采取了相同的方法,所以我删除了我的。但是,你的对我不起作用。不知道为什么。
  • @domsson 感谢您的反馈。我已经在 chrome 和 safari 中进行了测试,对我来说效果很好。也已更新,因此它现在也可以在 Firefox 中使用。您能否检查一下它现在是否有效,或者让我更准确地知道什么无效。谢谢
  • 第一个也是最重要的问题是两个选择中只有一个有效。每一秒的选择都不会产生任何突出显示的文本。第二个也是不太重要的一个是有一个短暂但值得注意的时刻(可能是半秒),您会看到一个矩形选择,然后变为圆形选择。但是,这比昨天要好得多,当时我无法突出显示任何文本。这是在 Firefox 68.2.0esr / Linux 上。
  • 再次感谢@domsson 的反馈!是的,FF 不起作用,因为他处理点击鼠标的方式与 chrome 和 safari 不同。根据您释放鼠标的时间,会出现矩形到圆形的问题。我们可以改进它,但我不想重写 jvascript 中的选择行为.. 快乐编码
  • 非常感谢@caramba,你做了我认为不可能的事情。 +1m 非常感谢!
【解决方案2】:

有了这个 CSS,效果更像 VS Code 的文本选择,只是没有立即圆整,而且对每个交点没有效果(并且行之间有一行 1 个像素的空白,由于没有边框),但选择它时文本不会移动。

提醒一下Visual Studio Code的文字选择是这样的:



下面代码sn-p的其他问题:

  • 如果在文本框外释放点击,圆角效果可能不起作用;
  • 如果文本是从文本框外选择的,则默认选择;
  • 本例中,如果光标选中页眉文本,松开对下方文本的点击,则上方选中的文本写在下方,效果只在下方段落;
  • 在本例中,如果光标选择下部文本并释放对上部文本的单击,则下部选定文本写在上方,并且效果仅对现在位于标题文本中的选定复制部分起作用;
  • 如果文本选择得太快(我认为这是原因),圆角对标题文本不起作用。

无论如何,这个文本选择发生了奇怪的事情,如果有人能处理它们,那就太好了。

var selectionElements = document.querySelectorAll('.selection');
selectionElements.forEach(function(element){
    element.setAttribute('original-content', element.innerHTML);  // this will be needed to reset to original after a selection has been made
    element.addEventListener('mouseup', function() {
        replaceContentWithSelectionWrapper(this)
    });
});

function replaceContentWithSelectionWrapper(element) {
    let selection = window.getSelection().toString();
    if(selection.length <= 0) { // if selection length is not bigger then 0 we can stop right here
        return;
    }
    // next lines should be self explanatory
    // get start of string until selection
    // get the end of string after selection
    // concatenate all strings back together
    let selObj = window.getSelection(); 
    let selRange = selObj.getRangeAt(0);
    let originalString = element.innerHTML;
    let start = originalString.substr(0, selRange.startOffset);
    let end = originalString.substr(selRange.endOffset);
    element.innerHTML = start + '<span class="mark-special-selected">' + selection + '</span>' + end;
    document.body.classList.add('selections-enabled');
}

function clearSelections() {
    var selections = document.querySelectorAll('[original-content]');
    selections.forEach(function(selection){
        selection.innerHTML = selection.getAttribute('original-content');
    });
}

document.body.addEventListener('mousedown', function(){
    if(document.body.classList.contains('selections-enabled')) {
        document.body.classList.remove('selections-enabled');
        clearSelections();
    }
});
.selection {
  font-size: 18px;
}
.mark-special-selected, .selection::selection{
  background: #8884;
  border-radius: 4px;
}
<h1 class="selection">Here is some text, you can select some of it with your mouse</h1>
<p class="selection">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多