【问题标题】:Codeacademy's rock, paper, scissors gameCodeacademy 的石头、纸、剪刀游戏
【发布时间】:2017-01-17 08:57:59
【问题描述】:

我试图弄清楚如何将我的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);
}

【问题讨论】:

标签: javascript html css function conditional


【解决方案1】:

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

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

【讨论】:

  • 非常感谢。这对我帮助很大。但是,有没有办法使所有文本居中。我的意思是当脚本显示“结果是平局!”时或“恭喜!”它向左一点。
  • 您可以随时尝试添加 text-align: center;
  • 哇...抱歉这些愚蠢的问题。并再次感谢您的帮助。定位真的让我很头疼。 :(
  • 不客气!您是否愿意将您的问题标记为已回答,以便将其关闭? - 谢谢
【解决方案2】:

容器选择器中的text-align: center; 应该可以解决问题。

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

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

Useful guide for flex

【讨论】:

    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多