css 的 clip-path 属性,绘制气泡

气泡组装

  • 气泡可以看做是由圆形和倒三角形组装起来的
  • 利用 clip-path 属性,组装两个元素

圆形

css 复制代码
width: 100px;
height: 100px;
clip-path: circle(50% at 50% 50%);
background: #ccc;

倒三角形

css 复制代码
width: 100px;
height: 100px;
clip-path: polygon(0% 0%, 100% 0%, 50% 100%, 0% 0%);
background: #ddd;

组合

  • 两个元素组合到一起,调整宽度与定位位置
html 复制代码
<div class="pop">
  <div class="circle"></div>
  <div class="trangle"></div>
</div>
<style lang="scss" scoped>

.pop {
  width: 100px;
  height: 120px;
  position: relative;
  .circle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    clip-path: circle(50% at 50% 50%);
    background: #ccc;
  }
  .trangle {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 30px;
    clip-path: polygon(0% 0%, 100% 0%, 50% 100%, 0% 0%);
    background: #ddd;
  }
}

</style>

边框效果

  • 用两个气泡叠加。下层气泡的尺寸略大于上层气泡的尺寸,形成边框效果
html 复制代码
<template>
  <div class="myPop">
    <div class="backBox">
      <div class="circle"></div>
      <div class="trangle"></div>
    </div>
    <div class="frontBox">
      <div class="circle"></div>
      <div class="trangle"></div>
    </div>
    <div class="msg">气泡</div>
  </div>
</template>
<style lang="scss" scoped>
.myPop {
  width: 102px;
  height: 122px;
  position: relative;
  & > div {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .circle {
    position: absolute;
    top: 0;
    left: 0;
    clip-path: circle(50% at 50% 50%);
  }
  .trangle {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    clip-path: polygon(0% 0%, 100% 0%, 50% 100%, 0% 0%);
  }
  .backBox {
    width: 102px;
    height: 122px;
    .circle {
      width: 102px;
      height: 102px;
      background: #4e700f;
    }
    .trangle {
      width: 62px;
      height: 30px;
      background: #4e700f;
      clip-path: polygon(0% 0%, 100% 0%, 50% 100%, 0% 0%);
    }
  }
  .frontBox {
    width: 100px;
    height: 120px;
    .circle {
      width: 100px;
      height: 100px;
      background: #d5f0a5;
    }
    .trangle {
      width: 60px;
      height: 30px;
      background: #d5f0a5;
    }
  }
  .msg {
    width: 100%;
    height: 100%;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
相关推荐
ashcn20012 小时前
水滴按钮解析
前端·javascript·css
Java陈序员5 小时前
告别手写礼簿!一款开源免费的电子红白喜事礼簿系统!
javascript·css·html
winfredzhang8 小时前
从零构建:基于 Node.js 的全栈视频资料管理系统开发实录
css·node.js·html·音视频·js·收藏,搜索,缩略图
加个鸡腿儿1 天前
经验分享2:SSR 项目中响应式组件的闪动陷阱与修复实践
前端·css·架构
华仔啊1 天前
写 CSS 用 px?这 3 个单位能让页面自动适配屏幕
前端·css
菩提小狗1 天前
Sqli-Labs Less-3 靶场完整解题流程解析-豆包生成
前端·css·less
web小白成长日记1 天前
CSS 作用域隔离实战:React、Vue 与 Styled Components 的三种范式
前端·css·vue.js·react.js
@@小旭2 天前
实现头部Sticky 粘性布局,并且点击菜单滑动到相应位置
前端·javascript·css
Irene19912 天前
CSS 定位属性(relative、absolute、fixed、sticky)与实用技巧总结
css
我的写法有点潮2 天前
推荐几个国外比较流行的UI库(上)
前端·javascript·css