<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title></title>
<style type="text/css">
#d2
{
width: 100px;
color: yellow;
height: 100px;
border: 1px solid;
position: absolute;left: 0px; top: 0px;
background-color: #000000;
text-align: center;
font-size: 35px;
font-weight: bolder;
}
#d1
{
width: 100px;
height: 100px;
border: 1px solid;
margin-top: 100px;
margin-left: 50px;
}
</style>
<script type="text/javascript">
flag=true;
x=0
function fun1()
{
var input = document.getElementsByTagName("input")[0];
var div1=document.getElementsByTagName("div")[0];
if(x>=innerWidth-div1.offsetWidth)
{
flag=false;
}
else if(x<=0)
{
flag=true;
}
if(flag)
{
x=x+80
}
else
{
x=x-80
}
div1.style.left=x+"px"
}
</script>
</head>
<body>
<div id="d2">动次打次</div>
<div id="d1">
<input type="button" value="动起来!" οnclick="fun1()"/>
</div>
</body>
</html>
转载于:https://www.cnblogs.com/bellow/p/4526497.html