zoukankan      html  css  js  c++  java
  • web拖动Drag&Drop原理

    <html>
    <head>
    <title>Page Title</title>
    <style>

    .drag 
    {
        cursor
    : move;
    }




    .box 
    {
        margin
    : 0px;
        width
    : 200px;
        border
    : 1px solid #ccc;
    }

    .box h3.title 
    {
        margin
    : 0px;
        width
    : 100%;
        background-color
    : #ccc;
    }

    .box div.content 
    {
        margin
    : 0px;
        width
    : 100%;
        text-align
    : left;
    }

    </style>

    <script type="text/javascript">
    //GreatGhoul
    //
    兼容ff, ie
    //
    要拖动的对象的title设置为'dragable'
    //
    拖动点的class设置为'drag',拖动点必须为可拖动对象的子节点

    var DragableObj = {
        handle: 
    null,
        dx: 
    0,
        dy: 
    0,

        init: 
    function(e) {
            e 
    = e || event;
            
    this.handle = e.target || e.srcElement;
            
    if (this.handle.className.indexOf('drag'== -1return;
            while (this.handle.tagName != 'HTML' && this.handle.title != "dragable"
               {
                
    this.handle = this.handle.parentNode || this.handle.parentElement;
            }

            
    if (this.handle.title != 'dragable'return;
            
    this.handle.style.position = 'relative';
            
    this.dx = parseInt(this.handle.style.left + 0- e.clientX;
            
    this.dy = parseInt(this.handle.style.top  + 0- e.clientY;
            document.onmousemove 
    = DragableObj.drag;
        }
    ,
        drag: 
    function(e) {
            e 
    = e || event;
            
    if (this.handle != null{
                
    this.handle.style.left = (e.clientX + this.dx) + 'px';
                
    this.handle.style.top  = (e.clientY + this.dy) + 'px';
            }

        }
    ,
        drop: 
    function(e) {
            
    this.handle = null;
            document.onmousemove 
    = null;
        }

    }
    ;
    document.onmousedown 
    = DragableObj.init;
    document.onmouseup   
    = DragableObj.drop;
    document.onselectstart 
    = function(e) {
        e 
    = e || event;
        eo 
    = e.target || event.srcElement;
        
    if (eo.className.indexOf('drag'!= -1return false;
    }
    ;

    </script>
    </head>
    <body>
    <br>例1:
    <div class="box" title="dragable">
    <h3 class="drag title">拖动标题</h3>
    <div class="content">内容</div>
    </div>

    <br>例2:
    <div class="drag" title="dragable">拖动我</div>

    </body>
    </html>
  • 相关阅读:
    1130
    Oracle 数据库常用操作语句大全
    Oracle用sys登陆报:ORA-28009:connection as sys should be as sysdba
    导出数据报ORA-39002: 操作无效 ORA-39070: 无法打开日志文件。 ORA-39087: 目录名 DUMP_DIR 无效
    SGI STL源码stl_bvector.h分析
    SGI STL源码stl_vector.h分析
    CGI 萃取技术 __type_traits
    迭代器iterator和traits编程技法
    智能指针分析及auto_ptr源码
    C++深拷贝和浅拷贝细节理解
  • 原文地址:https://www.cnblogs.com/hainange/p/6153302.html
Copyright © 2011-2022 走看看