Inventory System - Drag and Drop Resizable Grid
HTML
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
<div>
By: <a href="http://ericeastwood.com/">Eric Eastwood</a>
</div>
<div>
Row:<input type="range" id="row-count" min="1" max="50" step="1" value="3" /><label for="row-count"></label>
<br />
Column:<input type="range" id="column-count" min="1" max="50" step="1" value="3" /><label for="column-count"></label>
</div>
<div class="inventory-table" id="personal-inventory">
<div class="inventory-row">
<div class="inventory-cell">
<div class="inventory-item grape"></div>
</div>
<div class="inventory-cell">
<div class="inventory-item sword1" data-item-type="weapon sword"></div>
</div>
<div class="inventory-cell">
<div class="inventory-item carrot"></div>
</div>
<div class="inventory-cell">
</div>
<div class="inventory-cell">
</div>
</div>
<div class="inventory-row">
<div class="inventory-cell">
<div class="inventory-item carrot" data-item-type="food"></div>
</div>
<div class="inventory-cell">
<div class="inventory-item meat1" data-item-type="food cookable"></div>
</div>
<div class="inventory-cell">
<div class="inventory-item meat1" data-item-type="food cookable"></div>
</div>
<div class="inventory-cell"></div>
<div class="inventory-cell"></div>
</div>
<div class="inventory-row">
<div class="inventory-cell">
<div class="inventory-item axe1" data-item-type="weapon axe"></div>
</div>
<div class="inventory-cell">
</div>
<div class="inventory-cell">
<div class="inventory-item grape" data-item-type="food"></div>
</div>
<div...
CSS
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body
{
background: #332f2c;
color: #dddddd;
font-family: Arial;
}
/* Item atlas: http://i.imgur.com/ngGK5MF.png
* Table atlas: http://i.imgur.com/nOqKSbE.png
*/
a, a:link, a:visited {
color: #c00;
text-decoration: none;
-webkit-transition: all 0.3s ease;
transition: all 0.3s ease;
}
a:hover {
color: #ff5c19;
text-decoration: underline;
}
a:active {
color: #6aff19;
}
input[type="range"]
{
width: 400px;
}
.inventory-table
{
}
.inventory-row
{
display: flex;
flex-direction: row;
justify-content: flex-start; /* align items in Main Axis */
align-items: flex-start; /* align items in Cross Axis */
align-content: flex-start; /* Extra space in Cross Axis */
}
.inventory-cell
{
width: 36px;
height: 36px;
flex-shrink: 0;
display: flex;
flex-direction: row;
justify-content: center; /* align items in Main Axis */
align-items: center; /* align items in Cross Axis */
align-content: center; /* Extra space in Cross Axis */
background-image: url('http://i.imgur.com/nOqKSbE.png');
background-position: -229px -331px;
}
/* left */
.inventory-cell:first-child
{
width: 38px;
padding-left: 2px;
background-position: -190px -331px;
}
/* right */
.inventory-cell:last-child
{
width: 38px;
padding-right: 2px;
background-position: -266px -331px;
}
/* top ROW */
.inventory-row:first-child .inventory-cell
{
height: 38px;
padding-top: 2px;
background-position: -229px -292px;
}
/* bottom ROW */
.inventory-row:last-child .inventory-cell
{
height: 38px;
padding-bottom: 2px;
background-position: -229px -368px;
}
/* top left */
.inventory-row:first-child .inventory-cell:first-child
{
width: 38px;
height: 38px;
background-position: -190px -292px;
}
/* top right */
.inventory-row:first-child...
JavaScript
"use strict";
jQuery.fn.extend({
addRemoveItems: function(targetCount) {
return this.each(function() {
var $children = $(this).children();
var rowCountDifference = targetCount - $children.length;
//console.log('row count diff: ' + rowCountDifference);
if(rowCountDifference > 0)
{
// Add items
for(var i = 0; i < rowCountDifference; i++)
{
//console.log($rows.first());
$children.last().clone().appendTo(this);
}
}
else if(rowCountDifference < 0)
{
// remove items
$children.slice(rowCountDifference).remove();
}
});
},
// Modified and Updated by MLM
// Origin: Davy8 (http://stackoverflow.com/a/5212193/796832)
parentToAnimate: function(newParent, duration) {
duration = duration || 'slow';
var $element = $(this);
//console.log($element);
if($element.length > 0)
{
newParent = $(newParent); // Allow passing in either a JQuery object or selector
var oldOffset = $element.offset();
$(this).appendTo(newParent);
var newOffset = $element.offset();
var temp = $element.clone().appendTo('body');
temp.css({
'position': 'absolute',
'left': oldOffset.left,
'top': oldOffset.top,
'zIndex': 1000
});
$element.hide();
temp.animate({
'top': newOffset.top,
'left': newOffset.left
}, duration, function() {
$element.show();
temp.remove();
});
//console.log("parentTo Animate done");
}
...