js实现div自动在窗口左右移动
进入页面div自动从左边移动到右边,然后到达最右边又往左移动,一直循环<script>functionmoveD(){varwidth=window.innerWidth...
进入页面div自动从左边移动到右边,然后到达最右边又往左移动,一直循环
<script>
function moveD(){
var width=window.innerWidth||
document.documentElement.clientWidth||
document.body.clientWidth;
var height=window.innerHeight||
document.documentElement.clientHeight||
document.body.clientHeight;
var d=document.getElementById("d");
var h=d.style.left;
h=parseInt(h);
width=parseInt(width);
height=parseInt(height);
if(h+130!=width){
setTimeout("r();",1000);
else if(h!==0){
setTimeout("l();",1000);
}
} 展开
<script>
function moveD(){
var width=window.innerWidth||
document.documentElement.clientWidth||
document.body.clientWidth;
var height=window.innerHeight||
document.documentElement.clientHeight||
document.body.clientHeight;
var d=document.getElementById("d");
var h=d.style.left;
h=parseInt(h);
width=parseInt(width);
height=parseInt(height);
if(h+130!=width){
setTimeout("r();",1000);
else if(h!==0){
setTimeout("l();",1000);
}
} 展开
展开全部
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8 />
<title>UFO来了</title>
<script>
window.onload = function() {
var i = 10;
var j = 0;
var e = target;
var win = document.documentElement || document.body;
function intern() {
var width = e.clientWidth;
var height = e.clientHeight;
var left = parseFloat(e.style.left);
var top = parseFloat(e.style.top);
var windowWidth = win.clientWidth;
var windowHeight = win.clientHeight;
if (windowWidth - width < (left + i)) {
i = -i;
} else if ((left + i) < 0) {
i = -i;
}
if (windowHeight - height < (top + j)) {
j = -j;
} else if ((top + j) < 0) {
j = -j;
}
e.style.left = left + i + "px";
e.style.top = top + j + "px";
}
setInterval(intern, 30);
};
</script>
</head>
<body>
<div id="target" style="border-radius:90px;background-color: red; width: 30px; height: 30px; position: absolute; top:100px; left: 0px;"></div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询