图像剪裁
代码
CSS代码:
.clip-img-x { display: inline-block; overflow: hidden; position: relative; } .clip-img { display: block; } .clip-shape { width: 150px; height: 150px; outline: 9999px solid rgba(0,0,0,.5); position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; cursor: move; }
HTML代码:
<div class="clip-img-x"> <div class="clip-shape"></div> <img src="./mm.jpg" class="clip-img"> </div>
实现的关键就是使用巨大尺寸的outline
模拟镂空。
新手引导
代码
CSS代码:
.guide-x { text-align: center; padding: 100px 16px; background-color: #fff; position: relative; overflow: hidden; } .guide-overlay { position: absolute; left: 0; top: 0; right: 0; bottom: 0; background: linear-gradient(transparent, transparent); } .guide-overlay-shape { width: 100px; height: 60px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; border-radius: 50%; box-shadow: 0 0 0 9999px rgba(0,0,0,.75); }
HTML代码:
<div class="guide-x"> <div class="guide-overlay"> <i class="guide-overlay-shape" data-content="发布按钮搬到这里了"></i> </div> <button class="button">发布</button> </div>
实现的关键就是使用巨大尺寸的box-shadow
扩展模拟镂空。
也可以使用径向渐变实现,但没有box-shadow
好理解好上手。
外圆角选项卡
代码
CSS代码:
.tab-x { display: flex; flex-direction: row-reverse; justify-content: flex-end; padding-left: 20px; border-bottom: 1px solid rgba(0,0,0,.1); } .tab-a { --backgroundColor: #fff; background-color: var(--backgroundColor); line-height: 20px; padding: 10px 20px; border-radius: 16px 16px 0 0; filter: drop-shadow(0 -1px 1px rgba(0,0,0,.1)); position: relative; } /* 创建外侧圆弧 */ .tab-a::before, .tab-a::after { content: ''; position: absolute; bottom: 0; width: 16px; height: 16px; } .tab-a::before { background: radial-gradient(circle at 0 0, transparent, transparent 16px, var(--backgroundColor) 17px); right: 100%; } .tab-a::after { background: radial-gradient(circle at 100% 0, transparent, transparent 16px, var(--backgroundColor) 17px); left: 100%; } .tab-a.active { --backgroundColor: teal; color: #fff; z-index: 1; }
HTML代码:
<div class="tab-x"> <a href="javascript:" class="tab-a">选项卡3</a> <a href="javascript:" class="tab-a">选项卡2</a> <a href="javascript:" class="tab-a active">选项卡1</a> </div>
这里外部的圆角使用径向渐变实现。我们也可以使用box-shadow
实现类似的效果,如下:
代码如下:
<div class="quar-radius"></div>
.quar-radius { width: 96px; height: 96px; border-radius: 0 0 50% 0; clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); box-shadow: 0 0 0 100px teal; }
任意图形镂空
黑色黑色半透明遮罩,中间镂空的是不规则图形,例如五角星:
代码
CSS代码:
.shape-hollow-x { width: 600px; max-width: 100%; height: 300px; background: linear-gradient(red, green); position: relative; } .shape-hollow { position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; background-color: rgba(0,0,0,.75); /* 实际开发需要-webkit- */ mask: no-repeat center; mask-image: linear-gradient(black, black), url(./star.svg); mask-size: cover, 120px 120px; mask-composite: exclude; mask-composite: source-out; }
HTML代码:
<div class="shape-hollow-x"> <i class="shape-hollow"></i> </div>
关于mask遮罩的详细介绍:https://www.zhangxinxu.com/wordpress/?p=6513
拓展:新颖的过场动效
img.shape-hollow { /* 动画 */ animation: starIn 2s infinite; } @keyframes starIn { from { -webkit-mask-size: 100%, 0 0; mask-size: 100%, 0 0; } to { -webkit-mask-size: 100%, 300% 300%; mask-size: 100%, 300% 300%; } }