基於jquery的不規則矩形的排列實現代碼

代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>不規則寬高排列</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.2.js">
</script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.8.18/jquery-ui.min.js">
</script>
<style type="text/css">
.box {
position: absolute;
width: 100px;
height: 100px;
border: 1px solid #ffffff;
display: none;
background: url(img/1.jpg);
/*margin-left: 4px;
margin-top: 4px;*/
}
.box2 {
position: absolute;
width: 100px;
height: 202px;
border: 1px solid #ffffff;
display: none;
background: url(img/2.jpg);
/*margin-left: 4px;
margin-top: 4px;*/
}
.box3 {
position: absolute;
width: 202px;
height: 100px;
border: 1px solid #ffffff;
display: none;
background: url(img/3.jpg);
/*margin-left: 4px;
margin-top: 4px;*/
}
.box4 {
position: absolute;
width: 202px;
height: 202px;
border: 1px solid red;
background: url(img/4.jpg);
display: none;
}
</style>
</head>
<body>
<!– 排列好下面20個方塊 –>
<p id="pannel" style=" position:relative; width:1500px; height:800px; border:1px solid red; overflow:hidden;">
<p class="box">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box">
</p>
<p class="box">
</p>
<p class="box3">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box2">
</p>
<p class="box">
</p>
<p class="box4">
</p>
<p class="box">
</p>
<p class="box">
</p>
</p>
<p id="con">
</p>
<script type="text/javascript">
//初始化矩陣
var initMatrix = function(x, y){
if (!x || !y) {
return;
}
x = ~ ~ x;
y = ~ ~ y;
var mt = [];
var i = 0;
var getX = function(xl){
var i = 0;
var matrixX = [];
for (; i < xl; i++) {
matrixX[i] = 0;
}
return {
mt: matrixX,
isComplete: false,
spaces: [{
index: 0,
len: matrixX.length
}]
};
}
for (; i < y; i++) {
mt[i] = getX(x);
}
return mt;
}
//生成元素相應的隊列
var getQuene = function(eleColl, base){
if (!eleColl) {
return;
}
var quene = [];
var i = 0;
var len = eleColl.length;
var getEleMatrix = function(ele, base){
var ht = ele.outerHeight() / base.height;
var wt = ele.outerWidth() / base.width;
return {
ele: ele,
ht: ht,
wt: wt
}
}
for (; i < len; i++) {
quene[i] = getEleMatrix($(eleColl[i]), base);
}
return quene;
}
//以行為單位掃描矩陣
var sortEveryEle = function(pannelMatrix, sortQuene, unitEle, callback){
if (!pannelMatrix || !sortQuene) {
return;
}
unitEle = unitEle ||
{
width: 0,
height: 0
};
var checkSpace = function(line){
var i = 0;
var len = line.mt.length;
var tmpWt = 0;
var tmpidx = 0;
var tmpQuene = [];
var isSetIdx = false;
for (; i < len; i++) {
if (line.mt[i] == 0) {
if (!isSetIdx) {
tmpidx = i;
isSetIdx = true;
}
tmpWt++;
}
if ((line.mt[i] == 1) || (i == len – 1)) {
//保存space信息到裡面隊列
if (tmpWt > 0) {
tmpQuene.push({
index: tmpidx,
len: tmpWt
});
}
//清空臨時信息
tmpidx = 0;
tmpWt = 0;
}
}
if (tmpQuene.length <= 0) {
line.isComplete = true;
}
line.spaces = tmpQuene;
return;
}
var updateMartix = function(curLine, mt, ele, spidx, lineNum){
var i = j = 0;
//獲取當前空白信息
var sp = curLine.spaces[spidx];
//如果占用多行則更新所有占用行的空間
if (ele.ht > 1) {
j = 0;
for (; j < ele.ht; j++) {
i = 0;
for (; i < ele.wt; i++) {
mt[lineNum + j].mt[sp.index + i] = 1;
}
//更新空白空間
checkSpace(mt[lineNum + j]);
}
}
else {//如果是單行的話隻要更新第一個本行
for (; i < ele.wt; i++) {
curLine.mt[sp.index + i] = 1;
}
//更新模塊
checkSpace(curLine);
}
};
//獲取合適的元素
var getRightEle = function(stQu, wd){
var i = 0;
var len = stQu.length;
for (; i < len; i++) {
if (stQu[i].wt <= wd) {
return stQu.splice(i, 1);
}
}
return;
}
//掃描單行
var scanLine = function(oneLine, sortQuene, mt, lineNum){
var i = 0;
var len = oneLine.spaces.length;
//填充
var theWt = oneLine.spaces[i].len;
var mtLeft = mtTop = 0;
//判斷能容納的寬是多少
var rtEle = getRightEle(sortQuene, theWt);
if (rtEle) {
//放置元素
//rtEle[0].ele.css("left", oneLine.spaces[i].index * 102);//base width
//rtEle[0].ele.css("top", lineNum * 102);//base height;
mtLeft = oneLine.spaces[i].index * (unitEle.width || 0);
mtTop = lineNum * (unitEle.height || 0);
if (callback) {
callback({
left: mtLeft,
top: mtTop,
rect: rtEle[0]
});
}
//更新矩陣
updateMartix(oneLine, mt, rtEle[0], i, lineNum);
//返回位置隊列
return {
left: mtLeft,
top: mtTop,
rect: rtEle[0]
}
}
}
var i = j = 0;
var pmLen = pannelMatrix.length;
var completeLine = 0;
var thePosQuene = [], pos;
for (; i < pmLen; i++) {
while (!pannelMatrix[i].isComplete && (sortQuene.length > 0)) {
pos = scanLine(pannelMatrix[i], sortQuene, pannelMatrix, i);
if (pos) {
thePosQuene.push(pos);
}
}
}
return thePosQuene;
}
var con = $("#con");
//生成相關的二維數組
var baseWidth = 102;
var baseHeight = 102;
var base = {
width: baseWidth,
height: baseHeight
}
var pannel = $("#pannel");
var thePannelSize = {
width: pannel.width(),
height: pannel.height()
};
var pannelMatrix = initMatrix(thePannelSize.width / baseWidth, thePannelSize.height / baseHeight);
//得到要排序的不規則寬高的方塊隊列
var sortQuene = getQuene(pannel.find("p"), base);
//遍歷matrix
var theQu = sortEveryEle(pannelMatrix, sortQuene, base);
var theQuOne = theQu.shift();
var selfCall = function(){
if (!theQuOne) {
return;
}
var my = arguments.callee;
theQuOne.rect.ele.show().animate({
left: "+" + theQuOne.left,
top: "+" + theQuOne.top
}, {
duration: 1000,
easing: "easeOutBounce",
complete: function(){
theQuOne = theQu.shift();
my.call();
}
});
}
selfCall();

</script>
</body>
</html>

發佈留言