为何点击了盒子。没有移动,指正下,非常感谢
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>练习</title>
<style>
div{
width:100px;
height:200px;
background-color:red;
}
</style>
</head>
<body>
<div>1</div>
<script>
var div=document.querySelector('div');
function animate(obj,target){
div.addEventListener('click',function(){
var timer=setInterval(function(){
if(obj.offsetleft>=target){
clearInterval(timer);
}
obj.style.left= obj.offsetleft + 1 + 'px';
},30);
});
};
animate(div,300);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>练习</title>
<style>
div {
width: 100px;
height: 200px;
background-color: red;
position: relative;
}
</style>
</head>
<body>
<div style="left:0">1</div>
<script>
var div = document.querySelector('div');
function animate(obj, target) {
obj.addEventListener('click', function () {
var timer = setInterval(function () {
if (Number.parseInt(obj.style.left) >= target) {
console.log(1);
clearInterval(timer);
}
obj.style.left = Number.parseInt(obj.style.left) + 1 + 'px';
}, 60);
});
};
animate(div, 30);
</script>
</body>
</html> kogawananari 发表于 2022-4-10 15:01
Number.parseInt(obj.style.left)这是啥意思啊 小黄练编程 发表于 2022-4-10 16:52
Number.parseInt(obj.style.left)这是啥意思啊
str转int 单位会被去掉
页:
[1]