js實例教程-基於jquery的不規則矩形的排列實現代碼

发布时间:2018-11-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js實例教程-基於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>

代碼如下:

<!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 &amp;& (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>

覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!

脚本宝典总结

以上是脚本宝典为你收集整理的js實例教程-基於jquery的不規則矩形的排列實現代碼全部内容,希望文章能够帮你解决js實例教程-基於jquery的不規則矩形的排列實現代碼所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。