本文主要和大家詳細介紹了QQ面板拖曳效果,探秘慕課網DOM事件,具有一定的參考價值,有興趣的小夥伴們可以參考一下,希望能幫助到大家。
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面板拖曳效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!