国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

基于原生js實現九宮格算法代碼實例

瀏覽:91日期:2024-05-01 15:14:20

九宮格算法核心:

利用控件索引index計算出控件所在的行數和列數; 利用控件計算出left距離; 利用控件計算出top距離; 寫特效時需要用到定位

公式:

行 row=parseInt(i/cols);

列 col=parseInt(i%cols);

i是當前的盒子,cols是總列數,

代碼示例:

<!DOCTYPE html><html lang='en'> <head> <meta charset='UTF-8'> <title>九宮格</title> <style> *{padding: 0;margin: 0; } #top{margin-top:30px;margin-bottom: 20px;margin-left:20px; } #bottom{position: relative; } #bottom .content{width: 220px;height: 360px;background-color: skyblue;margin: 0 0 15px 15px;padding: 5px; } .content img{width: 220px;height: 308px; } #bottom .content p:last-child{font-size: 15px;color: red; } </style> </head> <body> <div id='top'> <button>排成三列</button> <button>排成四列</button> <button>排成五列</button> </div> <div id='bottom'> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> <div class='content'> <img src='http://www.cgvv.com.cn/bcjs/images/dianying.jpg'> <p>是一部非常成功的導演處女作</p> <p>幾乎全面啟用新演員的做法</p> </div> </div> <script> window.onload=function(){var top=document.getElementById('top');var btns=top.getElementsByTagName('button');var content=document.getElementById('bottom');// console.log(content.children);//console.log(btns);//定義變量標識盒子的寬度和高度 var cssW=220; var cssH=360; var marginXY=15; //監聽按鈕點擊事件btns[0].onclick=function(){ getContent(3);}btns[1].onclick=function(){ getContent(4)}btns[2].onclick=function(){ getContent(5);}function getContent(cols){ var cols; //遍歷 for(var i=0;i<content.children.length;i++){ var currentCont=content.children[i]; //console.log(currentCont); //盒子所在的行 var row=parseInt(i/cols); //盒子所在的列 var col=parseInt(i%cols); //console.log('盒子在第' +row+ '行,''在第' +col+ '列'); currentCont.style.position='absolute'; currentCont.style.left=col*(cssW+marginXY)+'px'; currentCont.style.top=row*(cssH+marginXY)+'px'; }} } </script> </body></html>

九宮格(用原生js實現)

1、本文的九宮格是用原生的js實現的;

2、實現的九宮格效果是:可交換1-9的任意方格,且將方格拖拽至大盒子外松開后可自動回到拖拽之前的位置。

3、代碼如下:

html代碼:

<ul id='box'> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li></ul>

css代碼:

body,div,p,h1,h2,h3,h4,h5,h6,ol,ul,li,dl,dt,dd,th,tr,td,hr,caption,table,form,img,input,legend,fieldset{ margin:0; padding:0;}html { overflow: hidden;}ul { list-style: none;}#box { position: relative; margin: 20px auto; width: 640px; height: 640px; border: 1px solid #eee;}#box li { position: absolute; width: 200px; height: 200px; line-height: 200px; text-align: center; font-size: 40px; font-weight: bold; background: #eee;}#box .active { z-index: 1; color: #fff; background: blue;}

js代碼:

window.onload = function () { var oBox = document.getElementById(’box’); var aLi = oBox.children; for(var i = 0; i < aLi.length; i++) { // 布局 aLi[i].style.left = 210 * (i % 3) + 10 + ’px’; aLi[i].style.top = 210 * Math.floor(i / 3) + 10 + ’px’; // 添加拖拽功能 aLi[i].index = i; aLi[i].onmousedown = function (ev) { var e = ev || window.event; var iX = e.clientX - this.offsetLeft; var iY = e.clientY - this.offsetTop; if(this.setCapture) { this.setCapture(); } var oThat = this; // 添加class名稱 this.className = ’active’; document.onmousemove = function (ev) {var e = ev || window.event;var iL = e.clientX - iX;var iT = e.clientY - iY; oThat.style.left = iL + ’px’;oThat.style.top = iT + ’px’; // 交換位置的條件for(var j = 0; j < aLi.length; j++) { if(oThat != aLi[j] && oThat.offsetLeft + oThat.offsetWidth > aLi[j].offsetLeft + aLi[j].offsetWidth / 2 && oThat.offsetTop + oThat.offsetHeight > aLi[j].offsetTop + aLi[j].offsetHeight / 2 && oThat.offsetLeft < aLi[j].offsetLeft + aLi[j].offsetWidth / 2 && oThat.offsetTop < aLi[j].offsetTop + aLi[j].offsetHeight / 2) { var iCurIndex = oThat.index; // 交換位置 aLi[j].style.left = 210 * (iCurIndex % 3) + 10 + ’px’; aLi[j].style.top = 210 * Math.floor(iCurIndex / 3) + 10 + ’px’; // 交換下標 oThat.index = aLi[j].index; aLi[j].index = iCurIndex; break; }} }; document.onmouseup = function () {document.onmousemove = null;document.onmouseup = null; if(oThat.releaseCapture) { oThat.releaseCapture();} // 去掉class名稱oThat.className = ’’; // 重置當前拖拽元素的位置oThat.style.left = 210 * (oThat.index % 3) + 10 + ’px’;oThat.style.top = 210 * Math.floor(oThat.index / 3) + 10 + ’px’; }; return false; }; }};

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 18在线网站 | 米奇精品一区二区三区在线观看 | 欧美亚洲综合另类在线观看 | 精品欧美小视频在线观看 | 毛片在线视频观看 | 免费观看欧美一级牲片一 | 99久久精品免费观看国产 | 久久久久久国产精品视频 | 在线观看黄网视频免费播放 | 伊人色在线观看 | 手机成人看片 | 国产三级a三级三级 | 精品视频免费在线 | 久草免费资源视频 | 精品三级国产 | 国产大片线上免费看 | 91色老99久久九九爱精品 | 国产精品二区三区免费播放心 | 欧美做爰野外在线视频观看 | 国产三级在线视频观看 | 成年人在线视频网站 | 欧美做a欧美 | 亚洲精品一区二区三区不卡 | 美女脱了内裤张开腿让男人桶网站 | 久久怡红院亚欧成人影院 | 久久免费看 | 一级aaa毛片 | 欧美一级看片免费观看视频在线 | 欧美乱大交xxxxx | 亚洲视频网站在线观看 | 国产成人一级片 | 在线男人的天堂 | 欧美成人高清手机在线视频 | 欧美成人午夜在线全部免费 | 91热久久免费频精品黑人99 | 成人国产网站 | 免费观看一级成人毛片 | 精品国产91久久久久久久 | 亚洲视色 | 成年男人午夜片免费观看 | 日本三级香港三级妇三 |