显示 Div、设置超时、隐藏 Div

单击 div 时,应出现一个图像,3 秒后再次消失。该函数的第一行让它出现,但如果我添加超时,则单击图像时不会发生任何事情。我该如何解决?


(另外,如果有人可以包括我如何让心脏出现在#stickycat div 的中心,请这样做!)


function heart() {

    document.getElementById("heart").style.display = "block"

    setTimeout(fn(){ document.getElementById("heart").style.display = "none"}, 3000)

}

#stickycat {

    position: fixed;

    bottom:10px;

    right: 10px;

    width:100px;

    height:100px;

    border-radius:50%;

    border: solid 4px rgba(54, 215, 183, 1);

    background-color:white;

    text-align:center;

    box-shadow: 1px 1px 10px rgba(54, 215, 183, 1), -1px 1px 10px rgba(54, 215, 183, 1), -1px -1px 10px rgba(54, 215, 183, 1), 1px -1px 10px rgba(54, 215, 183, 1);

}

#stickycat img {

    max-width:100%;

    max-height:100%;

    z-index:2;

    border-radius:50%;

}

#heart {

    animation: pulse 2s linear infinite;

    position:absolute;

    width:50px;

    height:50px;

    display:none;

}

@keyframes pulse {

    0% { transform: scale(1); }

    50% { transform: scale(1.3); }

    100% { transform: scale(1); }

}

<div id="stickycat" onclick="heart()">

<img src="https://i.pinimg.com/originals/9d/b1/3f/9db13f4f06bfa1600e970fd32f1851db.gif">

<img id="heart" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Heart_coraz%C3%B3n.svg/1200px-Heart_coraz%C3%B3n.svg.png">

</div>


DIEA
浏览 92回答 1
1回答

万千封印

首先,fn()不是一种声明函数的方法。您可以使用function()关键字声明匿名函数。position心的属性设置为absolute。因此居中部分可以通过使用top和leftcss 属性来管理。下面是工作示例。function heart() {&nbsp; &nbsp; document.getElementById("heart").style.display = "block"&nbsp; &nbsp; setTimeout(function(){ document.getElementById("heart").style.display = "none"}, 3000);}&nbsp; #stickycat {&nbsp; &nbsp; position: fixed;&nbsp; &nbsp; bottom:10px;&nbsp; &nbsp; right: 10px;&nbsp; &nbsp; width:100px;&nbsp; &nbsp; height:100px;&nbsp; &nbsp; border-radius:50%;&nbsp; &nbsp; border: solid 4px rgba(54, 215, 183, 1);&nbsp; &nbsp; background-color:white;&nbsp; &nbsp; text-align:center;&nbsp; &nbsp; box-shadow: 1px 1px 10px rgba(54, 215, 183, 1), -1px 1px 10px rgba(54, 215, 183, 1), -1px -1px 10px rgba(54, 215, 183, 1), 1px -1px 10px rgba(54, 215, 183, 1);&nbsp; }&nbsp; #stickycat img {&nbsp; &nbsp; max-width:100%;&nbsp; &nbsp; max-height:100%;&nbsp; &nbsp; z-index:2;&nbsp; &nbsp; border-radius:50%;&nbsp; }&nbsp; #heart {&nbsp; &nbsp; animation: pulse 2s linear infinite;&nbsp; &nbsp; position:absolute;&nbsp; &nbsp; top: 25px;&nbsp; &nbsp; left: 25px;&nbsp; &nbsp; width:50px;&nbsp; &nbsp; height:50px;&nbsp; &nbsp; display:none;&nbsp; }&nbsp; @keyframes pulse {&nbsp; &nbsp; 0% { transform: scale(1); }&nbsp; &nbsp; 50% { transform: scale(1.3); }&nbsp; &nbsp; 100% { transform: scale(1); }&nbsp; }<div id="stickycat" onclick="heart()">&nbsp; <img src="https://i.pinimg.com/originals/9d/b1/3f/9db13f4f06bfa1600e970fd32f1851db.gif">&nbsp; <img id="heart" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Heart_coraz%C3%B3n.svg/1200px-Heart_coraz%C3%B3n.svg.png"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript