js實(shí)現(xiàn)九宮格布局效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)九宮格布局效果的具體代碼,供大家參考,具體內(nèi)容如下
效果



代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#container{
width: 1200px;
margin:0 auto;
}
#top{
padding: 20px;
}
#bottom{
position: relative;
}
.box{ //每一個(gè)小塊
width: 220px;
height: 360px;
margin: 0 15px 15px 0;
background-color:#e8e8e8;
}
.box img{
width: 220px;
height: 300px;
}
.box p{
color: orangered;
}
</style>
</head>
<body>
<div id="container">
<div id="top">
<button id="btn1">3列</button>
<button id="btn2">4列</button>
<button id="btn3">5列</button>
</div>
<div id="bottom">
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
<div class="box">
<img src="../img/bg2.jpg" />
<h4>以為遇見你</h4>
<p>世界才會(huì)變得美麗</p>
</div>
</div>
</div>
</body>
<script type="text/javascript">
window.onload=function () {
//判斷是否有id
function $(id) {
return typeof id ==='string'?document.getElementById(id):null;
}
//改變每個(gè)位置的函數(shù) jiuge
function jiuge(lieshu,pn) {
var boxW=220,boxH=360,boxXY=15;
for(var i=0;i<pn.children.length;i++){
var row =Math.floor(i/lieshu);//行
var col=Math.floor(i%lieshu);//列
console.log("當(dāng)前盒子所在的坐標(biāo):("+row+","+col+")");
var sd=pn.children[i];
sd.style.position='absolute';
sd.style.left=col*(boxW+boxXY)+'px';
sd.style.top=row*(boxH+boxXY)+'px';
}
}
//調(diào)用
$('btn1').addEventListener('click',function () {
jiuge(3,$('bottom'));
});
$('btn2').addEventListener('click',function () {
jiuge(4,$('bottom'));
});
$('btn3').addEventListener('click',function () {
jiuge(5,$('bottom'));
});
}
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript帶回調(diào)函數(shù)的異步腳本載入方法實(shí)例分析
這篇文章主要介紹了javascript帶回調(diào)函數(shù)的異步腳本載入方法,以實(shí)例形式較為詳細(xì)的分析了javascript回調(diào)函數(shù)實(shí)現(xiàn)異步載入的定義與使用技巧,需要的朋友可以參考下2015-07-07
JavaScript中的console.profile()函數(shù)詳細(xì)介紹
這篇文章主要介紹了JavaScript中的console.profile()函數(shù)詳細(xì)介紹,本文講解了console.profile()函數(shù)的瀏覽器支持情況、console.profile()的使用、Firebug中Profile按鈕的使用等內(nèi)容,需要的朋友可以參考下2014-12-12
5個(gè)javascript的數(shù)字格式化函數(shù)分享
Javascript沒有任何內(nèi)建的格式化函數(shù),這里我們通過Google收集了5個(gè)javascript的數(shù)字格式化函數(shù),希望對(duì)于大家的web開發(fā)能夠帶來方便2011-12-12
javascript中的 object 和 function小結(jié)
JavaScript的面向?qū)ο笫腔谠蔚?,所有?duì)象都有一條屬于自己的原型鏈。Object與Function可能很多看Object instanceof Function , Function instanceof Object都為true而迷惑,所以首先看下對(duì)象的實(shí)例。2016-08-08
基于JavaScript實(shí)現(xiàn)百度搜索框效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)百度搜索框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
jquery自定義插件結(jié)合baiduTemplate.js實(shí)現(xiàn)異步刷新(附源碼)
本文主要介紹了jquery自定義插件結(jié)合baiduTemplate.js實(shí)現(xiàn)異步刷新的具體實(shí)例,具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
當(dāng)鼠標(biāo)移出灰色區(qū)域時(shí)候,菜單項(xiàng)怎么隱藏起來
當(dāng)鼠標(biāo)移出灰色區(qū)域時(shí)候,菜單項(xiàng)怎么隱藏起來...2007-11-11
JavaScript 生成隨機(jī)數(shù)并自動(dòng)大小排序
JavaScript按規(guī)定生成隨機(jī)數(shù),并按指定順序自動(dòng)排序,本例中將生成1——100以內(nèi)的隨機(jī)數(shù),并按照由小到大的順序排列起來。2009-12-12

