当前位置:首页 > JavaScript

js实现3d图片轮播

2026-03-01 08:03:20JavaScript

实现3D图片轮播的方法

使用JavaScript和CSS 3D变换可以创建3D图片轮播效果。以下是实现方法:

基本HTML结构

<div class="carousel-container">
  <div class="carousel">
    <img src="image1.jpg" alt="Image 1">
    <img src="image2.jpg" alt="Image 2">
    <img src="image3.jpg" alt="Image 3">
    <img src="image4.jpg" alt="Image 4">
  </div>
</div>
<button class="prev">Previous</button>
<button class="next">Next</button>

CSS样式设置

.carousel-container {
  perspective: 1000px;
  width: 500px;
  height: 300px;
  margin: 0 auto;
}

.carousel {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 1s;
}

.carousel img {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  box-shadow: 0 0 10px rgba(0,0,0,0.5);
}

JavaScript实现逻辑

const carousel = document.querySelector('.carousel');
const images = document.querySelectorAll('.carousel img');
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');

let currentIndex = 0;
const totalImages = images.length;
const angle = 360 / totalImages;

// 初始化3D布局
function setupCarousel() {
  images.forEach((img, index) => {
    const rotateY = angle * index;
    img.style.transform = `rotateY(${rotateY}deg) translateZ(250px)`;
  });
}

// 旋转到下一张
function nextSlide() {
  currentIndex = (currentIndex + 1) % totalImages;
  rotateCarousel();
}

// 旋转到上一张
function prevSlide() {
  currentIndex = (currentIndex - 1 + totalImages) % totalImages;
  rotateCarousel();
}

// 执行旋转
function rotateCarousel() {
  const rotateY = -angle * currentIndex;
  carousel.style.transform = `rotateY(${rotateY}deg)`;
}

// 事件监听
nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);

// 初始化
setupCarousel();

增强交互效果

添加自动播放和悬停暂停功能:

let autoPlayInterval;

function startAutoPlay() {
  autoPlayInterval = setInterval(nextSlide, 3000);
}

function stopAutoPlay() {
  clearInterval(autoPlayInterval);
}

// 鼠标悬停暂停
carousel.addEventListener('mouseenter', stopAutoPlay);
carousel.addEventListener('mouseleave', startAutoPlay);

// 开始自动播放
startAutoPlay();

响应式调整

添加窗口大小变化时的调整:

js实现3d图片轮播

function adjustCarousel() {
  const containerWidth = document.querySelector('.carousel-container').offsetWidth;
  const translateZ = containerWidth / 2 / Math.tan(Math.PI / totalImages);

  images.forEach(img => {
    const currentRotateY = img.style.transform.match(/rotateY\((\d+)deg\)/)[1];
    img.style.transform = `rotateY(${currentRotateY}deg) translateZ(${translateZ}px)`;
  });
}

window.addEventListener('resize', adjustCarousel);

这个实现创建了一个3D轮播效果,图片围绕Y轴旋转,形成环形排列。可以通过按钮控制旋转方向,也支持自动播放和响应式调整。

标签: 图片js
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swipe…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

vue图片实现多选

vue图片实现多选

Vue 图片多选实现方法 在 Vue 中实现图片多选功能可以通过以下几种方式完成,具体取决于项目需求和复杂度。 使用 v-model 和数组存储选中状态 通过 v-model 绑定一个数组来记录选中…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js实现 功能

js实现 功能

在 JavaScript 中实现功能通常涉及多个关键步骤,具体取决于功能需求。以下是常见功能的实现方法和示例: 基本功能实现 使用函数封装逻辑是最直接的方式。例如,实现一个计算两数之和的函数: f…

js实现轮播代码

js实现轮播代码

基础轮播实现 使用HTML、CSS和JavaScript创建一个简单的轮播效果。HTML部分定义轮播容器和图片元素。 <div class="carousel"> <div c…