技法57 (◐‿◑) 用Js实现div自动移动(600px循环)
http://xxx.fishc.com/forum/201705/05/225545py2j222x5z6x55n2.png按照提示,完成代码,秀秀你的编程能力!
不许看答案,否则打屁屁
程序分析:
实现div移动,周期内移动
1.定时器
2.onload事件
3.offsetLeft
4.style.left
源代码:
**** Hidden Message *****
○面试题索引贴●
如果喜欢,请订阅{:10_303:} :
HTML5 - 庖丁解牛 + JavaScript - 庖丁解牛
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Javascript Test</title>
<style>
.container {
position : relative;
margin : 50px auto;
width : 600px;
height : 90px;
overflow : hidden;
background-color : #ccc;
}
.runner {
position : absolute;
top : 5px;
left : 0;
width : 80px;
height : 80px;
background-color : #00fdff;
}
</style>
</head>
<body>
<div class="container">
<div class="runner" id="runner"></div>
</div>
<script>
window.onload = function () {
let runner = document.getElementById('runner');
let speed = 2;
let timer = null;
timer = setInterval(function () {
let left = runner.offsetLeft;
if (left === 520) {
runner.style.left = '0px';
left = 0;
}
runner.style.left = left + speed + 'px';
}, 30);
};
</script>
</body>
</html> 看一看offset 要看要看 看看
window.onload = function(){
var d = document.getElementById("d1");
d.style.left = "200px";
var n = 0;
var dir = 5;
var t = setInterval(function(){
if(n > 600){
dir = -5;
}
if(n < 0){
dir = 5;
}
n+=dir;
d.style.left = n + "px";
},20);
}; 看一下它的原理 上 嘿嘿嘿 {:5_102:} 做完了
看看 答案 LIH
6666 芝麻开门
页:
[1]