zoukankan      html  css  js  c++  java
  • js实现可拖动div的代码

    正好用到一个需要拖动div的东东,虽然实现方式有很多种,不过我觉得这个还算比较优雅,其实也是网上找的改了改,特此记录下来。
    代码
    var MoveDiv = function(){};

    /**
    * @deprecated 移动div的方法
    * @param{id} id 要移动的层ID
    */
    MoveDiv.Move
    =function(id)
    {
        
    var o=document.getElementById(id);

        o.onselectstart 
    = function()
        {
            
    return(false);
        };

        o.onmousedown 
    = function(e) {
            e 
    = e||window.event;
            
    var x=e.layerX||e.offsetX;
            
    var y=e.layerY||e.offsetY;
            x
    =x-document.body.scrollLeft;
            y
    =y-document.body.scrollTop;
            document.onmousemove 
    = function(e)
            {
                e
    =e||window.event;
                o.style.left
    =(e.clientX-x)+"px";
                o.style.top
    =(e.clientY-y)+"px";
            };

            document.onmouseup
    =function()
            {
                document.onmousemove
    =null;
            };
        };
    }
  • 相关阅读:
    php 数组
    条件语句if else ,switch ,while ,do.while
    if..else 判断中的 Boolean()转换
    wampserver 集成环境
    sublime text 安装及使用
    vue tab切换
    SVG 基础
    gitosis管理员的密钥丢失解决办法
    源码安装MySQL
    Xshell远程登录
  • 原文地址:https://www.cnblogs.com/gaotianle/p/1754837.html
Copyright © 2011-2022 走看看