7. 旋转切换的轮播图

文章介绍了如何使用HTML和CSS3(包括SASS变量和关键帧动画)创建一个可响应的轮播图,图片会按照一定角度旋转展示。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>旋转切换的轮播图</title>
    <style>
      body {
        background: #000;
      }

      .container {
        width: 200px;
        height: 200px;
        border-radius: 50%;
        outline: 5px solid #fff;
        margin: 50px auto;
        display: flex;
        justify-content: center;
        overflow: hidden;
      }

      .inner {
        width: 400px;
        height: 400px;
        border-radius: 50%;
        flex-shrink: 0;
        margin-top: 100px;
        position: relative;
      }
      .inner img {
        width: 200px;
        height: 200px;
        border-radius: 50%;
        position: absolute;
        margin-left: 100px;
        margin-top: -100px;
        transform-origin: center 300px;
      }
      .inner img:nth-child(1) {
        transform: rotate(0deg);
      }
      .inner img:nth-child(2) {
        transform: rotate(60deg);
      }
      .inner img:nth-child(3) {
        transform: rotate(120deg);
      }
      .inner img:nth-child(4) {
        transform: rotate(180deg);
      }
      .inner img:nth-child(5) {
        transform: rotate(240deg);
      }
      .inner img:nth-child(6) {
        transform: rotate(300deg);
      }

      @keyframes rotation {
        12.8205128205%,
        16.6666666667% {
          transform: rotate(-60deg);
        }
        29.4871794872%,
        33.3333333333% {
          transform: rotate(-120deg);
        }
        46.1538461538%,
        50% {
          transform: rotate(-180deg);
        }
        62.8205128205%,
        66.6666666667% {
          transform: rotate(-240deg);
        }
        79.4871794872%,
        83.3333333333% {
          transform: rotate(-300deg);
        }
        96.1538461538%,
        100% {
          transform: rotate(-360deg);
        }
      }
      .inner {
        animation: rotation 7.8s ease-in infinite;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="inner">
        <img src="https://picsum.photos/id/51/600" />
        <img src="https://picsum.photos/id/52/600" />
        <img src="https://picsum.photos/id/53/600" />
        <img src="https://picsum.photos/id/54/600" />
        <img src="https://picsum.photos/id/55/600" />
        <img src="https://picsum.photos/id/56/600" />
      </div>
    </div>
  </body>
</html>

<!--
@use 'sass:math';

body {
  background: #000;
}

$size: 200px; //大小
$n: 6; // 图片个数
$pDeg: 360deg / $n; //每个扇形的角度
$r: $size/2;
$R: $r/math.sin($pDeg/2);
$innerSize: $R * 2;

.container {
  width: $size;
  height: $size;
  border-radius: 50%;
  outline: 5px solid #fff;
  margin: 50px auto;
  display: flex;
  justify-content: center;
  overflow: hidden;
}

.inner {
  width: $innerSize;
  height: $innerSize;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: $r;
  position: relative;
  img {
    width: $size;
    height: $size;
    border-radius: 50%;
    position: absolute;
    margin-left: $r;
    margin-top: -$r;
    transform-origin: center #{$R + $r};
    @for $i from 1 through $n {
      &:nth-child(#{$i}) {
        transform: rotate($pDeg * ($i - 1));
      }
    }
  }
}

$u: 1 / $n * 100%; //动画每个阶段经历的时间百分比
$rotateDuration: 0.3s; //每张图片旋转的时间
$stopDuration: 1s; //每张图片停留的时间
$duration: ($rotateDuration + $stopDuration) * $n;
$backPercent: $rotateDuration/($rotateDuration + $stopDuration) * $u;
@keyframes rotation {
  @for $i from 1 through $n {
    $p: $u * $i;
    $deg: $pDeg * $i;
    #{$p - $backPercent},
    #{$p} {
      transform: rotate(-$deg);
    }
  }
}

.inner {
  animation: rotation $duration ease-in infinite;
}
-->
`three.js` 是一个用于创建和展示 3D 和 2D 图形的 JavaScript 库。要实现一个基于 `three.js` 的动态轮播图,我们可以利用其强大的三维图形渲染能力来模拟各种复杂的动画效果。 ### 基本概念 在 `three.js` 中构建动态轮播图的主要组成部分包括: 1. **场景 (`Scene`)** - 包含所有对象(如几何体、材质、灯光等)的集合。 2. **相机 (`Camera`)** - 观察场景的角度。 3. **渲染器 (`Renderer`)** - 实际绘制场景到屏幕上的组件。 4. **模型/几何体 (`Geometry`)** - 描述物体形状的对象。 5. **材质 (`Material`)** - 给几何体赋予外观的属性,比如颜色、纹理等。 ### 示例概述 假设我们要制作一个简单的动态轮播图,其中包含三个不同的圆环模型,在轮播过程中围绕中心旋转并变换大小。 #### 步骤 1: 设置基本环境 ```javascript // 初始化 scene, camera, renderer const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 添加背景色 scene.background = new THREE.Color(0x000000); // 黑色背景 // 创建一个可调节大小的圆环 function createRing(size) { const geometry = new THREE.TorusGeometry(size, size / 20, 40, 20); const material = new THREE.MeshBasicMaterial({ color: 0xffffff }); return new THREE.Mesh(geometry, material); } const rings = [ createRing(5), createRing(10), createRing(15) ]; rings.forEach(ring => { ring.rotation.x = Math.random() * Math.PI; ring.position.z = Math.random() * 10 + 1; // 随机位置 }); // 添加模型到场景 rings.forEach(ring => scene.add(ring)); // 控制动画循环播放 let animationIndex = 0; const animate = () => { requestAnimationFrame(animate); if (animationIndex >= rings.length) animationIndex = 0; // 更新模型大小和位置 rings[animationIndex].scale.set( Math.sin(animationIndex / 3 * Math.PI) * 0.2, Math.sin(animationIndex / 3 * Math.PI) * 0.2, Math.sin(animationIndex / 3 * Math.PI) * 0.2 ); rings[animationIndex].position.y += 0.1; // 下降动画 rings[animationIndex].rotation.y -= 0.01; // 旋转动画 // 移动到下一个模型 animationIndex++; renderer.render(scene, camera); }; animate(); ``` ### 相关问题: 1. 如何调整上述代码以改变轮播图的动画速度? 2. 是否能添加更多的交互性,如用户点击切换下一张图片? 3. 怎样优化这个代码,使其在不同设备上都能流畅运行?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值