做一个好看的首屏图片展示动画

前些日逛掘金,逛到了coco大佬的很炫酷的图片动画:现代 CSS 之高阶图片渐隐消失术 - 掘金 (juejin.cn)

利用 mask@property 做出图片像素化渐隐的效果实在是炫酷,正好现在没项目在摸鱼,脑子一抽开始用 nuxt3 写个人博客,正好首屏展示用得上 (又菜又爱玩)

定义网格

开始的想法是这样的,在大佬的代码基础上修改,通过代码获取到窗口宽高,然后通过 css变量 来动态修改行数列数,试了老半天也不太对,老是报错:

var(--w) is not a number

Undefined operation "calc(var(--w) * var(--h)) + 1"

也许一开始想用 js 去修改预处理器就有问题了🙃

正好又看油管一个大佬的图像碎片化,so,干脆用这个差不多的方法来代替 mask,做一个丐版图片渐隐,不需要 @propertyPaint 这种,兼容性还是蛮稳,也就一点点小卡

首先定义一下网格宽高,主要就是一层网格铺在图片前面,颜色跟背景色一致,后面再触发隐藏网格

js 复制代码
const centerBg = ref(null); // ref 获取到标签
const mask = ref(null); // ref 获取到标签
const row = ref(0); // 行数
const col = ref(0); // 列数
const len = ref(0); // 每个网格的大小
const initImg = () => {
  const imageWidth = window.innerWidth;
  const imageHeight = window.innerHeight;
  const maxGridCount =
    window.innerWidth > 1920
      ? 1000
      : window.innerWidth > 1080
      ? 800
      : window.innerWidth > 768
      ? 600
      : 400;
  // len 取宽高中较小的值,保证铺满
  len.value = Math.min(imageWidth, imageHeight) / Math.sqrt(maxGridCount);
  col.value = Math.floor(imageWidth / len.value);
  row.value = Math.floor(imageHeight / len.value);
};
const getCellStyle = (rowIndex, colIndex) => {
  return {
    width: `${len.value}px`,
    transition: `opacity ${100 + Math.random() * 500}ms ${
      ((colIndex + rowIndex / 2) / 20) * Math.random() * 500
    }ms`,
  };
};
html 复制代码
<template>
  <div
    class="centerBg relative w-screen h-screen overflow-hidden flex flex-col justify-center items-center gap-6"
    ref="centerBg"
  >
    <!-- mask -->
    <div class="mask absolute inset-0" ref="mask">
      <div v-for="rowIndex in row" :key="rowIndex" class="row flex">
        <div
          v-for="colIndex in col"
          :key="colIndex"
          class="col flex-1 aspect-square"
          :style="getCellStyle(rowIndex, colIndex)"
        ></div>
      </div>
    </div>
    <!-- title -->
    ...
    <!-- 简介 -->
    ...
    <!-- waves -->
    ...
  </div>
</template>

这段代码也没什么好解释的,len 是取的宽高较小的值为了保证能铺满,然后看下标签那部分,getCellStyle 里面的 transition 随便改,现在是从左往右带一些倾斜

修改 css

先把 css 的部分给写了,后面修改 --o 让网格隐藏就完工~

scss 复制代码
.centerBg {
  --o: 1;
  --img: url("assets/images/0.png");
  background: var(--img) center center/cover;
  .col {
    background: var(--bg); // 背景色
    opacity: var(--o);
  }
}
.dark .centerBg {
  --img: url("assets/images/_0.png");
}

触发动画

在完成定义网格和修改样式后,可以用个延时,或者正好可以等图片加载完在触发,用 Image 对象的 decode() 等,不然直接触发是没有效果的

js 复制代码
onMounted(() => {
  initImg();
  setTimeout(()=>{
    centerBg.value.style.setProperty("--o", 0)
  });
});

几百个 div 的动画,卡不卡就另说了 🤔

相关推荐
不会敲代码113 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Sailing16 小时前
🚀 别再乱写 16px 了!CSS 单位体系已经进入“计算时代”,真正的响应式布局
前端·css·面试
球球pick小樱花2 天前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
AAA阿giao2 天前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
掘金安东尼3 天前
用 CSS 打造完美的饼图
前端·css
掘金安东尼3 天前
纯 CSS 实现弹性文字效果
前端·css
前端Hardy4 天前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy4 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
parade岁月5 天前
Tailwind CSS v4 — 当框架猜不透你的心思
前端·css
前端Hardy5 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html