简体   繁体   English

使用SVG rect作为灯箱,整个页面都带有切口吗?

[英]Use SVG rect as lightbox with cutout for the whole page?

I am currently trying to use an SVG to highlight a certain element on a page. 我目前正在尝试使用SVG突出显示页面上的某个元素。 For that I use an SVG rect as overlay and a mask as cutout. 为此,我使用SVG rect作为覆盖层,并使用遮罩作为切口。

Now my problem starts with scrolling, the SVG rect is only drawn in the current viewport. 现在,我的问题从滚动开始,SVG rect仅在当前视口中绘制。 When scrolling down the overlay is not shown. 向下滚动时,不会显示叠加层。

I can't use position: fixed as the cutout would also scroll. 我不能使用position: fixed因为切口也会滚动。 For structural reasons on our system I can't use a simple div solution. 由于我们系统上的结构原因,我无法使用简单的div解决方案。 After resizing the browser window it is drawn the whole length. 调整浏览器窗口的大小后,将绘制整个长度。

Is it possible to force the svg to be drawn for the whole page height? 是否可以强制在整个页面高度上绘制svg

 .looong { height: 3000px; } #cutout { box-sizing: content-box; position: absolute; z-index: 9999997; background-color: #FFF; background-color: rgba(255,255,255,.9); border: 1px solid rgba(0,0,0,.5); border-radius: 4px; box-shadow: 0 2px 15px rgba(0,0,0,.4); transition: all .3s ease-out; } #overlay { position: absolute; top: 0; left: 0; width:100%; height:100%; z-index: 9999997; } 
 <div class="looong"> Some Text, Big div </div> <svg id="overlay"> <defs> <mask id="overlay-mask"> <rect x="0" y="0" width="100%" height="100%" fill="white"></rect> <rect id="cutout" x="100" y="200" width="100" height="50" fill="black"></rect> </mask> </defs> <rect x="0" y="0" width="100%" height="100%" fill="black" fill-opacity="0.6" mask="url(#overlay-mask)" ></rect> </svg> 

The problem 问题

An absolutely positioned box is placed in respect to its nearest positioned container. 相对于其最近放置的容器放置一个绝对放置的盒子。 If a positioned ancestor does not exist the box is instead positioned relative to the "initial containing block". 如果不存在定位的祖先,则将框相对于“初始包含块”定位。

The "initial containing block" has the dimensions of the viewport so height: 100%; “初始包含块”具有视口的尺寸,因此height: 100%; on #outer is therefore equal to the height of the viewport. 因此,在#outer上的高度等于视口的高度。

For more information on the initial containing block see https://www.w3.org/TR/CSS22/visudet.html#containing-block-details . 有关初始包含块的更多信息,请参见https://www.w3.org/TR/CSS22/visudet.html# contains- block- details

The solution 解决方案

You can achieve the desired result with the following changes: 您可以通过以下更改获得所需的结果:

  • Add a html selector with the style position: relative; 添加一个样式为html选择器position: relative; . This will position #overlay relatively to it making its height: 100%; 这将使#overlay相对于其height: 100%; fill the full height of the html element 填充html元素的整个高度

 html { position: relative; } .looong { height: 3000px; } #cutout { box-sizing: content-box; position: absolute; z-index: 9999997; background-color: #FFF; background-color: rgba(255, 255, 255, .9); border: 1px solid rgba(0, 0, 0, .5); border-radius: 4px; box-shadow: 0 2px 15px rgba(0, 0, 0, .4); transition: all .3s ease-out; } #overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 9999997; } 
 <div class="looong"> Some Text, Big div </div> <svg id="overlay"> <defs> <mask id="overlay-mask"> <rect x="0" y="0" width="100%" height="100%" fill="white"></rect> <rect id="cutout" x="100" y="200" width="100" height="50" fill="black"></rect> </mask> </defs> <rect x="0" y="0" width="100%" height="100%" fill="black" fill-opacity="0.6" mask="url(#overlay-mask)" ></rect> </svg> 

Try this 尝试这个

 .full-body{ position: relative; margin: -10px; } .looong { height: 3000px; padding: 10px; } #cutout { box-sizing: content-box; position: absolute; z-index: 9999997; background-color: #FFF; background-color: rgba(255,255,255,.9); border: 1px solid rgba(0,0,0,.5); border-radius: 4px; box-shadow: 0 2px 15px rgba(0,0,0,.4); transition: all .3s ease-out; } #overlay { position: absolute; top: 0; left: 0; width:100%; height:100%; z-index: 9999997; } 
 <div class="full-body"> <div class="looong"> Some Text, Big div </div> <svg id="overlay"> <defs> <mask id="overlay-mask"> <rect x="0" y="0" width="100%" height="100%" fill="white"></rect> <rect id="cutout" x="100" y="200" width="100" height="50" fill="black"></rect> </mask> </defs> <rect x="0" y="0" width="100%" height="100%" fill="black" fill-opacity="0.6" mask="url(#overlay-mask)" ></rect> </svg> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM