js實現(xiàn)九宮格布局效果
本文實例為大家分享了js實現(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{ //每一個小塊 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='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> <div class='box'> <img src='http://www.hdgsjgj.cn/img/bg2.jpg' /> <h4>以為遇見你</h4> <p>世界才會變得美麗</p> </div> </div> </div> </body> <script type='text/javascript'> window.onload=function () { //判斷是否有id function $(id) { return typeof id ===’string’?document.getElementById(id):null; } //改變每個位置的函數(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)2. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)3. jscript與vbscript 操作XML元素屬性的代碼4. jsp 實現(xiàn)的簡易mvc模式示例5. php使用正則驗證密碼字段的復(fù)雜強度原理詳細講解 原創(chuàng)6. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)7. 基于PHP做個圖片防盜鏈8. Jsp servlet驗證碼工具類分享9. XML在語音合成中的應(yīng)用10. JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實例
