javascript - 试图让 Bootstrap Carousal 显示 4 个已修复的项目
问题描述
我找到了这个:
CSS:
* { box-sizing: border-box; }
body { font-family: sans-serif; }
.scene {
border: 1px solid #CCC;
margin: 40px 0;
position: relative;
width: 210px;
height: 140px;
margin: 40px auto;
perspective: 1000px;
}
.carousel {
width: 100%;
height: 100%;
position: absolute;
transform: translateZ(-288px);
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;
}
.carousel__cell:nth-child(9n+1) { background: hsla( 0, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+2) { background: hsla( 40, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+3) { background: hsla( 80, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+4) { background: hsla(120, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+5) { background: hsla(160, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+6) { background: hsla(200, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+7) { background: hsla(240, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+8) { background: hsla(280, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+0) { background: hsla(320, 100%, 50%, 0.8); }
.carousel__cell:nth-child(1) { transform: rotateY( 0deg) translateZ(288px); }
.carousel__cell:nth-child(2) { transform: rotateY( 40deg) translateZ(288px); }
.carousel__cell:nth-child(3) { transform: rotateY( 80deg) translateZ(288px); }
.carousel__cell:nth-child(4) { transform: rotateY(120deg) translateZ(288px); }
.carousel__cell:nth-child(5) { transform: rotateY(160deg) translateZ(288px); }
.carousel__cell:nth-child(6) { transform: rotateY(200deg) translateZ(288px); }
.carousel__cell:nth-child(7) { transform: rotateY(240deg) translateZ(288px); }
.carousel__cell:nth-child(8) { transform: rotateY(280deg) translateZ(288px); }
.carousel__cell:nth-child(9) { transform: rotateY(320deg) translateZ(288px); }
html:
<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 class="carousel__cell">5</div>
<div class="carousel__cell">6</div>
<div class="carousel__cell">7</div>
<div class="carousel__cell">8</div>
<div class="carousel__cell">9</div>
前面一个后面一个
javascript:
var carousel = document.querySelector('.carousel');
var cellCount = 9;
var selectedIndex = 0;
function rotateCarousel() {
var angle = selectedIndex / cellCount * -360;
carousel.style.transform = 'translateZ(-288px) rotateY(' + angle + 'deg)';
}
var prevButton = document.querySelector('.previous-button');
prevButton.addEventListener( 'click', function() {
selectedIndex--;
rotateCarousel();
});
var nextButton = document.querySelector('.next-button');
nextButton.addEventListener( 'click', function() {
selectedIndex++;
rotateCarousel();
});
https://codepen.io/desandro/pen/jxwELK
但似乎无法让 4 个固定项目工作,任何帮助,我剥离了 css 和 javascript 的某些部分,但没有正确显示项目。
我试过这个CSS:
.carousel__cell:nth-child(9n+1) { background: hsla( 0, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+2) { background: hsla( 40, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+3) { background: hsla( 80, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+4) { background: hsla(120, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+5) { background: hsla(160, 100%, 50%, 0.8); }
.carousel__cell:nth-child(1) { transform: rotateY( 0deg) translateZ(128px);
}
.carousel__cell:nth-child(2) { transform: rotateY( 90deg) translateZ(128px);
}
.carousel__cell:nth-child(3) { transform: rotateY( 180deg) translateZ(128px);
}
.carousel__cell:nth-child(4) { transform: rotateY(360deg) translateZ(128px);
}
.carousel__cell:nth-child(5) { transform: rotateY(160deg) translateZ(288px);
}
并将单元格计数更改为 4。
但我错过了一个细胞
解决方案
.scene {
border: 1px solid #CCC;
margin: 40px 0;
position: relative;
width: 210px;
height: 140px;
margin: 40px auto;
perspective: 1000px;
}
.carousel {
width: 100%;
height: 100%;
position: absolute;
transform: translateZ(-288px);
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;
}
.carousel__cell:nth-child(9n+1) { background: hsla( 0, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+2) { background: hsla( 40, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+3) { background: hsla( 80, 100%, 50%, 0.8); }
.carousel__cell:nth-child(9n+4) { background: hsla(120, 100%, 50%, 0.8); }
.carousel__cell:nth-child(1) { transform: rotateY( 0deg) translateZ(128px); }
.carousel__cell:nth-child(2) { transform: rotateY( 90deg) translateZ(128px); }
.carousel__cell:nth-child(3) { transform: rotateY( 180deg) translateZ(128px); }
.carousel__cell:nth-child(4) { transform: rotateY(270deg) translateZ(128px); }
var cellCount = 4;
推荐阅读
- javascript - 剑道在网格下拉菜单中更新页面上的任何点击
- javascript - 如何在 WordPress 中翻译 Vue 输出?
- ios - 如何在 ARKit 中通过手势动作在 SCNNode 中添加标签节点和用户图像?
- python-2.7 - 如何在 python 2.7 django 中保存时检查对象是插入还是更新
- java - jar 文件中缺少主要属性,MANIFEST.MF 文件位于 META-INF 文件夹中
- c# - c# 以编程方式设置或清除文件的“已访问”元信息
- git - 寻找一个在关闭文件时自动提交 git 的文本编辑器
- email - BIND DKIM 语法错误
- javascript - 我想在这段代码中显示 Fetched API
- geode - 获取 java.lang.IllegalStateException:无法在定位器中创建区域