You need to enable JavaScript to run this app. 如何改造3D轮播代码实现固定4项显示的Bootstrap轮播
优惠活动
大模型
产品
解决方案
定价
更多

如何改造3D轮播代码实现固定4项显示的Bootstrap轮播

4项目固定展示3D轮播(可适配Bootstrap)实现

之前修改出现显示异常的根因

  • 角度规则错配:4个单元格的环形布局单格旋转间隔应为360°/4=90°,原修改中第4个单元格设置为旋转360°,和第1个单元格位置完全重叠,无法形成独立的四棱柱结构
  • 景深参数不匹配:轮播容器默认的初始偏移是translateZ(-288px),单独把单元格的translateZ改成128px,会导致环形半径和容器景深计算错位,单元格偏移出可视区
  • 残留旧规则未清理:原本适配9个单元格的背景规则、第5个及后续单元格的定位规则没有删除,会额外渲染出位置错误的元素
  • 逻辑边界缺失:设置cellCount=4后,没有补全按钮的类名绑定、循环边界判断,导致交互失效

完整可运行实现

对应样式代码

* {
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
}
.scene {
  border: 1px solid #CCC;
  margin: 40px auto;
  position: relative;
  width: 210px;
  height: 140px;
  perspective: 600px;
}
.carousel {
  width: 100%;
  height: 100%;
  position: absolute;
  transform: translateZ(-300px);
  transform-style: preserve-3d;
  transition: transform 1s;
}
.carousel__cell {
  position: absolute;
  width: 190px;
  height: 120px;
  left: 10px;
  top: 10px;
  border: 2px solid black;
  line-height: 116px;
  font-size: 80px;
  font-weight: bold;
  color: white;
  text-align: center;
  backface-visibility: hidden;
}
/* 替换原9单元格背景规则 */
.carousel__cell:nth-child(1) { background: hsla(0, 100%, 50%, 0.8); }
.carousel__cell:nth-child(2) { background: hsla(90, 100%, 50%, 0.8); }
.carousel__cell:nth-child(3) { background: hsla(180, 100%, 50%, 0.8); }
.carousel__cell:nth-child(4) { background: hsla(270, 100%, 50%, 0.8); }
/* 4单元格环形定位,统一300px环形半径 */
.carousel__cell:nth-child(1) { transform: rotateY(0deg) translateZ(300px); }
.carousel__cell:nth-child(2) { transform: rotateY(90deg) translateZ(300px); }
.carousel__cell:nth-child(3) { transform: rotateY(180deg) translateZ(300px); }
.carousel__cell:nth-child(4) { transform: rotateY(270deg) translateZ(300px); }

对应结构代码

<!-- 轮播可视区域 -->
<div class="scene">
  <div class="carousel">
    <div class="carousel__cell">1</div>
    <div class="carousel__cell">2</div>
    <div class="carousel__cell">3</div>
    <div class="carousel__cell">4</div>
  </div>
</div>
<!-- 控制按钮 -->
<button class="previous-button">Previous</button>
<button class="next-button">Next</button>

对应交互代码

const carousel = document.querySelector('.carousel');
const cellCount = 4;
let selectedIndex = 0;

function rotateCarousel() {
  const angle = selectedIndex * (360 / cellCount) * -1;
  carousel.style.transform = `translateZ(-300px) rotateY(${angle}deg)`;
}

// 上一页交互
document.querySelector('.previous-button').addEventListener('click', () => {
  selectedIndex--;
  selectedIndex = (selectedIndex + cellCount) % cellCount;
  rotateCarousel();
});

// 下一页交互
document.querySelector('.next-button').addEventListener('click', () => {
  selectedIndex++;
  selectedIndex = selectedIndex % cellCount;
  rotateCarousel();
});

// 初始化
rotateCarousel();

适配Bootstrap的操作提示

  • 不需要替换Bootstrap的现有栅格系统,直接把上述结构放到Bootstrap的container、row、col-*容器内即可实现响应式布局
  • 如需调整单元格大小,环形半径可按照公式计算:环形半径 = (单元格宽度/2) / tan(180°/单元格数),4个单元格场景下即可保证元素不会重叠、间距均匀
  • 若后续需扩展更多展示项目,可按4个为一组追加单元格,每个新单元格的旋转角度在上一个基础上累加90°即可,无需修改整体逻辑

内容的提问来源于stack exchange,提问作者redoc01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.18 12:36:06