• js拖入拖出效果


    <!DOCTYPE html>
    <html>
    <head>
    <title>js拖进和拖出效果</title>
    <style type="text/css">
    .parent{
    position: relative;
    }
    #drop{
    300px;
    height: 200px;
    background-color: #ff0000;
    padding: 5px;
    border:2px solid #000000;
    position: absolute;
    left: 200px;
    top: 0px;
    overflow: hidden;
    }
    #item{
    100px;
    height: 100px;
    background-color: #ffff00;
    padding: 5px;
    position: absolute;
    left: 0;
    top:0;
    border:1px dashed #000000;
    }
    #copyitem0,#copyitem1,#copyitem2,#copyitem3{
    100px;
    height: 100px;
    background-color: #ffff00;
    padding: 5px;
    left: 0;
    top:0;
    float: left;
    border:1px dashed #000000;}
    *[draggable=true]{
    -webkit-user-select:none;/*禁止选择文字*/
    -khtml-user-drag:element;/*兼容safai 设置元素可拖拽*/
    cursor: move;
    }
    *:-khtml-drag{
    background-color: rgba(238,238,238,0.5);
    }
    </style>
    <script type="text/javascript">
    /*兼容性*/
    function listenEvent(eventTarget,eventType,eventHandler){
    if(eventTarget.addEventListener){
    eventTarget.addEventListener(eventType,eventHandler,false);
    }else if(eventTarget.attachEvent){
    eventType="on"+eventType;
    eventTarget.attachEvent(eventType,eventHandler);
    }else{
    eventTarget["on"+eventType]=eventHandler;
    }
    }

    //取消事件默认动作
    function cancelEvent(event){
    if(event.preventDefault){
    event.preventDefault();
    }else{
    event.returnValue=false;
    }
    }

    //阻止捕获和冒泡阶段中当前事件的进一步传播
    function cancelPropagation(event){
    if(event.stopPropagation){
    event.stopPropagation();
    }else{
    event.cancelBubble=true;
    }
    }

    window.onload=function(){
    var index=0;//标记拖拽元素个数
    var target=document.getElementById("drop");//目标元素
    listenEvent(target,"dragenter",cancelEvent);
    listenEvent(target,"dragover",dragOver);
    listenEvent(target,"drop",function(evt){
    cancelPropagation(evt);
    evt=evt||window.event;
    evt.dataTransfer.dropEffect='copy';
    /*dataTransfer对象用来传递拖拽的数据。
    *dataTransfer对象有 getData()和setData()两个主要方法,
    *操作dataTransfer中携带的数据
    */
    var id=evt.dataTransfer.getData("Text");
    var item=document.getElementById(id);
    /*将拖拽div复制一份放入目标div中*/
    var copyitem=document.createElement("div");
    var idstr="copyitem"+index;
    copyitem.setAttribute("id",idstr);
    copyitem.innerHTML=item.innerHTML+index;
    copyitem.setAttribute("draggable","true");
    listenEvent(copyitem,"dragstart",function(evt){
    evt=evt||window.event;
    evt.dataTransfer.effectAllowed='copy';
    evt.dataTransfer.setData("Text",copyitem.id);

    });
    target.appendChild(copyitem);
    index++;
    /*拖出目标元素*/
    var target2=document.getElementById("item");//目标元素
    listenEvent(target2,"dragenter",cancelEvent);
    listenEvent(target2,"dragover",dragOver);
    listenEvent(target2,"drop",function(evt){
    cancelPropagation(evt);
    evt=evt||window.event;
    evt.dataTransfer.dropEffect='copy';
    var ids=evt.dataTransfer.getData("Text");

    removeElementDiv(ids);

    });

    });
    var item=document.getElementById("item");
    item.setAttribute("draggable","true");

    listenEvent(item,"dragstart",function(evt){
    evt=evt||window.event;
    evt.dataTransfer.effectAllowed='copy';
    evt.dataTransfer.setData("Text",item.id);
    });

    }

    function dragOver(evt){
    if(evt.preventDefault){
    evt.preventDefault();
    }
    evt=evt||window.event;
    evt.dataTransfer.dropEffect='copy';
    return false;
    }

    /*元素拖出去执行删除*/
    function removeElementDiv(el){
    var child = document.getElementById(el);
    if(!child){return;}
    child.parentNode.removeChild(child);
    }
    </script>
    </head>
    <body>
    <div>
    <p>把黄色小块拖入红色大方框中</p>
    </div>
    <div class="parent">
    <!-- onselectstart="return false;"禁止选择文字兼容IE -->
    <div id="item" draggable="true" onselectstart="return false;">hello</div>
    <div id="drop"></div>
    </div>

    </body>
    </html>

  • 相关阅读:
    第六章学习小结
    malloc iOS
    iOS事件传递机制
    对 runloop 的理解
    深恶痛绝重写setter和getter
    数据库常见问题总结
    iOS多应用自动打包
    一段文字中包含多种语言时行间距问题
    一个成熟应用的排版方案
    Flask纪要
  • 原文地址:https://www.cnblogs.com/zyx-blog/p/9336828.html
Copyright © 2020-2023  润新知