首页 > 代码库 > 拖拽 - 原声javascript
拖拽 - 原声javascript
//by sunhw
//获取className
function returnClassName(className, parentNode){
var parentNode = parentNode ? document.getElementById(parentNode) : document;
var classNames = [];
var eles = parentNode.getElementsByTagName(‘*‘);
for(var i = 0, len = eles.length; i < len; i++){
if(eles[i].className == className){
classNames.push(eles[i]);
}
}
return classNames;
}
function returnClassName(className, parentNode){
var parentNode = parentNode ? document.getElementById(parentNode) : document;
var classNames = [];
var eles = parentNode.getElementsByTagName(‘*‘);
for(var i = 0, len = eles.length; i < len; i++){
if(eles[i].className == className){
classNames.push(eles[i]);
}
}
return classNames;
}
window.onload = dragFun;
function dragFun(){
var dragNode = returnClassName(‘login_logo_webqq‘, ‘loginPanel‘)[0];
dragNode.onmousedown = dragBeginFun;
var colseBtn = document.getElementById(‘ui_boxyClose‘);
colseBtn.onclick = function(){
document.getElementById(‘loginPanel‘).style.display = ‘none‘;
}
var dragNode = returnClassName(‘login_logo_webqq‘, ‘loginPanel‘)[0];
dragNode.onmousedown = dragBeginFun;
var colseBtn = document.getElementById(‘ui_boxyClose‘);
colseBtn.onclick = function(){
document.getElementById(‘loginPanel‘).style.display = ‘none‘;
}
//click tab change
var loginStatePanel = document.getElementById(‘loginStatePanel‘),
loginStateTxt = document.getElementById(‘login2qq_state_txt‘),
loginStateShow = document.getElementById(‘loginStateShow‘),
loginState = document.getElementById(‘loginState‘);
var loginStatePanel = document.getElementById(‘loginStatePanel‘),
loginStateTxt = document.getElementById(‘login2qq_state_txt‘),
loginStateShow = document.getElementById(‘loginStateShow‘),
loginState = document.getElementById(‘loginState‘);
//click loginStatePanel show panel need zuzhi moren event
loginState.onclick = function(e){
e = e || window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
loginStatePanel.style.display = ‘block‘;
}
loginState.onclick = function(e){
e = e || window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
loginStatePanel.style.display = ‘block‘;
}
//for each
var list = loginStatePanel.getElementsByTagName(‘li‘);
for(var i = 0, len = list.length; i < len; i++){
var list = loginStatePanel.getElementsByTagName(‘li‘);
for(var i = 0, len = list.length; i < len; i++){
//onmouseover
list[i].onmouseover = function(){
this.style.backgroundColor = ‘#567‘;
}
//onmouseout
list[i].onmouseout = function(){
this.style.backgroundColor = ‘#fff‘;
}
//onclick
list[i].onclick = function(e){
e = e || window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;//IE zuzhi moren event
}
list[i].onmouseover = function(){
this.style.backgroundColor = ‘#567‘;
}
//onmouseout
list[i].onmouseout = function(){
this.style.backgroundColor = ‘#fff‘;
}
//onclick
list[i].onclick = function(e){
e = e || window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;//IE zuzhi moren event
}
loginStatePanel.style.display = ‘none‘;
var id = this.id;
loginStateTxt.innerHTML = returnClassName(‘stateSelect_text‘, id)[0].innerHTML;
loginStateShow.className = ‘‘;
loginStateShow.className = ‘login-state-show ‘ + id;
}
}
var id = this.id;
loginStateTxt.innerHTML = returnClassName(‘stateSelect_text‘, id)[0].innerHTML;
loginStateShow.className = ‘‘;
loginStateShow.className = ‘login-state-show ‘ + id;
}
}
//none loginStatePanel
document.onclick = function(){
loginStatePanel.style.display = ‘none‘;
}
document.onclick = function(){
loginStatePanel.style.display = ‘none‘;
}
}
function dragBeginFun(event){
event = event || window.event;
var dragParentNode = document.getElementById(‘loginPanel‘);
event = event || window.event;
var dragParentNode = document.getElementById(‘loginPanel‘);
//光标当前的位置 = 光标的坐标值 - 盒子距离窗口左边的值
var disX = event.clientX - dragParentNode.offsetLeft;
var disY = event.clientY - dragParentNode.offsetTop;
var disX = event.clientX - dragParentNode.offsetLeft;
var disY = event.clientY - dragParentNode.offsetTop;
//onmousemove event
document.onmousemove = function(event){
event = event || window.event;
dragMoveFun(event, disX, disY);
return false;
}
document.onmousemove = function(event){
event = event || window.event;
dragMoveFun(event, disX, disY);
return false;
}
//onmouseup event
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
function dragMoveFun(e, posX, posY){
var dragParentNode = document.getElementById(‘loginPanel‘);
var dragParentNode = document.getElementById(‘loginPanel‘);
//
var left = e.clientX - posX ;
var top = e.clientY - posY;
var left = e.clientX - posX ;
var top = e.clientY - posY;
//窗口宽度和高度
var windWidth = document.documentElement.clientWidth || document.body.clientWidth;
var windHeight = document.documentElement.clientHeight || document.body.clientHeight;
var windWidth = document.documentElement.clientWidth || document.body.clientWidth;
var windHeight = document.documentElement.clientHeight || document.body.clientHeight;
//可拖拽的最大宽度和高度 窗口宽度 - 盒子的自身宽度
var maxW = windWidth - dragParentNode.offsetWidth;
var maxH = windHeight - dragParentNode.offsetHeight;
var maxW = windWidth - dragParentNode.offsetWidth;
var maxH = windHeight - dragParentNode.offsetHeight;
//判断左右边界
if(left < 0){
left = 0;
}else if(left > maxW){
left = maxW - 10;
}
if(left < 0){
left = 0;
}else if(left > maxW){
left = maxW - 10;
}
//判断上下边界
if(top < 0){
top = 10;
}else if(top > maxH){
top = maxH;
}
if(top < 0){
top = 10;
}else if(top > maxH){
top = maxH;
}
dragParentNode.style.left = left + ‘px‘;
dragParentNode.style.top = top + ‘px‘;
}
dragParentNode.style.top = top + ‘px‘;
}
拖拽 - 原声javascript
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。