codeacademy是一个石头、剪子、布的游戏

Codeacademy's rock, paper, scissors game

本文关键字:剪子 游戏 石头 一个 codeacademy      更新时间:2023-09-26

我试图找出如何中心(垂直和水平)我的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 transform: 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,因为它的支持要好得多。但是,嘿,随你的便

容器选择器中的text-align: center;应该可以做到这一点。

你也可以使用'flexbox'来设置水平和垂直居中:

.container {
  width: 50%;
  margin: 0 auto;
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
} 

flex实用指南