js定时器,左右运动

setTimeout只在指定时间后执行一次

  1. <script>  
  2. //定时器 异步运行  
  3. function hello(){  
  4. alert("hello");  
  5. }  
  6. //使用方法名字执行方法  
  7. var t1 = window.setTimeout(hello,1000);  
  8. var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法  
  9. window.clearTimeout(t1);//去掉定时器  
  10. </script>

setInterval以指定时间为周期循环执行

  1. //实时刷新时间单位为毫秒  
  2. setInterval('refreshQuery()',8000);   
  3. /* 刷新查询 */  
  4. function refreshQuery(){  
  5.    $("#mainTable").datagrid('reload',null);  
  6. }  

//放入移开 鼠标事件,js 左右运动。

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
div,body{
border: 0;margin: 0;padding: 0;
}
#btn{
position: relative;
top: 0;
left: -300px;
width: 300px;
height: 300px;
background-color: aqua;
}
#btn span{
font-size: 18px;
width: 20px;
height: 50px;
background-color:darksalmon ;
position: absolute;
left: 300px;
top: 140px;
}
</style>

<script>

window.onload = function(){

var odiv = document.getElementById("btn");
odiv.onmouseover = function(){
startMove(20,0);
}
odiv.onmouseout = function(){
startMove(-20,-300);
}
}
var timer = null;
function startMove(sudu,zhongdianzhi){
clearInterval(timer);
var odiv = document.getElementById("btn");
timer = setInterval(function(){
if (odiv.offsetLeft == zhongdianzhi) {
clearInterval(timer);
} else{
odiv.style.left = odiv.offsetLeft + sudu + 'px';
}
},30)
}


</script>
<body>
<div id="btn"><span id="share">分享</span></div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值