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>
相关推荐
搬砖的阿wei1 天前
CSS常用选择器总结
前端·css
RFCEO1 天前
前端编程 课程十二、:CSS 基础应用 Flex 布局
前端·css·flex 布局·css3原生自带的布局模块·flexible box·弹性盒布局·垂直居中困难
ziblog1 天前
HTML5 Canvas梦幻背景动画特效
前端·css
weixin_456907412 天前
2026+:html+css 生态的成型之年与平台化跃迁
前端·css·html
2301_780669862 天前
HTML-CSS-常见标签和样式(标题的排版、标题的样式、选择器、正文的排版、正文的样式、整体布局、盒子模型)
前端·css·html·javaweb
weixin_456907412 天前
CSS DSF.soolCXZ LsoolbDSF:html 中 doos() 的 Copy-goos-Prite 实现实验笔记
css·笔记·html
咕噜咕噜啦啦2 天前
CSS3基础
前端·css·css3
⑩-2 天前
HTML期末课设作业
css·html
H_z_q24013 天前
Web前端制作一个评论发布案例
前端·javascript·css
西红市杰出青年3 天前
CSS 选择器详细教程:原理、语法、方向/“轴”与实战
css·python