Minicolors - Dynamic Usage

by bizamajig

HTML

<link rel="stylesheet" href="http://www.ynternet.sk/jq/colorpicker2/jquery.miniColors.css">
<script src="http://www.ynternet.sk/jq/colorpicker2/jquery.miniColors.js"></script>
<div id="add" style="background:yellow; width:100px;cursor:pointer"> add new </div>
<div id="container"> </div>

CSS

#container {
    width:500px;
    height:500px;
    background: palegoldenrod;
    position: relative;
    top:20px; 
    left: 100px;
    padding: 0px;
    z-index:2;
}
.add_to_this { 
    padding:5px;
    position: absolute;
    display:inline-block;
    background: yellowgreen;
    width:200px;
    height:70px;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select: none;
    z-index:100;
}

JavaScript

function handler() {
    if ($(this).find("#menu").length) {
        return;
    }
    $('input').miniColors({                         
       opacity: true
    });
    var currentIndex = parseInt($(this).css('z-index'), 10);
    var newIndex = currentIndex + 1;
    $(this).css('z-index', newIndex);
}
$("#add").on({
    click: function(e) {
        var timestamp = Date.now();
        var posx = Math.floor(Math.random() * 400);
        var posy = Math.floor(Math.random() * 400);
        $('#container').append(function() {
            return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; cursor:pointer;">Click me, drag, change z-index & color <input type="hidden" name="color" value="#FFCC00" data-opacity=".5" /> </div>').click(handler).draggable({
                containment: "#container",
                scroll: false,
                cursor: 'lock'
        });
    });
    }
});