zoukankan      html  css  js  c++  java
  • JS拖拽

    代码
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>runcode</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="Author" content="Sheneyan" />
    <script type="text/javascript">
    /**
    *取得元素的真实css属性
    *@param {Object} d 元素
    *@param {String} a 元素的css属性名
    *@version 0.2
    */
    function gs(d,a){
    if (d.currentStyle){
    var curVal=d.currentStyle[a]
    }
    else{
    var curVal=document.defaultView.getComputedStyle(d, null)[a]
    }
    return curVal;
    }
    /**
    * 取得鼠标坐标
    * @return Position
    */
    function getMouseLocation(e){
    if(!document.all){
    mouseX
    = e.pageX;
    mouseY
    = e.pageY;
    }
    else{
    mouseX
    = event.x + document.body.scrollLeft;
    mouseY
    = event.y + document.body.scrollTop;
    }
    return {x:mouseX,y:mouseY};
    }
    /**
    * 拖动对象
    * @param {DOM Object} DOM对象
    */
    function drag(e,obj){
    var p1 = getMouseLocation(e);
    var startRight = null;
    var startTop = null;
    var startLeft = null;
    var startBottom = null;
    var l = gs(obj,"left");
    var r = gs(obj,"right");
    var t = gs(obj,"top");
    var b = gs(obj,"bottom");
    if(!l)
    startRight
    = parseInt(r);
    else
    startLeft
    = parseInt(l);
    if(!t)
    startBottom
    = parseInt(b);
    else
    startTop
    = parseInt(t);
    if(obj.setCapture)
    obj.setCapture();
    else if (window.captureEvents)
    window.captureEvents(Event.MOUSEMOVE
    |Event.MOUSEUP);
    document.onmousemove
    = function(e){
    var p2 = getMouseLocation(e);
    var xMove = p2.x-p1.x;
    var yMove = p2.y-p1.y;
    if(!l)
    obj.style.right
    = (startRight - xMove)+"px";
    else
    obj.style.left
    = (startLeft + xMove)+"px";
    if(!t)
    obj.style.bottom
    = (startBottom - yMove)+"px";
    else
    obj.style.top
    = (startTop + yMove)+"px";
    }
    document.onmouseup
    = function(){
    if(obj.releaseCapture)
    obj.releaseCapture();
    else if (window.captureEvents)
    window.captureEvents(Event.MOUSEMOVE
    |Event.MOUSEUP);
    document.onmouseup
    = null;
    document.onmousemove
    = null;
    }
    }
    </script>
    <style type="text/css">
    div#test
    {border:solid 1px blue;background:red;position:absolute;left:100px;top:200px;width:200px;height:200px;cursor:pointer;}
    </style>
    </head>
    <body>
    <div id="test" onmousedown="drag(event,this)">拖我拖我拖我拖我拖我拖我拖我<div>
    </body>
    </html>
  • 相关阅读:
    FTP(文件传输协议)工作原理
    Web测试和App测试有什么区别
    JMeter中文版用户手册
    shell常用命令
    RAID技术介绍和总结
    SQL Insert语句数据以以unicode码存储 解决存储数据出现乱码的问题
    IIS 允许无后缀文件访问的配置
    cocos2dx 背景用小尺寸图片滚动填充的方法
    (转)Windows7 64位系统搭建Cocos2d-x 2.2.1最新版以及Android交叉编译环境(详细教程) .
    大数据应用期末总评
  • 原文地址:https://www.cnblogs.com/nikyxxx/p/1706239.html
Copyright © 2011-2022 走看看