gpt4 book ai didi

javascript - 如何将移动物体与背景居中?

转载 作者:行者123 更新时间:2023-11-28 06:34:04 28 4
gpt4 key购买 nike

我的背景图片是 1080x648 像素,我将它居中。我输入了一个迷你游戏的 javascript 代码,其中图像文件将在屏幕上移动,但是,图像文件从屏幕左侧开始,在我的背景图像之外。有没有办法让它和我的背景一起居中?

这是我的代码:

<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()*500)
.animate({"left":"1000px"},Math.random()*3500+1500,function(){$(me).css("top",Math.random()*500)})
.animate({"left":"0px"},Math.random()*3500+1500,function(){$(me).css("top",Math.random()*500);runIt(me)});
}
$(".target").each(function(){runIt($(this))});

score = 0;
function calcScore(me){
$(me).hide();
score++;
$("#message").html("You shot "+score);
if(score==6){
alert("Congratulations! You cleared the lake!");
window.location.href = "alfheim_lake.html";
}
}

$(document).ready(function(){
$(".target").each(function(){runIt($(this))});
})

window.onload = function(){
setInterval(function(){
alert("You have drowned.");
window.location.href = "game_2.html";
}, 60000);
};
</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>
</div>
</body>

这是我的 CSS 文件:

@charset "UTF-8";

body {
line-height:0;
text-align:center;
padding:8px;
padding-left:80px;
}

#container {
overflow:hidden;
float:left;
width:1080px;
height:648px;
}

#background {
background-image:url(images/underwater.jpg);
width:1080px;
height:648px;
float:left;
overflow: hidden;
background-repeat:no-repeat;
}

.target {
position:absolute;
width:80px;
height:80px;
cursor:pointer;
}

最佳答案

我想这一定对你有用

#background{ 
width:1080px;
height:648px;
float:left;
background: url(images/underwater.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
overflow: hidden;
}

关于javascript - 如何将移动物体与背景居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34810188/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com