doMove函数实现移动

我最终的目的是用源生js代码封装一个domove()函数,可以在传参之后实现元素任意方向的运动,在达到最终目的前应该一步一步地完成: 
1.配合之前封装过的getstyle()函数,实现点击按钮之后让一个div向前移动10px的效果:

<body>
    <button id="btn">前进</button>
    <div id="div1" style="width:100px; height:100px; background:red; margin-top:20px; position:absolute;"></div>
    <script>
        var oBtn=document.getElementById("btn");
        var oDiv=document.getElementById("div1");
        function getstyle(obj,attr){
            return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
        }
        oBtn.onclick=function(){
            var nowPosition =parseInt(getstyle(oDiv,'left'));
            oDiv.style.left=nowPosition+10+'px';
        }
    </script>
</body>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.用两个按钮分别控制div向前向后动 
3.把div向前向后动改成上下左右移动 
· 
· 
· 
按照这样的思路,再结合计时器,最终一步步实现一点按钮就能使元素连贯地变到目的的样式:

function doMove(id,derection,speed,subjectPosition){
//id:要运动的元素;derection:以什么方向为参照来运动(left、right、top、bottom);speed:计时器每循环一次移动的距离;subjectPosition:运动所要达到的样式的值
        speed= parseInt(getstyle(getid(id),derection))> subjectPosition? -speed : speed;//判断运动的方向
        clearInterval(getid(id).timer);
        getid(id).timer=setInterval(function(){
            var nowPosition =parseInt(getstyle(getid(id),derection));
            var thisPosition=nowPosition+speed;
            if(thisPosition>=subjectPosition&&speed>0||thisPosition<=subjectPosition&&speed<0){
            //判断有无达到目标点
                thisPosition=subjectPosition;
                clearInterval(getid(id).timer);
            }
            getid(id).style[derection]=thisPosition+'px';
        },30)
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

例如:模拟一些网页,做一个“点击有惊喜”的效果。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#div1{width:800px; height:800px; background:#000; position:relative;left:50%; margin-left:-400px;}
#div2{width:30px; height:200px; background:#666; color:#fff;text-align:center; padding-top:50px; position:absolute; right:0; top:40%; cursor:pointer;}
#div3{width:80px; height:200px; background:#69F; color:#909;text-align:center; padding-top:50px; position:absolute; right:-80px; top:40%;}
</style>
</head>

<body>
<div id="div1">
    <div id="div2">点击有惊喜</div>
    <div id="div3">啦啦啦啦啦啦我就是惊喜!</div>
</div>
<script src="myjs/myjs.js"></script>
<script>
    var click=true;
    getid('div2').onclick =function(){
        if(click==true){
            doMove('div2','right',10, parseInt(getstyle(getid('div3'),'width')));
            doMove('div3','right',10,0);
            click=false;
        }else{
            doMove('div2','right',10,0);
            doMove('div3','right',10,-parseInt(getstyle(getid('div3'),'width')));
            click=true;
        }
    }
    function doMove(id,derection,speed,subjectPosition){
        speed= parseInt(getstyle(getid(id),derection))> subjectPosition? -speed : speed;
        clearInterval(getid(id).timer);
        getid(id).timer=setInterval(function(){
            var nowPosition =parseInt(getstyle(getid(id),derection));
            var thisPosition=nowPosition+speed;
            if(thisPosition>=subjectPosition&&speed>0||thisPosition<=subjectPosition&&speed<0){
                thisPosition=subjectPosition;
                clearInterval(getid(id).timer);
            }
            getid(id).style[derection]=thisPosition+'px';
        },30)
    }
</script>
</body>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

未点击之前: 
这里写图片描述

点击之后: 
这里写图片描述

domove()函数封装好了,接下来说一下JavaScript的回调函数:

1.定义:回调函数就是把我们一个函数执行完毕后再随即执行的一个函数; 
2.但是回调函数不是一定有的,因此在一段程序结束的地方需要做一个判断:if(endFn){endFn();} 
也可以写为:endFn&&endFn(); 
这两者等价 
注:未定义就返回未定义,否则在一段程序执行完后紧接着执行回调函数里面的内容。

例:利用回调函数让一个div做向右、向下、向左再向上回到原点的运动(当然,运动到上、下、左、右的边界值都已经提前设定好)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#div1{width:100px; height:100px; background:red; position:absolute; top:40px; left:40px;}
</style>
</head>

<body>
<input type="button" value="走" id="btn1" />
<div id="div1"></div>
<script src="myjs/myjs.js"></script>
<script>
    getid('btn1').onclick =function(){
        doMove('div1','left',10,800,function endFn(){
            doMove('div1','top',10,500,function endFn(){
                doMove('div1','left',10,40,function endFn(){
                    doMove('div1','top',10,40);
                })
            });
        });
    };
    function doMove(id,derection,speed,subjectPosition,endFn){
        speed= parseInt(getstyle(getid(id),derection))> subjectPosition? -speed : speed;
        clearInterval(getid(id).timer);
        getid(id).timer=setInterval(function(){
            var nowPosition =parseInt(getstyle(getid(id),derection));
            var thisPosition=nowPosition+speed;
            if(thisPosition>=subjectPosition&&speed>0||thisPosition<=subjectPosition&&speed<0){
                thisPosition=subjectPosition;
            }
            getid(id).style[derection]=thisPosition+'px';
            if(parseInt(getstyle(getid(id),derection))===subjectPosition){
                clearInterval(getid(id).timer);
                endFn&&endFn();
            }
        },30)
    }
</script>
</body>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

接下来就是关于以上内容的练习:

练习1:写四个按钮,分别是“向左”、“向右”、“向上”和“向下”,点击哪个按钮就让元素向相应的方向移动,并且遇到四边设定的边界就停止运动

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#div1{width:100px; height:100px; background:red; position:absolute; top:40px; left:40px;}
</style>
</head>

<body>
<input type="button" value="向右" id="btn1" />
<input type="button" value="向左" id="btn2" />
<input type="button" value="向下" id="btn3" />
<input type="button" value="向上" id="btn4" />
<div id="div1"></div>
<script src="myjs/myjs.js"></script>
<script>
    getid('btn1').onclick =function(){doMove('div1','left',10,800);};
    getid('btn2').onclick =function(){doMove('div1','left',10,40);};
    getid('btn3').onclick =function(){doMove('div1','top',10,800);};
    getid('btn4').onclick =function(){doMove('div1','top',10,40);};
    function doMove(id,derection,speed,subjectPosition){
        speed= parseInt(getstyle(getid(id),derection))> subjectPosition? -speed : speed;
        clearInterval(getid(id).timer);
        getid(id).timer=setInterval(function(){
            var nowPosition =parseInt(getstyle(getid(id),derection));
            var thisPosition=nowPosition+speed;
            if(thisPosition>=subjectPosition&&speed>0||thisPosition<=subjectPosition&&speed<0){
                thisPosition=subjectPosition;
                clearInterval(getid(id).timer);
            }
            getid(id).style[derection]=thisPosition+'px';
        },30)
    }
</script>
</body>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

练习2:在页面上布局平铺20个50X50红色的方块,点击我们的浏览器的窗口时让每个方块每隔0.2秒以每0.1秒30像素的速度掉下来一个,直到所有的方块掉到500的位置

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
ul,li{margin:0; padding:0;}
li{list-style:none;width:50px; height:50px; background:#666; position:absolute; top:0;;}
</style>
</head>
<ul id="ul1">
</ul>
<body>
<script src="myjs/myjs.js"></script>
<script>
    var oUl1=document.getElementById("ul1");
    var aLi=oUl1.getElementsByTagName("li");
    var str="";
    for(var i=0;i<20;i++){
        str+="<li></li>";
    }
    oUl1.innerHTML=str;
    for(var i=0;i<20;i++){
        aLi[i].style.left=(i+1)*51+"px";
    }
    window.onclick=function(){
        var i=0;
        setInterval(function(){
            doMove(aLi[i],'top',30,500);
            i++;
        },200)//此处是在计时器里的循环,相当于双重循环
    }
    function doMove(id,derection,speed,subjectPosition){
        speed= parseInt(getstyle(getid(id),derection))> subjectPosition? -speed : speed;
        clearInterval(getid(id).timer);
        getid(id).timer=setInterval(function(){
            var nowPosition =parseInt(getstyle(getid(id),derection));
            var thisPosition=nowPosition+speed;
            if(thisPosition>=subjectPosition&&speed>0||thisPosition<=subjectPosition&&speed<0){
                thisPosition=subjectPosition;
                clearInterval(getid(id).timer);
            }
            getid(id).style[derection]=thisPosition+'px';
        },30)
    }
</script>
</body>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

效果图大致是这样的: 
这里写图片描述

2.正反方向“有缝”轮播图(哈哈,后面再说无缝轮播图)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="common.css" type="text/css" rel="stylesheet"/>
<style type="text/css">
#ul1{position:relative;}
#ul1 li{width:400px; height:500px; float:left;}
#div1{width:400px; height:500px; position:absolute; top:0; left:0; bottom:0; right:0; margin:auto; overflow:hidden;}
</style>
</head>
<body>
<div id="div1">
    <ul id="ul1" class="clearfix">
        <li><img src="img/1.png" width="400" height="500"/></li>
        <li><img src="img/2.png" width="400" height="500"/></li>
        <li><img src="img/3.png" width="400" height="500"/></li>
        <li><img src="img/4.png" width="400" height="500"/></li>
    </ul>
</div>
<button>正方向轮播</button>
<button>反方向轮播</button>
<script src="myjs/myjs.js"></script>
<script>
var aBtn=document.getElementsByTagName("button");
var timer=null;
var num=0;
var aLi =getid('ul1').getElementsByTagName('li');
aBtn[0].onclick=function(){fn(aLi.length-1,0,"++");}
aBtn[1].onclick=function(){fn(0,aLi.length-1,"--");}
/*将变换图片轮播方向的这段代码封装在JS文件内。
    aBtn[0].onclick=function(){
    clearInterval(timer);
    var aLi =getid('ul1').getElementsByTagName('li');
    getid('ul1').style.width = parseInt(getstyle(aLi[0],'width'))*aLi.length +'px';
    var num = 1;
    timer=setInterval(function(){
        doMove('ul1','left',20,-num*parseInt(getstyle(aLi[0],'width')));
        if(num ==aLi.length-1){
            num = 0;
        }else{
            num++;
        }

    },4000);
}
aBtn[1].onclick=function(){
    clearInterval(timer);
    var aLi =getid('ul1').getElementsByTagName('li');
    getid('ul1').style.width = parseInt(getstyle(aLi[0],'width'))*aLi.length +'px';
    var num = 3;
    timer=setInterval(function(){
        doMove('ul1','left',20,-num*parseInt(getstyle(aLi[0],'width')));
        if(num ==0){
            num = aLi.length-1;
        }else{
            num--;
        }

    },4000);
}*/
</script>
</body>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值