热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何应用圆角边框突出显示/选择-Howtoapplyroundedborderstohighlight/selection

IhaveusedVisualStudioOnlineforawhileforaproject,andthewaytheyapplyroundedborderst

I have used Visual Studio Online for a while for a project, and the way they apply rounded borders to selections in their online code viewer is very interesting:

我已经在项目中使用了Visual Studio Online一段时间,并且它们在其在线代码查看器中将圆形边框应用于选择的方式非常有趣:

I've tried inspecting the element and looking for some kind of custom CSS, but had no luck.

我已经尝试检查元素并寻找某种自定义CSS,但没有运气。

I have a feeling this requires some complex "hacks" to make it work, but it seems very interesting as I've never seen it done before.

我有一种感觉,这需要一些复杂的“黑客”来使它工作,但它似乎非常有趣,因为我以前从未见过它。

How are they able to apply rounded borders to a selection?

他们如何能够将圆形边框应用于选择?

Note: The normal selection is completely hidden WHILE selecting, and the rounded selection follows your cursor just like a regular selection. Not AFTER you have selected something.

注意:正常选择在选择时完全隐藏,圆角选择跟随光标,就像常规选择一样。不是在你选择了什么之后。

Edit: I have created a fork of @Coma's answer that should work in Firefox and select while the mouse if moving using:

编辑:我已经创建了一个@Coma答案的分支,应该可以在Firefox中使用并在鼠标移动时选择:

$(document).on('mousemove', function () {

(The borders in certain cases could still use work.)

(在某些情况下,边界仍然可以使用工作。)

4 个解决方案

#1


7  

Not perfect but it's working:

不完美,但它的工作:

http://jsfiddle.net/coma/9p2CT/

http://jsfiddle.net/coma/9p2CT/

Remove the real selection

删除真正的选择

::selection {
   background-color: transparent;
}

Add some styles

添加一些样式

span.highlight {
    background: #ADD6FF;
}

span.begin {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

span.end {
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
}

pre.merge-end > span:last-child {
    border-bottom-right-radius: 0;
}

pre.merge-end + pre > span:last-child {
    border-top-right-radius: 0;
}

pre.merge-begin > span:first-child {
    border-bottom-left-radius: 0;
}

pre.merge-begin + pre > span:first-child {
    border-top-left-radius: 0;
}

Wrap every character in a node element

包装节点元素中的每个字符

var format = function () {

    var before = -1;
    var html = $.trim(editor.text())
    .split("\n")
    .reverse()
    .map(function (line) {

        var a = line.length === before ? 'merge-end' : '';
        before = line.length;

        return '
' + line.split('').join('') + '
'; }) .reverse() .join(''); editor.html(html); };

Get the selected nodes and highlight them, take care of their parents

获取所选节点并突出显示它们,照顾他们的父母

var getSelectedNodes = function () {

    var i;
    var nodes = [];
    var selection = rangy.getSelection();

    for (i = 0; i 

Thanks to Tim Down for Rangy!

感谢Tim Down for Rangy!

#2


5  

I can assure you this has nothing to do with html, css border radius or highlighting. The proof?

我可以向你保证,这与html,css边框半径或突出显示无关。证据?

  1. The whole selection appears as a single block with multiple edges and veritices and is not symmetric. There is no way to have multiple edged shape directly in html, without using SVG or Canvas. (well, that's a possibility open for discussion)
  2. 整个选择显示为具有多个边和格的单个块,并且不对称。在不使用SVG或Canvas的情况下,无法直接在html中使用多边形状。 (嗯,这是一个可以讨论的可能性)
  3. If it is not a single block, but indeed multiple lines highlighted or marked with some kind of HTML or CSS or JS, then there cant be an outward facing curve like this:
    enter image description here
    (there are always possibilities. For example you could cover the selection using a white rectangle with a border-radius, but this appears to be highly inefficient and unlikely... So...)
  4. 如果它不是单个块,但实际上有多个行突出显示或用某种HTML或CSS或JS标记,那么就不能像这样的向外曲线:(总有可能。例如,你可以使用一个带有边界半径的白色矩形,但这看起来非常低效且不太可能......所以...)

Summary, they must be using the Canvas property and whole lot of codes to 'underlay' an interactive selection procedure. There are numerous different types of highlighting appearing in the editor, like 'same word highlighting', 'selected highlighting', 'out of focus highlighting', etc... For all these to happen efficiently, I can't find a better alternative than canvas.

总结一下,他们必须使用Canvas属性和大量代码来“衬托”交互式选择过程。编辑器中出现了许多不同类型的突出显示,例如“同一个词突出显示”,“选择突出显示”,“聚焦突出显示”等...为了使所有这些有效发生,我找不到更好的选择比帆布。

Don't be mad at me for posting this. But I didn't want to see my 4 hours of research as a waste. At least I got an observation and that's that.

发布此内容不要生我的气。但我不想把我4小时的研究视为浪费。至少我得到了一个观察,就是这样。

UPDATE :

更新:

Though I thought covering the selection using a white rectangle with a border-radius at the end, is a rather inefficient and unnecessary way. Microsoft doesn't think so.

虽然我认为在末尾使用带有边框半径的白色矩形来覆盖选区,但这是一种相当低效且不必要的方式。微软并不这么认为。

They are using the curved edged rectangles to cover up the end of highlights to give that effect. They are using absolutely positioned, round-edged

s to give the effect of highlighting. And at the end of that
, they overlay an image of a rounded rectangle.

他们使用弯曲的边缘矩形来掩盖高光的末端以产生这种效果。他们使用绝对定位的圆角

来产生突出效果。在
的末尾,它们覆盖了圆角矩形的图像。

And kudos to them, they have done a great job with it.

对他们赞不绝口,他们做得很好。

#3


2  

CSS' ::selection only supports declaring color, background, cursor and outline (See W3C). So there's no possibility to define border-radius for the selection with pure CSS.

CSS':: selection仅支持声明颜色,背景,光标和轮廓(参见W3C)。因此,不可能使用纯CSS为选择定义border-radius。

So I believe they did it like Niklas mentioned in comments:

所以我相信他们就像尼克拉斯在评论中提到的那样:

  1. Wait until the user selects something (selectstart, combination of mousedown and mouseup)
  2. 等到用户选择一些东西(selectstart,mousedown和mouseup的组合)
  3. Get the selected text
  4. 获取所选文本
  5. Get the position of the selected text (number of characters from the beginning) since if you just double-click a single word, you can't create a rule from it
  6. 获取所选文本的位置(从头开始的字符数),因为如果您只是双击一个单词,则无法从中创建规则
  7. Wrap the selection with a div or span
  8. 用div或span包装选择
  9. Apply styles to the wrapper
  10. 将样式应用于包装器
  11. Listen for the user to click something else etc. (unselects text) -> remove wrapper
  12. 听取用户点击其他内容等(取消选择文本) - >删除包装器

I begun to try to create a solution myself, but I lost my motivation since it tooks too much time. Maybe someone could need my suggestions (I used jQuery):

我开始尝试自己创建一个解决方案,但由于花费了太多时间,我失去了动力。也许有人可能需要我的建议(我使用jQuery):

For point 2:

对于第2点:

var selection = (window.getSelection() // > IE 9 
                 || document.selection.createRange() //

For point 4 use replace()

对于第4点,使用replace()

For point 6:

对于第6点:

$(".selection").replaceWith($(".selection")[0].childNodes);

Fiddle

小提琴

#4


1  

They are actually using round edged rectangles to cover the end of highlights in sentences which are smaller than the preceding or succeeding lines (just as I said in point 2). Check this out yourself:

它们实际上使用圆边矩形来覆盖小于前一行或后一行的句子中的高光的结尾(正如我在第2点中所说的那样)。自己检查一下:

  1. You cannot inspect element directly from the iframe. So click somewhere else and navigate to the iframe. I did it using chromes' built in source code inspector.
  2. 您无法直接从iframe检查元素。所以点击其他地方并导航到iframe。我是使用chromes的内置源代码检查器完成的。
  3. Then use this image to find out the position of line highlighted in the image.
  4. 然后使用此图像找出图像中突出显示的线条的位置。
  5. That
    contains all the "selection" highlights. They just put round edged, background-colored rectangles below the text using absolute, top and left!!!**
  6. 包含所有“选择”亮点。他们只是使用绝对,顶部和左侧在文本下方放置圆形的背景色矩形!!! **
  7. The next
    holds similar background-colored
    s, only they are for highlighting focused word, similar words, etc...
  8. 下一个
    拥有类似背景色的
    s,只是用于突出显示重点词,类似词等...

enter image description here

This is actually the content of the iframe. See the #document at the top?

这实际上是iframe的内容。请参阅顶部的#document?

See the expanded view. The small space above having the code is actually the highlighted section.

请参阅展开的视图。上面有代码的小空间实际上是突出显示的部分。

enter image description here

This is not a very good idea for a simple website though. They really needed to parse and stuff with the words and letters, since it is supposed to be a high-end code editor, so cant blame them for spending a comparatively little time to 'round'ening the edges a little.

对于一个简单的网站来说,这不是一个好主意。他们真的需要解析和填充单词和字母,因为它应该是一个高端的代码编辑器,所以不能责怪他们花费相对较少的时间来“圆润”边缘。


推荐阅读
author-avatar
mobiledu2502853587
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有