• 把函数作为参数,调用的时候,先判断这个参数是不是存在,然后调用函数的时候加上()


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style>
    *{margin:0px;padding:0px;}
    div{100px;height:100px;background:red;position:absolute;top:60px;left:20px;}
    input{100px;height:40px;}
    </style>
    <script>
    function getStyle(obj,attr){
    if(obj.currentStyle){
    return obj.currentStyle[attr]
    }else{
    return getComputedStyle(obj)[attr]
    }
    }

    window.onload=function(){
    var odiv=document.getElementsByTagName('div')[0];
    var oinput=document.getElementsByTagName('input');
    var time=null;
    function doMove(obj,attr,target,speed,fn){
    var cur=parseInt(getStyle(obj,attr))
    speed=cur<target?Math.abs(speed):-Math.abs(speed);
    clearInterval(obj.time);
    obj.time=setInterval(function(){
    cur+=speed;
    if(cur>=target&&speed>0||cur<target&&speed<0){
    clearInterval(obj.time);
    if(fn){
    fn()
    }
    cur=target;
    }else{
    odiv.style.left=cur+'px';
    }
    }) 
    }


    oinput[0].onclick=function(){
    doMove(odiv,'left',500,10,function(){
    alert('停1')
    }) 

    oinput[1].onclick=function (){
    doMove(odiv,'left',0,10,function(){
    alert('停2')
    }) 
    }


    }
    </script>
    </head>
    <body>
    <div></div>
    <input type="button" value="向右">
    <input type="button" value="向左">
    </body>
    </html>

  • 相关阅读:
    linux(ubuntu)设置开机启动
    mysql 数据库迁移
    maven GroupId和ArtifactID
    Android内存溢出与内存泄漏
    Android性能优化方向和相关工具
    安卓 px,dpi,dp,ps的区别与联系
    python 深浅拷贝
    公网ip和私网ip
    打开系统设置页
    如何用代码开启“个人热点”功能?如何用代码把iOS设备设置成一个无线路由?
  • 原文地址:https://www.cnblogs.com/luziluck/p/7889330.html
Copyright © 2020-2023  润新知