首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >点击翻转卡片使其保持翻转状态

问点击翻转卡片使其保持翻转状态
EN

Stack Overflow用户
提问于 2021-07-25 01:12:38
回答 1查看 43关注 0票数 0

我已经用HTML和CSS制作了带鼠标悬停的翻转卡片。

​

代码语言:javascript
复制
body {
  color: #e0e1dd;
  font-family: "Poppins", sans-serif;
}
.back_page {
  padding: 1em;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 3;
}
.back_page img {
  border-radius: 20px;
  height: 1.5em;
  width: 1.5em;
}
a img:hover {
  background:#415a77;
}
.square {
  max-width: 1000px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}
.box {
  cursor: pointer;
  width: 300px;
  height: 200px;
  margin-bottom: 30px;
}
/* BOX 1 */

.center1 {
  display: flex;
  justify-content: center;
  align-items: center;
}

.front1 {
  position: absolute;
  width: 300px;
  height: 200px;
  background-color: #778da9;
  transform: perspective(800px);
  backface-visibility: hidden;
  transition: all 0.5s;
  overflow: hidden;
  border-radius: 20px;
}

.box1:hover .front1 {
  transform: rotateX(180deg);
}
.front1::before {
  content: "";
  position: absolute;
  bottom: -25px;
  border-radius: 50%;
  background: #6e2626;
  width: 50px;
  height: 50px;
}

.back1 {
  position: absolute;
  width: 300px;
  height: 200px;
  background: #415a77;
  transform: perspective(800px) rotateY(0deg);
  backface-visibility: hidden;
  transition: all 0.5s;
  flex-direction: column;
  transform: rotateX(180deg);
  border-radius: 20px;
}

.box1:hover .back1 {
  transform: rotateX(0deg);
}

.back1 p {
  margin: 10px 30px;
  font-size: 20px;
  text-align: center;
}
代码语言:javascript
复制
<div class="square">
        <div class="box1 box">
            <div class="front1 center1">
                <h1>Front</h1>
            </div>
            <div class="back1 center1">
                <p>Back</p>
            </div>
        </div>
    </div>

​

我想这样做后,点击“后退”的卡片不会返回到前面(在一个页面上将是少数这些翻转卡片,所以当点击第一个将是在“后退”,然后点击另一个和这两个卡片将在背面)。接下来的事情是我需要将所有点击的卡片返回到初始状态。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-25 03:41:46

您可以添加一个用于保持卡片显示的类show,在box元素上添加onclick和,并相应地更改其余代码。

​

代码语言:javascript
复制
function focusCard(element) {
  element.getElementsByClassName("front")[0].classList.toggle("show");
  element.getElementsByClassName("back")[0].classList.toggle("show");
}
代码语言:javascript
复制
body {
  font-family: "Poppins", sans-serif;
}

#wrapper {
  display: flex;
  flex-direction: row;
}

.square {
  color: #e0e1dd;
  max-width: 1000px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.box {
  cursor: pointer;
  width: 300px;
  height: 200px;
  margin-bottom: 30px;
}

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.front {
  position: absolute;
  width: 300px;
  height: 200px;
  background-color: #778da9;
  transform: perspective(800px);
  backface-visibility: hidden;
  transition: all 0.5s;
  overflow: hidden;
  border-radius: 20px;
}

.box:hover .front,
.show {
  transform: rotateX(180deg);
}

.front::before {
  content: "";
  position: absolute;
  bottom: -25px;
  border-radius: 50%;
  background: #6e2626;
  width: 50px;
  height: 50px;
}

.back {
  position: absolute;
  width: 300px;
  height: 200px;
  background: #415a77;
  transform: perspective(800px) rotateY(0deg);
  backface-visibility: hidden;
  transition: all 0.5s;
  flex-direction: column;
  transform: rotateX(180deg);
  border-radius: 20px;
}

.box:hover .back,
.show {
  transform: rotateX(0deg);
}

.back p {
  margin: 10px 30px;
  font-size: 20px;
  text-align: center;
}
代码语言:javascript
复制
<div id="wrapper">
  <div class="square">
    <div class="box" onclick="focusCard(this)">
      <div class="front center">
        <h1>Front</h1>
      </div>
      <div class="back center">
        <p>Back</p>
      </div>
    </div>
  </div>
  <div class="square">
    <div class="box" onclick="focusCard(this)">
      <div class="front center">
        <h1>Front</h1>
      </div>
      <div class="back center">
        <p>Back</p>
      </div>
    </div>
  </div>
</div>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68512184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档