js實(shí)現(xiàn)照片墻功能實(shí)例
來(lái)源:易賢網(wǎng) 閱讀:1782 次 日期:2015-02-06 14:49:18
溫馨提示:易賢網(wǎng)小編為您整理了“js實(shí)現(xiàn)照片墻功能實(shí)例”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了js實(shí)現(xiàn)照片墻功能的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:<!doctype html>

<html xmlns="" xmlns:html5="">

<head>

<meta charset="utf-8">

<title>js照片墻</title>

<meta http-equiv="X-UA-Compatible" content="edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="Keywords" content="js照片墻">

<meta name="description" content="js照片墻">

<link href="css/style.css" rel="stylesheet">

<script src="move.js"></script>

<style>

*{padding: 0; margin: 0;}

li{list-style: none;}

ul{width: 660px; height:510px; background:#ccc;margin: 20px auto;}

li{width:200px; height: 150px; margin: 10px; float: left; }

</style>

<script>

/*

var arr=['a','b','c','d','e','d','f'];

var pos = arr.lastIndexOf('d');

alert(pos);

*/

window.onload=function(){

var aLi = document.querySelectorAll('li');

var oInput = document.querySelector('#btn');

var pos = [];

var len= aLi.length;

var izIndex= 2;

//布局轉(zhuǎn)換

for(var i=0;i<len;i++){

pos.push([aLi[i].offsetLeft,aLi[i].offsetTop]);

}

for(var i=0;i<len;i++){

aLi[i].style.left=pos[i][0]+'px';

aLi[i].style.top=pos[i][1]+'px';

aLi[i].style.position = 'absolute';

aLi[i].style.margin = '0px';

}

for(var i=0;i<len;i++){

aLi[i].index = i;

setDrag(aLi[i]);

}

oInput.onclick=function(){

var randomArr = [0,1,2,3,4,5,6,7,8];

randomArr.sort(function(num1,num2){

return Math.random()-0.5;

})

for(var i=0;i<len;i++){

//增加隨機(jī)位置的情況

startMove(aLi[i],{left:pos[randomArr[i]][0],top:pos[randomArr[i]][1]});

//修正索引

aLi[i].index = randomArr[i];

}

}

//拖拽

function setDrag(obj){

obj.onmousedown =function(ev){

izIndex++;

obj.style.zIndex= izIndex;

var ev = ev || event;

var disX = ev.clientX - obj.offsetLeft;

var disY = ev.clientY - obj.offsetTop;

document.onmousemove=function(ev){

var ev = ev || event;

obj.style.left = ev.clientX- disX +'px';

obj.style.top = ev.clientY - disY +'px';

for(var i=0; i<len; i++){

aLi[i].style.border='none';

}

var nL = nearLi(obj);

if(nL){

nL.style.border='2px solid red';

}

}

document.onmouseup= function(){

document.onmousemove = null;

document.onmouseup = null;

var nL = nearLi(obj);

if(nL){

nL.style.border='2px solid red';

startMove(obj,{left:pos[nL.index][0],top:pos[nL.index][1]});

startMove(nL,{left:pos[obj.index][0],top:pos[obj.index][1]});

var tmp =nL.index;

nL.index = obj.index;

obj.index = tmp;

nL.style.border='';

}else{

startMove(obj,{left:pos[obj.index][0],top:pos[obj.index][1]});

}

}

return false;

}

}

//檢測(cè)是否有碰撞

function isDump(obj1,obj2){

var l1= obj1.offsetLeft;

var r1= l1+obj1.offsetWidth;

var t1= obj1.offsetTop;

var b1 =obj1.offsetHeight +t1;

var l2= obj2.offsetLeft;

var r2= l2+obj2.offsetWidth;

var t2= obj2.offsetTop;

var b2 =obj2.offsetHeight +t2;

if(b2<t1 || l2>r1 || r2<l1 || t2>b1){

return false;

}else{

return true;

}

}

//尋找最近的節(jié)點(diǎn)

function nearLi(obj){

var index= -1;

var value =9999;

for(var i=0; i<len; i++){

if(isDump(obj,aLi[i]) && obj !=aLi[i]){

var c = getDis(obj,aLi[i]);

if(c <value){

value = c;

index = i;

}

}

}

if(index !=-1){

return aLi[index];

}else{

return false;

}

}

function getDis(obj1,obj2){

var x = obj1.offsetLeft - obj2.offsetLeft;

var y = obj1.offsetTop - obj2.offsetTop;

return Math.sqrt(Math.pow(x,2)+Math.pow(y,2));

}

}

</script>

</head>

<body>

<button id="btn">click</button>

<ul>

<li><img src="1l.jpg" width="200px" height="150px"></li>

<li><img src="2l.jpg" width="200px" height="150px"></li>

<li><img src="3l.jpg" width="200px" height="150px"></li>

<li><img src="4l.jpg" width="200px" height="150px"></li>

<li><img src="5l.jpg" width="200px" height="150px"></li>

<li><img src="6l.jpg" width="200px" height="150px"></li>

<li><img src="1l.jpg" width="200px" height="150px"></li>

<li><img src="2l.jpg" width="200px" height="150px"></li>

<li><img src="4l.jpg" width="200px" height="150px"></li>

</ul>

</body>

</html>

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:js實(shí)現(xiàn)照片墻功能實(shí)例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)