老师帮忙看下代码
来源:2-3 存储实现打怪小案例
小鲜花
2019-12-19 14:38:07
<!DOCTYPE html>
<html>
<head>
<title>locallstarag game</title>
<meta charset="utf-8">
<style type="text/css">
*{margin: 0;padding: 0;}
html,body{position: relative;height: 100%;}
.boder{width:300px;height:20px;border:1px solid #000;position: absolute;left: 50%;top: 20px;margin-left: -150px;}
.xue{width: 300px;height: 100%;background: red;}
img{position: absolute;top: 50%;left: 50%;margin: -100px 0 0 -75px;}
</style>
</head>
<body>
<div class="boder">
<div class="xue"></div>
</div>
<img src="1.jpeg">
<script type="text/javascript">
var timer=null,num=0,max=300;
var xue=document.querySelector('.xue');
if (localStorage.a) {
max=localStorage.a;
xue.style.width=max+'px';
};
onclick=function(){
var r=Math.random()*5+5;
max-=r;
localStorage.setItem('a',max);
clearInterval(timer);
timer=setInterval(function(){
num++;
if (num==10) {
clearInterval(timer);
num=0;
document.body.style.left=0;
document.body.style.top=0;
return;
};
document.body.style.left=Math.random()*-20-10+'px';
document.body.style.top=Math.random()*-20-10+'px'; },1000);
}
</script>
</body>
</html>
1回答
好帮手慕星星
2019-12-19
同学你好,代码中的问题:
1、H5存储的数据需要通过getItem方法获取数据,如下
2、需要将血条的判断加在点击事件中,这样才能点击一次设置一次
3、定时器可以设置事件短一些,看起来效果会好
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
相似问题