当前位置: 首页 > article >正文

旋转的错觉

像这种旋转动画怎么实现呢?

  1. 首先先实现布局:布局的话可以采用 Grid 布局,通过给不同元素设置不同的分区实现下图

.container {
  width: 300px;
  height: 300px;
  margin: 0 auto;
  margin-top: 100px;
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  grid-template:
    'a a b'
    'c e b'
    'c d d';
  gap: 5px;

  .item {
    overflow: hidden;
    border: 1px solid #ddd;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    &:nth-of-type(1) {
      grid-area: a;
    }

    &:nth-of-type(2) {
      grid-area: b;
    }

    &:nth-of-type(3) {
      grid-area: c;
    }

    &:nth-of-type(4) {
      grid-area: d;
    }

    &:nth-of-type(5) {
      grid-area: e;
    }
  }
}

@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
  1. 再进行动画处理,这里动画分两部分处理
    1. 一是外层容器顺时针旋转
    2. 二是里层的图片逆时针旋转

.container {
  animation: rotation 10s infinite linear;
  .item { 
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      animation: rotation 10s infinite linear reverse;
    }
  }
}
@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

经过上面两步会发现图片旋转是实现了,是会有白边。这是因为图片太小,在旋转过程中不够铺满外层容器,处理方法就是放大图片就行喽。

img {
  width: 210%;
  height: 210%;
  object-fit: cover;
  animation: rotation 10s infinite linear reverse;
}

完整代码如下:

import React from 'react';
import img1 from './image/3.jpg';
import img2 from './image/5.jpg';
import img3 from './image/2.jpg';
import img4 from './image/4.jpg';
import img5 from './image/1.jpg';
import './index.less'

export default function index() {
  return (
    <div className="container">
      <div className="item">
        <img src={img1} alt="" />
      </div>
      <div className="item">
        <img src={img2} alt="" />
      </div>
      <div className="item">
        <img src={img3} alt="" />
      </div>
      <div className="item">
        <img src={img4} alt="" />
      </div>
      <div className="item">
        <img src={img5} alt="" />
      </div>
    </div>
  );
}
.container {
  width: 300px;
  height: 300px;
  margin: 0 auto;
  margin-top: 100px;
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  grid-template:
    'a a b'
    'c e b'
    'c d d';
  gap: 5px;
  animation: rotation 10s infinite linear;

  .item {
    overflow: hidden;
    border: 1px solid #ddd;
    display: flex;
    justify-content: center;
    align-items: center;
    
    img {
      width: 210%;
      height: 210%;
      object-fit: cover;
      animation: rotation 10s infinite linear reverse;
    }

    &:nth-of-type(1) {
      grid-area: a;
    }

    &:nth-of-type(2) {
      grid-area: b;
    }

    &:nth-of-type(3) {
      grid-area: c;
    }

    &:nth-of-type(4) {
      grid-area: d;
    }

    &:nth-of-type(5) {
      grid-area: e;
    }
  }
}

@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

http://www.kler.cn/a/520177.html

相关文章:

  • Java Web-Tomcat Servlet
  • 运用python进行多任务学习过程中,手动调整权重时,如何选择项目并确定合适的权重值?
  • 【四川乡镇界面】图层shp格式arcgis数据乡镇名称和编码2020年wgs84无偏移内容测评
  • Spring Boot 支持的日志框架解析与最佳实践
  • day6手机摄影社区,可以去苹果摄影社区学习拍摄技巧
  • 力扣动态规划-10【算法学习day.104】
  • Couchbase UI: Logs
  • 蓝桥杯真题 - 三国游戏 - 题解
  • vue3中suspense的用法以及使用场景
  • Synology 群辉NAS安装(6)安装mssql
  • decison tree 决策树
  • 分公司-国内外大公司-github等开源项目推荐笔记
  • 【Postgres_Python】使用python脚本批量创建和导入多个PG数据库
  • 巴菲特购买比特币
  • SpringBoot+Electron教务管理系统 附带详细运行指导视频
  • 视频多模态模型——视频版ViT
  • C++小病毒-1.0勒索(更新次数:2)
  • WPF实战案例 | C# WPF实现大学选课系统
  • salesforce 可以 outbound profile 吗
  • Mac 上如何同时运行多个MySQL版本?