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.)
(在某些情况下,边界仍然可以使用工作。)
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!
5
I can assure you this has nothing to do with html, css border radius or highlighting. The proof?
我可以向你保证,这与html,css边框半径或突出显示无关。证据?
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 他们使用弯曲的边缘矩形来掩盖高光的末端以产生这种效果。他们使用绝对定位的圆角 And kudos to them, they have done a great job with it. 对他们赞不绝口,他们做得很好。 2 CSS' CSS':: selection仅支持声明颜色,背景,光标和轮廓(参见W3C)。因此,不可能使用纯CSS为选择定义border-radius。 So I believe they did it like Niklas mentioned in comments: 所以我相信他们就像尼克拉斯在评论中提到的那样: 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点: For point 4 use replace() 对于第4点,使用replace() For point 6: 对于第6点: Fiddle 小提琴 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点中所说的那样)。自己检查一下: This is actually the content of the iframe. See the 这实际上是iframe的内容。请参阅顶部的#document? See the expanded view. The small space above having the code is actually the highlighted section. 请参阅展开的视图。上面有代码的小空间实际上是突出显示的部分。 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. 对于一个简单的网站来说,这不是一个好主意。他们真的需要解析和填充单词和字母,因为它应该是一个高端的代码编辑器,所以不能责怪他们花费相对较少的时间来“圆润”边缘。#3
::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.
var selection = (window.getSelection() // > IE 9
|| document.selection.createRange() //
$(".selection").replaceWith($(".selection")[0].childNodes);
#4
#document
at the top?
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有