JavaScript-通过js实现IE8兼容的div曲线运动

JavaScript-通过js实现IE8兼容的div曲线运动

想挽留 发布于 2016-10-26 字数 150 浏览 1068 回复 1

试过用animate,但只能直线运动,用css3的transform,虽然能够实现,但是不兼容IE8,因为对原生的js不大熟悉,请问在js中有没有可以实现曲线运动效果的?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2017-02-03 1 楼

当然可以的,给你一个简单的demo吧,具体的代码你自己研究一下.看的明白了,那你的水平就上一个层次了.

<!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 type="text/css">
body,div{margin:0;padding:0;}
div{position:absolute;width:98px;height:38px;background:url(http://theme/images/logo.png) no-repeat;top:100px;left:50px;}
p,input{margin:10px;}
</style>
<script type="text/javascript">
window.onload = function ()
{
    var oDiv = document.getElementsByTagName("div")[0];
    var aInput = document.getElementsByTagName("input");
    var oP = document.getElementsByTagName("p")[0];
    var i = 0;
    
    aInput[0].onclick = function (event)
    {       
        (event || window.event).cancelBubble = true;
        clearEvent();
        this.value += "(已激活)";
        oP.innerHTML = "鼠标点击页面, 人物将移动至鼠标位置!";
        document.onclick = function (event)
        {
            var event = event || window.event;
            oDiv.style.background = "url(http://imtheme/images/logo.png) no-repeat";
            startMove(oDiv, {x:event.clientX, y:event.clientY}, function(){oDiv.style.background = "url(http://images/logo.png) no-repeat"});
            return false;
        }   
    };
    
    aInput[1].onclick = function (event)
    {       
        (event || window.event).cancelBubble = true;
        clearEvent();
        this.value += "(已激活)";
        oP.innerHTML = "按住鼠标左键,在页面划动,人物将按照鼠标轨迹移动。"
        var aPos = [{x:oDiv.offsetLeft, y:oDiv.offsetTop}];
        document.onmousedown = function (event)
        {
            var event = event || window.event;          
            aPos.push({x:event.clientX, y:event.clientY});
            document.onmousemove = function (event)
            {
                var event = event || window.event;
                aPos.push({x:event.clientX, y:event.clientY});  
                return false;
            }
            return false;
        }
        document.onmouseup = function ()
        {
            document.onmousemove = null;
            oDiv.style.background = "url(http://imgcn/theme/images/logo.png) no-repeat";     
            var timer = setInterval(function ()
            {
                if(aPos.length == 0)
                {
                    clearInterval(timer);
                    oDiv.style.background = "url(http://imgtheme/images/logo.png) no-repeat";
                    return; 
                };
                oDiv.style.left = aPos[0].x + "px";
                oDiv.style.top = aPos[0].y + "px";
                aPos.shift();
            }, 30);
        };
    }
    
    function clearEvent()
    {
        document.onclick = null;
        document.onmousedown = null;
        document.onmousemove = null;
        document.onmouseup = null;
        for (i = 0; i < aInput.length; i++)
        {
            aInput[i].value = aInput[i].value.replace("(已激活)", "");
            aInput[i].onmousedown = aInput[i].onmouseup = function (event)
            {
                (event || window.event).cancelBubble = true;    
            };
        }
    }
};
function startMove(obj, oTarget, fnEnd)
{
    clearInterval(obj.timer);
    obj.timer = setInterval(function ()
    {
        doMove(obj, oTarget, fnEnd) 
    }, 30)  
}
function doMove(obj, oTarget, fnEnd)
{
    var iX = (oTarget.x - obj.offsetLeft) / 5;
    var iY = (oTarget.y - obj.offsetTop) / 5;
    iX = iX > 0 ? Math.ceil(iX) : Math.floor(iX);
    iY = iY > 0 ? Math.ceil(iY) : Math.floor(iY);
    if (oTarget.x == obj.offsetLeft && oTarget.y == obj.offsetTop)
    {
        clearInterval(obj.timer);
        fnEnd && fnEnd();   
    }
    else
    {
        obj.style.left = obj.offsetLeft + iX + "px";
        obj.style.top = obj.offsetTop + iY + "px";  
    }
}
</script>
</head>
<body>
<input type="button" value="根据鼠标点击位置移动" /><input type="button" value="根据标鼠标轨迹移动" />
<p>请点击按钮激活功能!</p>
<div></div>
</body>
</html>