DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 基於jquery的不規則矩形的排列實現代碼
基於jquery的不規則矩形的排列實現代碼
編輯:JQuery特效代碼     
這個東西讓我想起了俄羅斯方塊,這個實現起來很簡單,容器裡面所有的塊元素用絕對定位排列,如果能放的下就放在這裡,如果放不下了,在隊列中找到能放得下的元素放置,
實在找不到,則換行排列下一行,具體思路是這樣。代碼裡有詳細的注釋直接看代碼吧。
下面是一個demo:
http://demo.jb51.net/js/2012/sortRect/
代碼打包下載 sortRect.rar
 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>不規則寬高排列</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js">
</script>
<script type="text/javascript" src="http://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個方塊 -->
<div id="pannel" style=" position:relative; width:1500px; height:800px; border:1px solid red; overflow:hidden;">
<div class="box">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box3">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box2">
</div>
<div class="box">
</div>
<div class="box4">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
<div id="con">
</div>
<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("div"), 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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved