码迷,mamicode.com
首页 > 其他好文 > 详细

简单的入门小动画

时间:2014-12-09 22:44:02      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    #div1{height:100px;width:100px;position:absolute;top:50;left:10px;background:red;}
</style>
<body>
    <input type="button" value="向前" id = ‘btn1‘>
    <input type="button" value="向后" id = ‘btn2‘>
    <div id="div1"></div>
    <script>
        function getEle(_id){
            return document.getElementById(_id);
        }
        var oBtn1 = getEle(btn1);
        var oBtn2 = getEle(btn2);
        var oDiv = getEle(div1);
        oBtn1.onclick = function(){
            doMove(oDiv, 12, 800);
        }
        oBtn2.onclick = function(){
            doMove(oDiv, -12, 10);
        }
        function doMove(obj, dir, target){
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
                var speed = parseInt(getStyle(obj,left)) + dir;
                if(speed > target && dir > 0){
                    speed = target;
                }
                if(speed < target && dir < 0){
                    speed = target;
                }
                obj.style.left = speed + px;
                if(speed == target){
                    clearInterval(obj.timer);
                }
                },30)
            
        }
        function getStyle(obj,attr){
            return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
        }
        
    </script>
</body>
</html>

 

简单的入门小动画

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/clearfix/p/4154213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!