画梯形我看行

背景

最近业务有需求需要开发一个梯形的切换标题,当时想着梯形也不是啥奇形怪状的图形,不分分钟拿下。但是仔细看了设计稿感觉还是有挺多细节的。

  1. 梯形之间相互重叠
  2. 梯形拥有自己的边框和内部阴影
  3. 左边梯形是直角梯形
  4. 点击后背景颜色需要变更
  5. 底部边框需要隐藏

实现

这样就可以等到一个宽度300,高度100的等腰梯形了

css 复制代码
.trapezoid {
  width: 200px;
  height: 0;
  border-bottom: 100px solid red;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
}

百度还发现一个有意思的属性也可以实现一个等腰梯形

css 复制代码
.trapezoid {
  width: 200px;
  height: 100px;
  background-color: red;
  clip-path: polygon( 25% 0%, 75% 0, 100% 100%,0 100%);
}

但是上面2种方式都没办法满足条件2,一旦实现边框或者需要实现内阴影这个需求,都不得劲;

最后找到一个以前没怎么用到的属性解决了

css 复制代码
.toggle-title {
  position: relative;
  padding: 0;
  text-decoration: none;
  display: inline-block;
  width: 150px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: 18px;
  color: #099aab;
  font-weight: bold;
  cursor: pointer;
}

.toggle-title::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 1px solid #7fcbd3;
  border-bottom: none;
  transform: perspective(1em) scale(1.02, 1.3) rotateX(5deg);
  z-index: -1;
  background: #c1e6ea;
  box-shadow: 0 0 10px #7fcbd3 inset;
  transform-origin: bottom;
  border-radius: 5px 5px 0 0;
  cursor: pointer;
}

相对来说基本手写css就实现了,重点核心就在于perspective这个属性,MDN或者看看文章学习下,发现一个是控制元素纵深效果的一个属性,确实厉害,css真的永远都学不完。

相关推荐
COOLMO研究AI2 小时前
企业官网服务页的信息架构怎么设计:从用户问题到语义化 HTML
前端·css·html
DsirNg16 小时前
断点不该认识设备:组件布局的容器状态治理
css·前端工程化·组件化·响应式布局·css grid·设计系统·container queries
溪语流沙1 天前
【每天一个CSS | Day11】赛博朋克故障风文字:红青分离 + 切片抖动
前端·css
utmhikari2 天前
【AI原生】借助AI之力优化个人博客前端样式
前端·css·hexo·next·个人博客·ai-native·vibe coding
两个西柚呀2 天前
JS事件总结
前端·javascript·css
ss2733 天前
AI全栈实战 | 2.1-02 CSS 布局:垂直居中为什么是经典面试题?所有布局在解决什么矛盾
前端·css
peter67683 天前
css揭秘-背景和边框
前端·css
溪语流沙4 天前
【每天一个CSS | Day08】整排按钮被鼠标吸走的磁吸导航
前端·css·计算机外设
peter67684 天前
css学习小结
前端·css·学习
liangshanbo12154 天前
什么是CSS原子化?它的优劣势是什么?
前端·css·tensorflow·css原子化