前端 CSS 经典:图层放大的 hover 效果

效果

思路

设置 3 层元素,最上层元素使用 clip-path 裁剪成圆,hover 改变圆大小,添加过渡效果。

实现代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="initial-scale=1.0, user-scalable=no, width=device-width"
    />
    <title>document</title>
    <style>
      body {
        background: #000;
      }

      .avatar {
        background: url(../../../images/demo/img.jpg);
        background-size: cover;
        height: 200px;
        width: 200px;
        border-radius: 50%;
        cursor: pointer;
        position: relative;
      }

      .avatar::after,
      .avatar::before {
        content: "";
        position: absolute;
        inset: 0;
        border-radius: 50%;
      }

      .avatar::before {
        background: rgba(0, 0, 0, 0.5);
      }

      .avatar::after {
        background: inherit;
        clip-path: circle(0% at 50% 50%);
        transition: 0.3s;
      }

      .avatar:hover::after {
        clip-path: circle(50% at 50% 50%);
      }
    </style>
  </head>
  <body>
    <div class="avatar"></div>
    <script></script>
  </body>
</html>
相关推荐
云浪2 分钟前
元素变形记:CSS 缩放函数全指南
前端·css
明似水17 分钟前
用 Melos 解决 Flutter Monorepo 的依赖冲突:一个真实案例
前端·javascript·flutter
独立开阀者_FwtCoder27 分钟前
stagewise:让AI与代码编辑器无缝连接
前端·javascript·github
清沫29 分钟前
Cursor Rules 开发实践指南
前端·ai编程·cursor
江城开朗的豌豆34 分钟前
JavaScript篇:对象派 vs 过程派:编程江湖的两种武功心法
前端·javascript·面试
不吃糖葫芦336 分钟前
App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离
前端·webview
江城开朗的豌豆1 小时前
JavaScript篇:字母侦探:如何快速统计字符串里谁才是'主角'?
前端·javascript·面试
kite01217 小时前
浏览器工作原理06 [#]渲染流程(下):HTML、CSS和JavaScript是如何变成页面的
javascript·css·html
coding随想9 小时前
JavaScript ES6 解构:优雅提取数据的艺术
前端·javascript·es6
小小小小宇9 小时前
一个小小的柯里化函数
前端