码迷,mamicode.com
首页 > Web开发 > 详细

JS -- 拖动特效

时间:2015-08-09 20:24:04      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

JS – 拖动特效

JS – 拖动特效

// 元素的 position 一定要为 absolute

window.onload = function () {
    var oDiv = document.getElementById(‘div1‘);
    oDiv.onmousedown = function (ev)    {
        var oEvent = ev||event;
        var disX= oEvent.clientX - oDiv.offsetLeft;
        var disY= oEvent.clientY - oDiv.offsetTop;
        document.onmousemove = function (ev) {
            var oEvent = ev||event;
            oDiv.style.left = oEvent.clientX - disX + ‘px‘;
            oDiv.style.top = oEvent.clientY - disY + ‘px‘;
        };
        document.onmouseup  =  function () {
            document.onmousemove = null;
            document.onmouseup = null;
        };
    };
};

JS -- 拖动特效

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4716176.html

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