画梯形我看行

背景

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

  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真的永远都学不完。

相关推荐
冰暮流星16 小时前
css新增盒子属性——尺寸调节
前端·css
Predestination王瀞潞2 天前
WEB前端技术基础(第三章:css-网页美化的衣装-上)
前端·css
冰暮流星2 天前
css之box-sizing属性
前端·javascript·css
倚肆2 天前
CSS ::before 和 ::after 伪元素详解
前端·css
小九今天不码代码2 天前
巧用 CSS linear-gradient 实现多种下划线文字特效(纯 CSS 无需额外标签)
css·css3·前端开发·linear-gradient·前端特效·下划线样式·文字特效
泷羽Sec-静安2 天前
Less-1 GET-Error based-Single quotes-String GET-基于错误-单引号-字符串
前端·css·网络·sql·安全·web安全·less
一只学java的小汉堡2 天前
HTML 01入门:从概念到开发环境搭建与页面头部配置
前端·css·html
九皇叔叔3 天前
HTML与CSS基础入门:从语法到页面结构全解析
css·html·1024程序员节
_殊途3 天前
HTML-CSS项目练习
前端·css·html
β添砖java3 天前
vivo响应式官网
前端·css·html·1024程序员节