1

我是JavaScript新手,现在遇到了这个问题:当我启动网页时,“btn”是不可见的(隐藏),但我需要它在鼠标按下之前可见。这是脚本:

var start= false;
var racket = document.getElementById("racket");
var btn = document.getElementById("btn");
btn.style.visibility = "visible";
btn.onmousedown = Start();

function Start() {
    btn.style.visibility = "hidden";
    start = true;
    document.onclick = RacketClick();
}

function RacketClick() {

}

当我启动网页时,btn 被隐藏了......你能帮帮我吗?

UPD 1 HTML 代码和 CSS 代码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <meta charset="utf-8" />
        <title>Main</title>
        <link href="/css/default.css" rel="stylesheet" />
        <script src="/js/mymain.js"></script>
    </head>
    <body>
        <div id="racket"></div>
        <div id="btn"></div>
    </body>
</html>

#racket {
    top: 100%;
    margin-top: -200px;
    left: 50%;
    margin-left: -77px;
    position: absolute;
    background-image: url('/images/racket.png');
    width: 154px;
    height: 250px;
    animation-name: racketanimation;
    animation-iteration-count: infinite;
    animation-delay: 0s;
    animation-fill-mode: forwards;
    animation-duration: 4s;
    animation-timing-function: linear;
    animation-direction:alternate;
}

@keyframes racketanimation {
    from {
        transform: rotateX(40deg);
    }
    to {
        transform: rotateX(55deg);
    }
}

#btn {
    top: 50%;
    left: 50%;
    position: absolute;
    margin-top: -128px;
    margin-left: -128px; 
    height: 256px; 
    width: 256px;
    background-image: url('/images/playnowborder.png');
    animation-iteration-count: infinite;
    animation-delay: 0s;
    animation-fill-mode: forwards;
    animation-duration: 10s;
    animation-name: clicken;
    animation-timing-function: linear;
}

@keyframes clicken {
    from {
        transform: rotate(360deg);
    }
    to {
        transform: rotate(0deg);
    }
}

我也是这个网站的新手!我希望你的答案,但我没有投票的声誉......

4

2 回答 2

3

您应该使用不带括号的函数分配:

btn.onmousedown = Start;

否则函数Start会在分配时执行,并且您的按钮会立即隐藏。

于 2013-07-18T08:29:37.783 回答
1

这条线是你的问题:

btn.onmousedown = Start();

您正在执行函数 start 并将结果 ( undefined) 分配给btn.onmousedown. 你应该有:

btn.onmousedown = Start;

不带括号,为函数赋值,而不是调用函数。

于 2013-07-18T08:30:38.350 回答