Visualizzazione dei risultati da 1 a 2 su 2

Discussione: drag e drop div

  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2007
    Messaggi
    371

    drag e drop div

    Salve vi illustro cosa vorrei realizzare e poi dove sono arrivato ad impantanarmi:
    3 div contenitori ognuno allinterno di un proprio div "box" solo il contenitore si sposta e puo andare dentro un altro box ma a quel punto il contenitore dell'altro box deve andare al posto dell'altro.
    Ossia se io sposto il contenitore1 nel box2 nello stesso istante il contenitore2 deve andare nel box1
    Spero di essermi spiegato....vi posto tutto il codice
    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .box-1{
    position:absolute;
    left:50px;
    top:27px;
    border:2px solid #900;
    padding:0.4em;
    width:116px;
    background-color:#fafafa;
    color: #900;
    }
    .box-2{
    position:absolute;
    left:212px;
    top:27px;
    border:2px solid #900;
    padding:0.4em;
    width:147px;
    background-color:#fafafa;
    color: #900;
    }
    .box-3{
    position:absolute;
    left:405px;
    top:27px;
    border:2px solid #900;
    padding:0.4em;
    width:170px;
    background-color:#fafafa;
    color: #900;
    }
    #drag11{
    width:100px;
    height:20px;
    position:relative;
    background-color:red;
    }
    #drag21{
    width:100px;
    height:20px;
    position:relative;
    background-color:blue;

    }
    #drag31{
    width:100px;
    height:20px;
    position:relative;
    background-color:green;
    }
    </style>
    <script>
    function allowDrop(ev) {
    ev.preventDefault();
    }
    function drag(ev) {
    ev.dataTransfer.setData("<div>", ev.target.id);
    }
    function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("<div>");
    ev.target.appendChild(document.getElementById(data ));
    }
    window.addEvent('domready', function () {
    $('#draggables div').each(function (drag) {
    new Drag.Move(drag);
    });
    });
    </script>
    </head>
    <body>
    <div class="box-1">
    <div id="drag11" draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">contenitore1 </div>
    </div>
    <div class="box-2" >
    <div id="drag21" draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">contenitore2</div>
    </div>
    <div class="box-3">
    <div id="drag31" draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">contenitore3</div>
    </div>
    </body>
    </html>

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2007
    Messaggi
    371

    RISOLTO COSI'

    <!DOCTYPE HTML>
    <html>
    <head>
    <link rel="stylesheet" href="css/page.css" type="text/css" />
    <script type="text/javascript" src="animatedcollapse.js"></script>
    </head>
    <body>
    <div id="widgets" class="colum1">
    <div class="widget" draggable="true">
    <header>A[hide][show]</header>
    <div id="widget1">box1</div>
    </div>
    <div class="widget" draggable="true">
    <header>B[hide][show]</header>
    <div id="widget2">box2</div>
    </div>
    <div class="widget" draggable="true">
    <header>C[hide][show]</header>
    <div id="widget3">box3</div>
    </div>
    <div class="widget" draggable="true">
    <header>D[hide][show]</header>
    <div id="widget4">box4</div>
    </div>
    <div class="widget" draggable="true">
    <header>E[hide][show]</header>
    <div id="widget5">box5</div>
    </div>
    </div>
    <script type="text/javascript" src="drag.js"></script>
    <script type="text/javascript">
    //Syntax: var uniquevar=new animatedcollapse("DIV_id", animatetime_milisec, enablepersist(true/fase), [initialstate] )
    var collapse1 = new animatedcollapse("widget1", 300, true)
    var collapse2 = new animatedcollapse("widget2", 300, true)
    var collapse3 = new animatedcollapse("widget3", 300, true)
    var collapse4 = new animatedcollapse("widget4", 300, true)
    var collapse5 = new animatedcollapse("widget5", 300, true)
    </script>
    </body>
    </html>

    /** I FILE JS **/
    /**drag.js**/
    (function () {
    var id_ = 'widgets';
    var widgets_ = document.querySelectorAll('#' + id_ + ' .widget');
    var dragSrcEl_ = null;

    this.handleDragStart = function (e) {
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.innerHTML);

    dragSrcEl_ = this;

    this.style.opacity = '0.5';

    // this/e.target is the source node.
    this.addClassName('moving');
    };

    this.handleDragOver = function (e) {
    if (e.preventDefault) {
    e.preventDefault(); // Allows us to drop.
    }

    e.dataTransfer.dropEffect = 'move';

    return false;
    };

    this.handleDragEnter = function (e) {
    this.addClassName('over');
    };

    this.handleDragLeave = function (e) {
    // this/e.target is previous target element.

    this.removeClassName('over');
    };

    this.handleDrop = function (e) {
    // this/e.target is current target element.

    if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
    }

    // Don't do anything if we're dropping on the same box we're dragging.
    if (dragSrcEl_ != this) {
    dragSrcEl_.innerHTML = this.innerHTML;
    this.innerHTML = e.dataTransfer.getData('text/html');
    }

    return false;
    };

    this.handleDragEnd = function (e) {
    // this/e.target is the source node.
    this.style.opacity = '1';

    [].forEach.call(widgets_, function (widget) {
    widget.removeClassName('over');
    widget.removeClassName('moving');
    });
    };

    [].forEach.call(widgets_, function (widget) {
    widget.setAttribute('draggable', 'true'); // Enable boxes to be draggable.
    widget.addEventListener('dragstart', this.handleDragStart, false);
    widget.addEventListener('dragenter', this.handleDragEnter, false);
    widget.addEventListener('dragover', this.handleDragOver, false);
    widget.addEventListener('dragleave', this.handleDragLeave, false);
    widget.addEventListener('drop', this.handleDrop, false);
    widget.addEventListener('dragend', this.handleDragEnd, false);
    });
    })();

    /** animatecollapse.js **/
    //Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)
    //Last updated June 27th, 07'. Added ability for a DIV to be initially expanded.

    var uniquepageid=window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, "") //get current page path and name, used to uniquely identify this page for persistence feature

    function animatedcollapse(divId, animatetime, persistexpand, initstate){
    this.divId=divId
    this.divObj=document.getElementById(divId)
    this.divObj.style.overflow="hidden"
    this.timelength=animatetime
    this.initstate=(typeof initstate!="undefined" && initstate=="block")? "block" : "contract"
    this.isExpanded=animatedcollapse.getCookie(uniquep ageid+"-"+divId) //"yes" or "no", based on cookie value
    this.contentheight=parseInt(this.divObj.style.heig ht)
    var thisobj=this
    if (isNaN(this.contentheight)){ //if no CSS "height" attribute explicitly defined, get DIV's height on window.load
    animatedcollapse.dotask(window, function(){thisobj._getheight(persistexpand)}, "load")
    if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
    this.divObj.style.visibility="hidden" //hide content (versus collapse) until we can get its height
    }
    else if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
    this.divObj.style.height=0 //just collapse content if CSS "height" attribute available
    if (persistexpand)
    animatedcollapse.dotask(window, function(){animatedcollapse.setCookie(uniquepageid +"-"+thisobj.divId, thisobj.isExpanded)}, "unload")
    }

    animatedcollapse.prototype._getheight=function(per sistexpand){
    this.contentheight=this.divObj.offsetHeight
    if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes"){ //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
    this.divObj.style.height=0 //collapse content
    this.divObj.style.visibility="visible"
    }
    else //else if persistence is enabled AND this content should be expanded, define its CSS height value so slideup() has something to work with
    this.divObj.style.height=this.contentheight+"px"
    }


    animatedcollapse.prototype._slideengine=function(d irection){
    var elapsed=new Date().getTime()-this.startTime //get time animation has run
    var thisobj=this
    if (elapsed<this.timelength){ //if time run is less than specified length
    var distancepercent=(direction=="down")? animatedcollapse.curveincrement(elapsed/this.timelength) : 1-animatedcollapse.curveincrement(elapsed/this.timelength)
    this.divObj.style.height=distancepercent * this.contentheight +"px"
    this.runtimer=setTimeout(function(){thisobj._slide engine(direction)}, 10)
    }
    else{ //if animation finished
    this.divObj.style.height=(direction=="down")? this.contentheight+"px" : 0
    this.isExpanded=(direction=="down")? "yes" : "no" //remember whether content is expanded or not
    this.runtimer=null
    }
    }


    animatedcollapse.prototype.slidedown=function(){
    if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
    if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
    alert("Please wait until document has fully loaded then click again")
    else if (parseInt(this.divObj.style.height)==0){ //if content is collapsed
    this.startTime=new Date().getTime() //Set animation start time
    this._slideengine("down")
    }
    }
    }

    animatedcollapse.prototype.slideup=function(){
    if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
    if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
    alert("Please wait until document has fully loaded then click again")
    else if (parseInt(this.divObj.style.height)==this.contenth eight){ //if content is expanded
    this.startTime=new Date().getTime()
    this._slideengine("up")
    }
    }
    }

    animatedcollapse.prototype.slideit=function(){
    if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
    alert("Please wait until document has fully loaded then click again")
    else if (parseInt(this.divObj.style.height)==0)
    this.slidedown()
    else if (parseInt(this.divObj.style.height)==this.contenth eight)
    this.slideup()
    }

    // -------------------------------------------------------------------
    // A few utility functions below:
    // -------------------------------------------------------------------

    animatedcollapse.curveincrement=function(percent){
    return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
    }


    animatedcollapse.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
    var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
    if (target.addEventListener)
    target.addEventListener(tasktype, functionref, false)
    else if (target.attachEvent)
    target.attachEvent(tasktype, functionref)
    }

    animatedcollapse.getCookie=function(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }

    animatedcollapse.setCookie=function(name, value, days){
    if (typeof days!="undefined"){ //if set persistent cookie
    var expireDate = new Date()
    var expstring=expireDate.setDate(expireDate.getDate()+ days)
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()
    }
    else //else if this is a session only cookie
    document.cookie = name+"="+value
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.