我希望img文件在屏幕上移动并在点击时消失,一旦消失,将会有一个弹出窗口显示完成。但是,只有一半的代码可以工作,而且img文件被固定不动。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function runIt(me) {
$(me).css("top",Math.random()*800)
.animate({"left":"1200px"},Math.random()*3000+1500,function(){$(me).css("top",Math.random()*800)})
.animate({"left":"-150px"},Math.random()*3000+1500,function(){$(me).css("top",Math.random()*800);runIt(me)});
}
$(".target").each(function(){runIt($(this))});
score = 0;
function calcScore(me){
$(me).hide();
score++;
$("#message").html("You shot "+score);
if(score==8){
alert("You got all of them!");
}
}
</script>
<body>
<div id="container">
<div id="background">
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
<div class="target" onclick="calcScore(this)"><img src="images/bottle.png" width="80" height="80" alt=""/></div>
</div>
</div>
</body>
答案 0 :(得分:4)
要解决此问题,只需在页面中添加此css
即可.target {
position: absolute;
}
并将您的触发器代码$(".target").each(function(){runIt($(this))});
放入文档中,如下所示
$(document).ready(function(){
$(".target").each(function(){runIt($(this))});
})