js实现QQ面板拖拽效果(慕课网DOM事件探秘)(全)
更新时间:2017年09月19日 09:04:32 作者:喵嘻嘻
这篇文章主要为大家详细介绍了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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Javascript DOM事件操作小结(监听鼠标点击、释放,悬停、离开等)
- JavaScript DOM事件(笔记)
- JavaScript call apply使用 JavaScript对象的方法绑定到DOM事件后this指向问题
- JS对象与JSON互转换、New Function()、 forEach()、DOM事件流等js开发基础小结
- 关于javascript DOM事件模型的两件事
- javascript循环变量注册dom事件 之强大的闭包
- 谈谈我对JavaScript DOM事件的理解
- 深入理解JS DOM事件机制
- 理解javascript中DOM事件
- js中DOM事件绑定分析
- JavaScript将DOM事件处理程序封装为event.js 出现的低级错误问题
- js DOM的事件常见操作实例详解
相关文章
uni-app使用uploadFile上传多张图片的具体实现
在微信小程序中不支持多张图片上传,需要做循环实现多张图片上传,下面这篇文章主要给大家介绍了关于uni-app使用uploadFile上传多张图片的具体实现,需要的朋友可以参考下2023-04-04用js实现判断当前网址的来路如果不是指定的来路就跳转到指定页面
用js实现判断当前网址的来路如果不是指定的来路就跳转到指定页面,需要的朋友可以参考下。2011-05-05JavaScript给按钮绑定点击事件(onclick)的方法
这篇文章主要介绍了JavaScript给按钮绑定点击事件(onclick)的方法,涉及javascript绑定onclick的基本技巧,非常具有实用价值,需要的朋友可以参考下2015-04-04
最新评论