gpt4 book ai didi

javascript - Codeacademy 的石头剪刀布游戏

转载 作者:行者123 更新时间:2023-11-28 05:35:24 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何将我的 innerHTML 居中(垂直和水平),同时记住内容的长度。这是我的 HTML 和 CSS。

<body>
<div class="container">
<p id="demo"></p>
<script>
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} document.getElementById("demo").innerHTML = "Computer: " + computerChoice;

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
document.getElementById("demo").innerHTML = "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("dmeo").innerHTML = "Sorry, the computer shows paper. You lost!";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("demo").innerHTML = "Sorry, the computer shows scissors. You lost!";
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("demo").innerHTML = "Sorry, the computer shows rock. You lost!";
}
}
}

compare (userChoice, computerChoice);
</script>

.container {
width: 50%;
margin: 0 auto;
padding: 10px;
}

body {
background-image: url(images/bg-img.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
margin: 0;
}

#demo {

font-family: Impact;
font-size: 40px;
color: #cddc39;
text-shadow: 2px 4px 3px rgba(106, 209, 25, 0.62);
}

最佳答案

您可以通过多种方法实现此目的。

选项 1:您可以使用负边距使 div 居中:

.container {
position: absolute;
left: 50%;
top: 50%;
width: 50%;
margin-left: -25%;
margin-right: -25%;
}

选项 2:您可以使用支持较少的 css 转换:translate

.container {
position: absolute;
left: 50%;
top: 50%;
width: 50%;

-ms-transform: translate(-50%,-50%); /* IE 9 */
-webkit-transform: translate(-50%,-50%); /* Safari */
transform: translate(-50%,-50%);
}

我建议使用选项 1,因为它得到了更好的支持。但是,嘿,无论你的船漂浮什么;)

关于javascript - Codeacademy 的石头剪刀布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39407661/

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