您好,登錄后才能下訂單哦!
這篇文章主要介紹了js如何實現(xiàn)QQ面板拖拽效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1、能夠嵌入動態(tài)文本于HTML頁面。2、對瀏覽器事件做出響應(yīng)。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗證數(shù)據(jù)。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。
QQ面板拖拽,效果如圖
JavaScript代碼如下:
function getByClass(clsName, parent) { var oParent = parent ? document.getElementById(parent) : document, eles = [], elements = oParent.getElementsByTagName('*'); for (var i = 0, l = elements.length; i < l; i++) { if (elements[i].className == clsName) { eles.push(elements[i]); } } return eles; } window.onload = drag; function drag() { var oTitle = getByClass("login_logo_webqq", "loginPanel")[0]; //拖頁 oTitle.onmousedown = fnDown; //關(guān)閉頁面 var close = document.getElementById("ui_boxyClose"); close.onclick = winClose; //切換狀態(tài) var loginState = document.getElementById("loginState"); var stateList = document.getElementById("loginStatePanel"); var lis = stateList.getElementsByTagName("li"); var stateTxt = document.getElementById("login2qq_state_txt"); var loginStateShow = document.getElementById("loginStateShow"); //點擊顯示下拉單 loginState.onclick = function (e) { stateList.style.display = "block"; //阻止事件冒泡; e = event || window.event; if(e.stopPropagation){ e.stopPropagation(); }else{ e.cancelBubble = true; } }; //鼠標(biāo)滑過,背景變色 for (var i = 0; i < lis.length; i++) { lis[i].onmouseover = function () { this.style.backgroundColor = "#888"; }; lis[i].onmouseout = function () { this.style.backgroundColor = "#fff"; }; //鼠標(biāo)點擊,txt改變,圖標(biāo)改變 lis[i].onclick = function (e) { stateList.style.display = "none"; //阻止事件冒泡 e = event || window.event; if(typeof e.stopPropagation){ e.stopPropagation(); }else { e.cancelBubble = true; } var id = this.id; loginStateShow.className = "login-state-show "+id; var text = getByClass("stateSelect_text",id)[0].innerHTML; stateTxt.innerHTML = text; } } document.onclick = function () { stateList.style.display = "none"; } } function winClose() { var box = document.getElementById("loginPanel"); box.style.display = "none"; } function fnDown(event) { var event = event || window.event; var oDrag = document.getElementById("loginPanel"); //光標(biāo)按下時光標(biāo)和面板之間的距離; var disX = event.clientX - oDrag.offsetLeft; var disY = event.clientY - oDrag.offsetTop; //移動 document.onmousemove = function (event) { event = event || window.event; fnMove(event, disX, disY); }; document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; } } function fnMove(event, posX, posY) { var oDrag = document.getElementById("loginPanel"); var l = event.clientX - posX; var t = event.clientY - posY; var winW = document.documentElement.clientWidth; var winH = document.documentElement.clientHeight; var maxW = winW - oDrag.offsetWidth - 10; var maxH = winH - oDrag.offsetHeight; //當(dāng)l=0時,窗口不能繼續(xù)外移 if (l < 0) { l = 0; } else if (l > maxW) { l = maxW; } if (t < 10) { t = 10; } else if (t > maxH) { t = maxH; } oDrag.style.left = l + "px"; oDrag.style.top = t + "px"; }
要點:
1.阻止事件冒泡
loginState.onclick點擊事件冒泡,導(dǎo)致下拉列表無法點開
loginState.onclick = function () { stateList.style.display = "block"; } document.onclick = function () { stateList.style.display = "none"; }
lis[i].onclick列表項的點擊事件冒泡,導(dǎo)致下拉列表無法隱藏
lis[i].onclick = function () { stateList.style.display = "none"; } loginState.onclick = function () { stateList.style.display = "block"; }
2.鼠標(biāo)事件坐標(biāo)獲取
function fnDown(event) { var event = event || window.event; var oDrag = document.getElementById("loginPanel"); //光標(biāo)按下時光標(biāo)和面板之間的距離; var disX = event.clientX - oDrag.offsetLeft; var disY = event.clientY - oDrag.offsetTop; //移動 document.onmousemove = function (event) { event = event || window.event; fnMove(event, disX, disY); }; document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; } } function fnMove(event, posX, posY) { var oDrag = document.getElementById("loginPanel"); var l = event.clientX - posX; var t = event.clientY - posY; var winW = document.documentElement.clientWidth; var winH = document.documentElement.clientHeight; var maxW = winW - oDrag.offsetWidth - 10; var maxH = winH - oDrag.offsetHeight; //當(dāng)l=0時,窗口不能繼續(xù)外移 if (l < 0) { l = 0; } else if (l > maxW) { l = maxW; } if (t < 10) { t = 10; } else if (t > maxH) { t = maxH; } oDrag.style.left = l + "px"; oDrag.style.top = t + "px"; }
3.封裝各瀏覽器通用的getElementsByClassName()方法
方法返回的是一個數(shù)組,切記
function getByClass(clsName, parent) { var oParent = parent ? document.getElementById(parent) : document, eles = [], elements = oParent.getElementsByTagName('*'); for (var i = 0, l = elements.length; i < l; i++) { if (elements[i].className == clsName) { eles.push(elements[i]); } } return eles; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“js如何實現(xiàn)QQ面板拖拽效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。