使用transform创建干净的立方体

问题描述 投票:2回答:1

所以有很多与此相关的帖子,但我似乎无法准确确定我的风格出错的地方,所以如果有些人认为这是对以前问题的重复,那么道歉。

这个问题非常简单:我只是试图建立一个随机骰子卷。 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>
css css-transforms
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.