gpt4 book ai didi

javascript - 尝试在用户单击时为 a div 设置动画。

转载 作者:行者123 更新时间:2023-11-28 03:37:48 27 4
gpt4 key购买 nike

我是一名初级程序员,正在尝试创建我的第一个结合了 html、css 和 javascript 的元素。我已经研究这个话题好几个小时了,我想找到一个有效的解决方案和解释。我有一个 html 和相应的 java 和 css 文件。我正在尝试创建一个内存游戏。我试图在用户单击时翻转卡片。我已经阅读了各种解决方案,但没有一个可以直接应用。我正在寻找纯 javascript/css 解决方案。提前致谢。

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css"> <!--making a link to the css stylesheet-->
<script src="scripts.js" type="text/javascript"></script>
<meta charset="UTF-8">
<title>Matching Game</title>
</head>
<body>
<div class = "title"><h1>Matching Game</h1></div>
<div class = "infobar">
<span>
__stars__
</span>
<span>
__move__
</span>
<span>
__timer__
</span>
<span>
__restart__
</span>
</div>
<div class = "gameboard">
<div class = "cardrows">
<div class = "flip1">
<div id = "card1" class = "card, off" onclick="flip(this.id)">
<div class = "cardback">
2
</div>
<div class = "cardfront ">
1
</div>
</div>
</div>
<div class = "flip2">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip3">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip4">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
</div>
<div class = "cardrows">
<div class = "flip1">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip2">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip3">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip4">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
</div>
<div class = "cardrows">
<div class = "flip1">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip2">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip3">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip4">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
</div>
<div class = "cardrows">
<div class = "flip1">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip2">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip3">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
<div class = "flip4">
<div class = "card">
<div class = "cardback">

</div>
<div class = "cardfront">

</div>
</div>
</div>
</div>
</div>
</body>
</html>

CSS

* {
-webkit-box-sizing: border-box; /*few lines of code for compatibility and for correct box resizing*/
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
background-color: rgb(228, 227, 225);
}

.title {
text-align: center;
color: black;
}

.gameboard {
width: 530px;
height: 530px;
background-color: #92bbce;
box-shadow: 0 0 11px 5px #00BCD4;
position: fixed;
right: 0;
left: 0;
margin-right: auto;
margin-left: auto;
margin-top: 20px;
margin-bottom: 20px;
}

.cardrows {
width: 524px;
height: 125px;
margin-left: 3px;
margin-right: 3px;
margin-top: 6px;
margin-bottom: 6px;
background-color: transparent;
position: relative;
}

.infobar {
text-align: center;
margin-left: auto;
margin-right: auto;
}


.cardback, .cardfront, .flip1, .flip2, .flip3, .flip4 {
width: 125px;
height: 125px;
}

.cardback, .cardfront {
/* hide back during swap */
backface-visibility: hidden;

position: absolute;
top: 0;
left: 0;

transition: transform 1s;
transform-style: preserve-3d;
}

.cardfront {
/* front placed above back element */
z-index: 2;
transform: rotateY(0deg);

background-color: #108DC0;
}

.cardback {
/* back hidden at start */
transform: rotateY(180deg);
}

.card {
position: relative;
}

.flip1, .flip2, .flip3, .flip4 {
/*container keeps perspective */
perspective: 1000px;
position: absolute;
}

.flip1 {
left: 3px;
}

.flip2 {
left: 134px;
}

.flip3 {
left: 265px;
}

.flip4 {
left: 396px;
}

.on {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );

transform: rotateY( 180deg );
}

.off {
-webkit-transform: rotateY( 0deg );
-moz-transform: rotateY( 0deg );
-o-transform: rotateY( 0deg );

transform: rotateY( 0deg );

}

Java:

function flip(el) {
console.log("flip called");
var elem = document.getElementById(el);
console.log(elem);
elem.className = "on";
console.log(elem);
}

最佳答案

如果您只是想翻开卡片,那您已经很接近了!

我刚刚添加了这两个 css 样式

transition: 1.0s;
transform-style: preserve-3d;

.on.off 类(他们需要知道动画运行了多长时间!)

function flip(el) {
console.log("flip called");
var elem = document.getElementById(el);
console.log(elem);
elem.className = "on";
console.log(elem);
}
* {
-webkit-box-sizing: border-box;
/*few lines of code for compatibility and for correct box resizing*/
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
background-color: rgb(228, 227, 225);
}

.title {
text-align: center;
color: black;
}

.gameboard {
width: 530px;
height: 530px;
background-color: #92bbce;
box-shadow: 0 0 11px 5px #00BCD4;
position: fixed;
right: 0;
left: 0;
margin-right: auto;
margin-left: auto;
margin-top: 20px;
margin-bottom: 20px;
}

.cardrows {
width: 524px;
height: 125px;
margin-left: 3px;
margin-right: 3px;
margin-top: 6px;
margin-bottom: 6px;
background-color: transparent;
position: relative;
}

.infobar {
text-align: center;
margin-left: auto;
margin-right: auto;
}

.cardback,
.cardfront,
.flip1,
.flip2,
.flip3,
.flip4 {
width: 125px;
height: 125px;
}

.cardback,
.cardfront {
/* hide back during swap */
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
transition: transform 1s;
transform-style: preserve-3d;
}

.cardfront {
/* front placed above back element */
z-index: 2;
transform: rotateY(0deg);
background-color: #108DC0;
}

.cardback {
/* back hidden at start */
transform: rotateY(180deg);
}

.card {
position: relative;
}

.flip1,
.flip2,
.flip3,
.flip4 {
/*container keeps perspective */
perspective: 1000px;
position: absolute;
}

.flip1 {
left: 3px;
}

.flip2 {
left: 134px;
}

.flip3 {
left: 265px;
}

.flip4 {
left: 396px;
}

.on {
transition: 1.0s;
transform-style: preserve-3d;
-webkit-transform: rotateY( 180deg);
-moz-transform: rotateY( 180deg);
-o-transform: rotateY( 180deg);
transform: rotateY( 180deg);
}

.off {
transition: 1.0s;
transform-style: preserve-3d;
-webkit-transform: rotateY( 0deg);
-moz-transform: rotateY( 0deg);
-o-transform: rotateY( 0deg);
transform: rotateY( 0deg);
}
<div class="title">
<h1>Matching Game</h1>
</div>
<div class="infobar">
<span>
__stars__
</span>
<span>
__move__
</span>
<span>
__timer__
</span>
<span>
__restart__
</span>
</div>
<div class="gameboard">
<div class="cardrows">
<div class="flip1">
<div id="card1" class="card, off" onclick="flip(this.id)">
<div class="cardback">
2
</div>
<div class="cardfront ">
1
</div>
</div>
</div>
<div class="flip2">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip3">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip4">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
</div>
<div class="cardrows">
<div class="flip1">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip2">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip3">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip4">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
</div>
<div class="cardrows">
<div class="flip1">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip2">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip3">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip4">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
</div>
<div class="cardrows">
<div class="flip1">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip2">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip3">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
<div class="flip4">
<div class="card">
<div class="cardback">

</div>
<div class="cardfront">

</div>
</div>
</div>
</div>
</div>

关于javascript - 尝试在用户单击时为 a div 设置动画。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44378437/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com