如何使用输入框通过CSS点击事件进行卡片翻转?

时间:2018-09-08 20:44:07

标签: html css sass onclick

我正在尝试使用隐藏在框中的输入类型使用CSS click-event进行卡片翻转,每次用户单击图块时,该框都会翻转并在背面显示内容,用户单击后面的图块,它将再次翻转到前面的图块。这是我的代码段,以获取更多信息:

.container {
  padding: 12rem;
  display: flex;
  justify-content: center;
}

.tile {
  perspective: 100rem;
  -moz-perspective: 100rem;
  position: relative;
  width: 50%;
  cursor: pointer;
  margin-right: 5rem;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.tile-back, .tile-front {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.tile-back {
  transform: rotateY(180deg);
}
.tile-back, .tile-back .card-body {
  font-size: 1.5rem;
  width: 50%;
  text-align: center;
}

.card {
  background-color: #58AA3F;
  height: 20rem;
  transition: all .8s ease;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.15);
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-transition: all 600ms;
  transition: all 600ms;
  z-index: 20;
}
.card div {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.card-body {
  font-size: 2rem;
  color: #fff;
  text-align: center;
}
.card-footer {
  font-size: 2rem;
}
.card:hover .card {
  -webkit-transform: rotateX(20deg);
  transform: rotateX(20deg);
  box-shadow: 0 20px 20px rgba(50, 50, 50, 0.2);
}
.card input {
  display: none;
}
.card :checked + .card {
  transform: rotateX(180deg);
  -webkit-transform: rotateX(180deg);
}
.card:hover :checked + .card {
  transform: rotateX(160deg);
  -webkit-transform: rotateX(160deg);
  box-shadow: 0 20px 20px rgba(255, 255, 255, 0.2);
}
<div class="tile">
   <input type="checkbox"/>
     <div class="card tile-front">
        <div class="card-body">
            <p>Click to display back</p>
         </div>
      </div>
      <div class="card tile-back">
         <div class="card-body">
            <p>Click to display front</p>
         </div>
      </div>
</div>

如果代码中缺少某些内容,则不会触发输入。

2 个答案:

答案 0 :(得分:3)

我对您的代码进行了一些更改。 为了清楚起见,我保留了该复选框的可见性,但您可以将其隐藏。

label[for=test] {
  display:block;
  -webkit-perspective: 800px;
  perspective: 800px;
}

.reversible {
  position: relative;
  width: 250px;
  margin: 50px auto;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-transition: transform 1s ease;
  transition: all 1s
}

input[type=checkbox]:checked + label .reversible {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.reversible .card {
  position: absolute;
  left: 0;
  top: 0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}


.tile-front {
  z-index: 2;
}

.tile-back {
  -webkit-transform: rotateY( 180deg);
  transform: rotateY( 180deg);
}

.card-body {
  font-size: 2rem;
  color: #fff;
  text-align: center;
}
.card-footer {
  font-size: 2rem;
}


.card {
  background-color: #58AA3F;
  height: 20rem;
  transition: all .8s ease;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.15);

}
<input type="checkbox" id="test"/>
<label for="test">
  <div class="reversible">
     <div class="card tile-back">
        <div class="card-body">
            <p>Click to display back</p>
         </div>
      </div>
      <div class="card tile-front">
         <div class="card-body">
            <p>Click to display front</p>
         </div>
      </div>
</div>
</label>

我希望对您有帮助

答案 1 :(得分:0)

OrderComparator为基础,我将其更改为与checkbox一起使用

body {
  font-family: sans-serif;
}

.scene {
  width: 200px;
  height: 260px;
  border: 1px solid #CCC;
  margin: 10px 0;
  perspective: 600px;
}

input {
  display: none;
}

label {
  width: 200px;
  height: 260px;
}

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

#card:checked + label .card {
  transform: rotateY(180deg);
}

.card__face {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 260px;
  color: white;
  text-align: center;
  font-weight: bold;
  font-size: 40px;
  backface-visibility: hidden;
}

.card__face--front {
  background: red;
}

.card__face--back {
  background: blue;
  transform: rotateY(180deg);
}
<div class="scene scene--card">
  <input id="card" type="checkbox">
  <label for="card">
    <div class="card">
      <div class="card__face card__face--front">front</div>
      <div class="card__face card__face--back">back</div>
    </div>
  </label>
</div>