javascript - 创建可以叠加图像的倒圆角箭头
问题描述
我正在寻找一种方法来创建如上所示的倒置指针。我已经看到了一百万种方法可以做到这一点,但使用的是直边而不是我需要的曲线。
它可以用 CSS 完成还是需要像 svg 之类的东西。我需要外部透明,这样我就可以做这样的事情并覆盖图像。
解决方案
这是对@Temani 解决方案的一个调整,它只使用background-size
and background-position
。
body {
background:pink;
}
.box {
height:200px;
position:relative;
}
.box:before,
.box:after{
content:"";
position:absolute;
z-index:-1;
width:50%;
top:0;
bottom:0;
background:url(https://picsum.photos/800/600?image=1069);
background-size: 200%;
}
.box:before {
left:0;
border-top-right-radius:20px;
background-position: left center;
}
.box:after {
right:0;
border-top-left-radius:20px;
background-position: right center;
}
<div class="box">
</div>
如果你想走 SVG 路线,你可以这样做:
body {
background:pink;
}
.box {
height: 200px;
}
<div class="box">
<svg width="100%" height="100%">
<defs>
<path id="notch" d="M -20,0 A 20,20 0 0 1 0,20 A 20,20 0 0 1 20,0 Z" fill="black"/>
<mask id="notchMask">
<rect width="100%" height="100%" fill="white"/>
<use xlink:href="#notch" x="50%"/>
</mask>
</defs>
<image xlink:href="https://picsum.photos/800/600?image=1069"
width="100%" height="100%" preserveAspectRatio="xMidYMid slice"
mask="url(#notchMask)"/>
</svg>
</div>
更新:
以前的 CSS 方法对于长内容会失败。这是一个新版本,希望适用于任何大小的内容。它需要添加几个额外的<div>
元素。
它的工作方式与以前类似,有两个半角元素。但是在每一半中,我们都嵌入了一个全角::before
元素。这样,背景图像可以设置为cover
. 我们只需要确保,对于右手边,我们使用right:0
的元素与背景图像是右对齐的。这意味着我们可以看到图像的两半。
body {
background: pink;
}
.box {
height: 1200px; /* Simulate long content */
position: relative;
}
.half {
position: absolute;
width: 50%;
top: 0;
bottom: 0;
border-top-right-radius: 20px;
overflow: hidden;
}
.half::before {
content: "";
position: absolute;
width: 200%;
left: 0;
top: 0;
bottom: 0;
background: url(https://picsum.photos/800/600?image=1069);
background-size: cover;
}
.half:nth-child(2) {
right: 0;
border-top-left-radius:20px;
}
.half:nth-child(2)::before {
left: auto;
right: 0;
}
<div class="box">
<div class="half"></div>
<div class="half"></div>
</div>
推荐阅读
- c - 如何从C中更大的json对象中提取一个json对象作为新的json
- ruby-on-rails - 获取孩子的所有家长协会
- html - Css3 动画在 Firefox 上不起作用(背景褪色)
- css - 这个 CSS 媒体屏幕如何针对分辨率宽度为 1080p 的手机?
- r - 在 R 中拆分数据框并将函数应用于每个部分
- rxjs - Rxjs - 组合 2 个流,仅在先前为 true 时发出
- javascript - 非数值总是出现
- html - Woocommerce - 将 CSS 类添加到商店页面中的图像
- java - AEM 6.1中的AMP(Accelerated Mobile Page)表单开发
- restart - 在 cockroachdb 中,为什么不确定性重新启动不更新不确定性窗口的上限?