您好,登錄后才能下訂單哦!
這篇文章主要介紹了js如何實現QQ面板拖拽效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1、能夠嵌入動態文本于HTML頁面。2、對瀏覽器事件做出響應。3、讀寫HTML元素。4、在數據被提交到服務器之前驗證數據。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創建和修改等。7、基于Node.js技術進行服務器端編程。
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; //關閉頁面 var close = document.getElementById("ui_boxyClose"); close.onclick = winClose; //切換狀態 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; } }; //鼠標滑過,背景變色 for (var i = 0; i < lis.length; i++) { lis[i].onmouseover = function () { this.style.backgroundColor = "#888"; }; lis[i].onmouseout = function () { this.style.backgroundColor = "#fff"; }; //鼠標點擊,txt改變,圖標改變 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"); //光標按下時光標和面板之間的距離; 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; //當l=0時,窗口不能繼續外移 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點擊事件冒泡,導致下拉列表無法點開
loginState.onclick = function () { stateList.style.display = "block"; } document.onclick = function () { stateList.style.display = "none"; }
lis[i].onclick列表項的點擊事件冒泡,導致下拉列表無法隱藏
lis[i].onclick = function () { stateList.style.display = "none"; } loginState.onclick = function () { stateList.style.display = "block"; }
2.鼠標事件坐標獲取
function fnDown(event) { var event = event || window.event; var oDrag = document.getElementById("loginPanel"); //光標按下時光標和面板之間的距離; 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; //當l=0時,窗口不能繼續外移 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()方法
方法返回的是一個數組,切記
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; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“js如何實現QQ面板拖拽效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。