这篇文章主要介绍了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。