cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <ol data-draggable="target">
		  <li data-draggable="item">Item 0</li>
  	<li data-draggable="item">Item 1</li>
  	<li data-draggable="item">Item 2</li>
  	<li data-draggable="item">Item 3</li>
</ol>

<ol data-draggable="target">
  	<li data-draggable="item">Item 4</li>
  	<li data-draggable="item">Item 5</li>
</ol>

<ol data-draggable="target">
  	<li data-draggable="item">Item 6</li>
	  <li data-draggable="item">Item 7</li>
</ol>

<ol data-draggable="target">
  	<li data-draggable="item">Item 8</li>
</ol>

            
          
!
            
              /* canvas styles */
html, body
{
	font:normal normal normal 100%/1.4 tahoma, sans-serif;
	background:#f9f9f9;
	color:#000;
}
body
{
	font-size:0.8em;
}

/* draggable targets */
[data-draggable="target"]
{
	float:left;
	list-style-type:none;
	
	width:42%;
	height:7.5em;
	overflow-y:auto;
	
	margin:0 0.5em 0.5em 0;
	padding:0.5em;
	
	border:2px solid #888;
	border-radius:0.2em;
	
	background:#ddd;
	color:#555;
}

/* draggable items */
[data-draggable="item"]
{
	display:block;
	list-style-type:none;
	
	margin:0 0 2px 0;
	padding:0.2em 0.4em;
	
	border-radius:0.2em;
	
	line-height:1.3;
}

/* items focus state */
[data-draggable="item"]:focus
{
	outline:none;
	
	box-shadow:0 0 0 2px #68b, inset 0 0 0 1px #ddd;
}

/* items grabbed state */
[data-draggable="item"][aria-grabbed="true"]
{
	background:#8ad;
	color:#fff;
}

            
          
!
            
              (function()
{

    //exclude older browsers by the features we need them to support
    //and legacy opera explicitly so we don't waste time on a dead browser
    if
    (
        !document.querySelectorAll 
        || 
        !('draggable' in document.createElement('span')) 
        || 
        window.opera
    ) 
    { return; }

    //get the collection of draggable items and add their draggable attributes
    for(var 
        items = document.querySelectorAll('[data-draggable="item"]'), 
        len = items.length, 
        i = 0; i < len; i ++)
    {
        items[i].setAttribute('draggable', 'true');
        items[i].setAttribute('aria-grabbed', 'false');
        items[i].setAttribute('tabindex', '0');
    }



    //dictionary for storing the selections data 
    //comprising an array of the currently selected items 
    //and a reference to the selected items' owning container
    var selections = 
    {
        items : [],
        owner : null
    };
    
    //function for selecting an item
    function addSelection(item)
    {
        //if the owner reference is still null, set it to this item's parent
        //so that further selection is only allowed within the same container
        if(!selections.owner)
        {
            selections.owner = item.parentNode;
        }
        
        //or if that's already happened then compare it with this item's parent
        //and if they're not the same container, return to prevent selection
        else if(selections.owner != item.parentNode)
        {
            return;
        }
                
        //set this item's grabbed state
        item.setAttribute('aria-grabbed', 'true');
        
        //add it to the items array
        selections.items.push(item);
    }
    
    //function for unselecting an item
    function removeSelection(item)
    {
        //reset this item's grabbed state
        item.setAttribute('aria-grabbed', 'false');
        
        //then find and remove this item from the existing items array
        for(var len = selections.items.length, i = 0; i < len; i ++)
        {
            if(selections.items[i] == item)
            {
                selections.items.splice(i, 1);
                break;
            }
        }
    }
    
    //function for resetting all selections
    function clearSelections()
    {
        //if we have any selected items
        if(selections.items.length)
        {
            //reset the owner reference
            selections.owner = null;

            //reset the grabbed state on every selected item
            for(var len = selections.items.length, i = 0; i < len; i ++)
            {
                selections.items[i].setAttribute('aria-grabbed', 'false');
            }

            //then reset the items array        
            selections.items = [];
        }
    }

    //shorctut function for testing whether a selection modifier is pressed
    function hasModifier(e)
    {
        return (e.ctrlKey || e.metaKey || e.shiftKey);
    }
    
    

    //mousedown event to implement single selection
    document.addEventListener('mousedown', function(e)
    {
        //if the element is a draggable item
        if(e.target.getAttribute('draggable'))
        {
            //if the multiple selection modifier is not pressed 
            //and the item's grabbed state is currently false
            if
            (
                !hasModifier(e) 
                && 
                e.target.getAttribute('aria-grabbed') == 'false'
            )
            {
                //clear all existing selections
                clearSelections();
            
                //then add this new selection
                addSelection(e.target);
            }
        }
        
        //else [if the element is anything else]
        //and the selection modifier is not pressed 
        else if(!hasModifier(e))
        {
            //clear all existing selections
            clearSelections();
        }

    }, false);
    
    //mouseup event to implement multiple selection
    document.addEventListener('mouseup', function(e)
    {
        //if the element is a draggable item 
        //and the multipler selection modifier is pressed
        if
        (
            e.target.getAttribute('draggable') 
            && 
            hasModifier(e)
        )
        {
            //if the item's grabbed state is currently true
            if(e.target.getAttribute('aria-grabbed') == 'true')
            {
                //unselect this item
                removeSelection(e.target);
                
                //if that was the only selected item 
                //then reset the owner container reference            
                if(!selections.items.length)
                {
                    selections.owner = null;
                }
            }
            
            //else [if the item's grabbed state is false]
            else
            {
                //add this additional selection
                addSelection(e.target);
            }
        }
        
    }, false);

    //dragstart event to initiate mouse dragging
    document.addEventListener('dragstart', function(e)
    {
        //if the element's parent is not the owner, then block this event
        if(selections.owner != e.target.parentNode)
        {
            e.preventDefault();
            return;
        }
                
        //[else] if the multiple selection modifier is pressed 
        //and the item's grabbed state is currently false
        if
        (
            hasModifier(e) 
            && 
            e.target.getAttribute('aria-grabbed') == 'false'
        )
        {
            //add this additional selection
            addSelection(e.target);
        }
        
        //we don't need the transfer data, but we have to define something
        //otherwise the drop action won't work at all in firefox
        //most browsers support the proper mime-type syntax, eg. "text/plain"
        //but we have to use this incorrect syntax for the benefit of IE10+
        e.dataTransfer.setData('text', '');
    
    }, false);

})();    

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console