因此有很多与此相关的帖子,但我似乎无法确切确定我的风格出了什么问题,如果您中的某些人将此视为对先前问题的重新思考,我深表歉意。
这个问题很简单:我只是试图建立一个随机骰子。 js可以正常工作,一切正常。问题在于多维数据集看起来很笨拙,我不确定为什么。我通常对CSS非常满意,但是对于3D变换我不做很多工作,因此我缺乏直觉。如果运行该代码段,则可以看到当多维数据集旋转时,并不是所有的侧面都是光滑的,有些侧面看起来会塌陷,我不确定为什么(尤其是1和2侧面)。完整的代码在下面的代码片段中:
const rollBtn = document.getElementById('roll');
const sides = ['one', 'two', 'three', 'four', 'five', 'six'];
const output = document.getElementById('dice_output');
let currentClass = '';
const rollDice = () => {
let randomSide = sides[Math.floor(Math.random() * (6) + 1)];
let currentSide = document.getElementById(randomSide);
let generatedSide = `show-${randomSide}`;
if (currentClass) {
output.classList.remove(currentClass);
}
output.classList.add(generatedSide);
currentClass = generatedSide;
}
rollBtn.addEventListener('click', rollDice);
* {
box-sizing: border-box;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
.container {
width: 80px;
margin: 5% auto;
text-align: center;
}
.stage {
width: 80px;
height: 80px;
perspective: 300px;
}
.btn-container {
width: 80px;
margin: 2% auto;
text-align: center;
}
.the-big-z {
z-index: 1000;
}
.the-die {
width: 80px;
height: 80px;
position: relative;
transform-style: preserve-3d;
transition: all ease .5s;
display: block;
margin-bottom: 5%;
}
.die-side {
width: 80px;
height: 80px;
color: #fff;
background-color: #000;
position: absolute;
text-align: center;
padding-top: 20%;
border: solid 3px teal;
}
#one {
transform: rotateY( 0deg) translateZ(0px);
}
#two {
transform: rotateY( 90deg) translateZ(0px);
}
#three {
transform: rotateY(180deg) translateZ(40px);
}
#four {
transform: rotateY(-90deg) translateZ(40px);
}
#five {
transform: rotateX( 90deg) translateZ(40px);
}
#six {
transform: rotateX(-90deg) translateZ(40px);
}
#dice_output.show-one {
transform: translateZ(-40px)
rotateY( 0deg);
}
#dice_output.show-two {
transform: translateZ(-40px)
rotateY( -90deg);
}
#dice_output.show-three {
transform: translateZ(-40px)
rotateY(-180deg);
}
#dice_output.show-four {
transform: translateZ(-40px)
rotateY( 90deg);
}
#dice_output.show-five {
transform: translateZ(-40px)
rotateX( -90deg);
}
#dice_output.show-six {
transform: translateZ(-40px)
rotateX( 90deg);
}
<html>
<head></head>
<body>
<div class="container clearfix">
<div class="stage">
<div id="dice_output" class="the-die">
<div id="one" class="die-side">1</div>
<div id="two" class="die-side" >2</div>
<div id="three" class="die-side" >3</div>
<div id="four" class="die-side" >4</div>
<div id="five" class="die-side" >5</div>
<div id="six" class="die-side" >6</div>
</div>
</div>
</div>
<div class="btn-container">
<button id="roll">roll the dice</button>
</div>
</body>
</html>
答案 0 :(得分:2)
您还需要将#one
和#two
转换为40px
const rollBtn = document.getElementById('roll');
const sides = ['one', 'two', 'three', 'four', 'five', 'six'];
const output = document.getElementById('dice_output');
let currentClass = '';
const rollDice = () => {
let randomSide = sides[Math.floor(Math.random() * (6) + 1)];
let currentSide = document.getElementById(randomSide);
let generatedSide = `show-${randomSide}`;
if (currentClass) {
output.classList.remove(currentClass);
}
output.classList.add(generatedSide);
currentClass = generatedSide;
}
rollBtn.addEventListener('click', rollDice);
* {
box-sizing: border-box;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
.container {
width: 80px;
margin: 5% auto;
text-align: center;
}
.stage {
width: 80px;
height: 80px;
perspective: 300px;
}
.btn-container {
width: 80px;
margin: 2% auto;
text-align: center;
}
.the-big-z {
z-index: 1000;
}
.the-die {
width: 80px;
height: 80px;
position: relative;
transform-style: preserve-3d;
transition: all ease .5s;
display: block;
margin-bottom: 5%;
}
.die-side {
width: 80px;
height: 80px;
color: #fff;
background-color: #000;
position: absolute;
text-align: center;
padding-top: 20%;
border: solid 3px teal;
}
#one {
transform: rotateY( 0deg) translateZ(40px); // Here
}
#two {
transform: rotateY( 90deg) translateZ(40px); // And here
}
#three {
transform: rotateY(180deg) translateZ(40px);
}
#four {
transform: rotateY(-90deg) translateZ(40px);
}
#five {
transform: rotateX( 90deg) translateZ(40px);
}
#six {
transform: rotateX(-90deg) translateZ(40px);
}
#dice_output.show-one {
transform: translateZ(-40px)
rotateY( 0deg);
}
#dice_output.show-two {
transform: translateZ(-40px)
rotateY( -90deg);
}
#dice_output.show-three {
transform: translateZ(-40px)
rotateY(-180deg);
}
#dice_output.show-four {
transform: translateZ(-40px)
rotateY( 90deg);
}
#dice_output.show-five {
transform: translateZ(-40px)
rotateX( -90deg);
}
#dice_output.show-six {
transform: translateZ(-40px)
rotateX( 90deg);
}
<html>
<head></head>
<body>
<div class="container clearfix">
<div class="stage">
<div id="dice_output" class="the-die">
<div id="one" class="die-side">1</div>
<div id="two" class="die-side" >2</div>
<div id="three" class="die-side" >3</div>
<div id="four" class="die-side" >4</div>
<div id="five" class="die-side" >5</div>
<div id="six" class="die-side" >6</div>
</div>
</div>
</div>
<div class="btn-container">
<button id="roll">roll the dice</button>
</div>
</body>
</html>