Codeacademy的摇滚,纸张,剪刀游戏

时间:2016-09-09 08:58:09

标签: javascript html5 css3 function conditional

我想弄清楚如何在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);
}

2 个答案:

答案 0 :(得分:0)

您可以通过多种方法实现这一目标。

选项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因为它得到了更好的支持。但是,嘿,无论什么漂浮在你的船上;)

答案 1 :(得分:0)

容器选择器中的

text-align: center;应该可以解决问题。

你也可以使用&#39; flexbox&#39;水平和垂直居中:

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

Useful guide for flex