在我的问题开头,我会说,我不是专门与"z“解决方案结婚的,但我不确定还有什么能奏效。
任务:附加一个掩蔽元素(固定的、不透明的.5等)在页面顶部,然后针对页面中的单个元素,并强制它在该掩码顶部显示。这样做的目的是突出显示集成到页面中的特定元素。
限制:没有提前定义要强制到顶部的元素,页面上的任何元素都应该是可行的目标。
目标元素的父元素将有几个可能的z-索引值,所有这些值都将低于掩码的z,并且都不能更改。递归地循环遍历所有的父母并设置临时css样式将无法工作,因为它会破坏进程中的许多其他东西。
只使用Css的解决方案更好,尽管需要一点js就可以了。
Ideas:我最有希望的想法是使用translateZ (或另一种3dTransform类型的样式)来强迫所有未转换的内容(包括掩码)顶部只有一个元素,但到目前为止,它还没有开始工作。
我还考虑过克隆目标元素,从目标收集位置和样式值,并在掩码顶部呈现克隆。这似乎有点笨拙(有点恶心),我不知道它是如何防弹。
到目前为止,我所读到的所有其他解决方案包括重新排序dom,纠正父级上的位置或z-索引样式,或者简单地向目标添加缺少的位置样式.这里没有一个是适用的解决方案。
发布于 2015-11-06 01:51:59
我选择了克隆选择,并有一个解决方案,我是卢克-温暖,但似乎是功能性的。下面是我放在一起创建克隆并定位克隆的方法:
var renderClonedEl = function($el, childrenToCopy) {
var $clone = $($el[0].cloneNode(true)),
offset = $el.offset(),
styles = window.getComputedStyle($el[0], null).cssText,
correction = {
left: parseInt($el.css('margin-left')) + parseInt($el.css('border-left-width')),
top: parseInt($el.css('margin-top')) + parseInt($el.css('border-top-width'))
},
$child, $cloneChild;
childrenToCopy = childrenToCopy || []; // Use array of selector strings to copy addional styles between children elements
$clone[0].style.cssText = styles;
for (var i = 0; i < childrenToCopy.length; i++) {
$child = $el.find(childrenToCopy[i]);
$cloneChild = $clone.find(childrenToCopy[i]);
_.each($cloneChild, function(item) {
item.style.cssText = window.getComputedStyle($child[0], null).cssText;
});
};
$clone.addClass('el-clone').appendTo('body').css({
top: offset.top - correction.top,
left: offset.left - correction.left
});
return $clone;
}..。用于克隆的其他助手样式:
.el-clone {
position: absolute;
z-index: 9999;
pointer-events: none;
}这样叫这个方法..。
var $newClone = renderClonedEl($('.el-to-clone'), ['.header > .title', '.header .btn']);我将原始el作为一个jQuery选择传递过来(因为我有可用的jQuery )。.offset()方法不考虑边距/填充/边框宽度,因此我正在对此进行调整。
childrenToCopy是一个选择字符串数组,我可以通过它额外地将计算过的样式从子元素复制到克隆中(当克隆包含许多子元素的复杂el时,其中一些元素正在丢失样式)。
我还会返回克隆,这样启动代码就可以在完成后正确地销毁它。
我有点撒了点jQuery和强调香草的东西.我刚好在这个项目中有两个库可供我使用。
发布于 2015-11-06 23:37:16
我非常害怕必须公开的克隆节点,因为可能会出现CSS选择器或事件处理程序中断。当然,如果你知道你的环境,如果你确信没有任何东西可以刹车,这可能是一个很好的方法。
但是,如果您感兴趣的元素恰好是简单的矩形,您可以非常安全地使用由四个较小的矩形组成的覆盖层()模糊其周围的。我做了一个快速的VanillaJS概念证明(点击周围):
function Obscurer() {
this.cont = document.createElement('div')
this.e1 = this.cont.appendChild(this.cont.cloneNode())
this.e1.className = 'cover'
this.e2 = this.cont.appendChild(this.e1.cloneNode())
this.e3 = this.cont.appendChild(this.e1.cloneNode())
this.e4 = this.cont.appendChild(this.e1.cloneNode())
}
Obscurer.prototype.obscureSurroundingsOf = function(el) {
if (el.parentNode === this.cont) {
document.body.removeChild(this.cont)
return
}
var b = el.getBoundingClientRect()
this.e1.style.width = b.left + b.width + 'px'
this.e1.style.height = b.top + 'px'
this.e2.style.left = this.e1.style.width
this.e2.style.height = b.top + b.height + 'px'
this.e3.style.top = this.e2.style.height
this.e3.style.left = b.left + 'px'
this.e4.style.top = b.top + 'px'
this.e4.style.width = b.left + 'px'
document.body.appendChild(this.cont)
}
var o = new Obscurer()
document.addEventListener('click', function(ev) {
o.obscureSurroundingsOf(ev.target)
}).cover {
position: absolute;
background-color: #000;
opacity: 0.5;
top: 0;
left: 0;
bottom: 0;
right: 0;
}<p>foo baz</p>
<p>baz <b>gazonk</b> <i>something</i></p>
<p>anything</p>
https://stackoverflow.com/questions/33556676
复制相似问题