这个地址是移动div并互相交换的页面 http://yslou.com/case/drop.html
在ie、遨游、360、oper、Chrome等浏览器试过都没问题。
在火狐里面不能移动,请高手帮忙修改一下,谢谢
源码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>元素拖揽</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
.t {
background-color: #EAEAEA;
height: 200px;
width: 200px;
border: 1px solid #CCC;
float:left;
}
.t2 {
background-color: #FFC;
height: 233px;
width: 234px;
border: 1px solid #FC9;
float:left;
margin:19px;
}
.t3{
background-color:#ece;
width:130px;
height:150px;
margin:10px;
border:1px solid #eac;float:left;
}
.t4{
background-color:#efe;
width:150px;
height:180px;
margin:13px;
border:1px solid #e2c;float:left;
}
.opacity{
opacity:0.5;
filter:alpha(opacity=50);
margin:0px;
padding:0px;
cursor:move;
}
.border{
border:1px dashed #FC6;
background:#fff;
}
.black{
background:#44F4F9;
border-color:#CCC
}
</style>
</head><body> <div id="t" class="t">fdsfdsafsafdsf</div>
<div id="t2" class="t2" >ffffffff</div>
<div id="t3" class="t3" >ggggggg</div>
<div id="t4" class="t4" >bbbbbbb</div><script type="text/javascript">
/*
使用规则:1,必须在CSS样式表里添加如下CSS类:
.opacity{
opacity:0.5;
filter:alpha(opacity=50);
margin:0px;
padding:0px;
cursor:move;
}
.border{
border:1px dashed #FC6;
background:#fff;
}
.black{
background:#EEF4F9;
border-color:#CCC
}
**类的属性可自定义
**最好保持类的独立性,不要类似.a.b.c{}这样的。
**在页面加载完毕时调用或将此JS加在页面最底部,调用方式:sang(elems),elems可以是单个ID,如"id1",也可以是一个元素对象,也可以是多个ID的数组,或元素对象的集合。
**如:要控制页面中ID为 id1,id2,id3的元素,即sang(['id1','id2','id3']),如要控制某父元素下的所有子元素,可用:sang(父元素对象.document.getElementsByTagName("元素名"))
*/
var sang=function(elems){
var leng,elms=[];
var ox,oy,x2,y2,ocls,mvid,ismv=false;
if(isArray(elems)){
leng=elems.length;
for(var i=0;i<leng;i++){
elms[i]=document.getElementById(elems[i])
}
}
else if(typeof elems=="string"){
elms[0]=document.getElementById(elems);
leng=1;
}
else{
if(elems.length){
elms=elems;
leng=elems.length;
}
else{
elms[0]=elems;
leng=1;
}
}
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
var w={
x:function(){
return event.clientX+Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
},
y:function(){
return event.clientY+Math.max(document.documentElement.scrollTop,document.body.scrollTop);
}
};
function getpos(obj){
var w={};
w.x=obj.offsetLeft;
w.y=obj.offsetTop;
w.w=obj.offsetWidth;
w.h=obj.offsetHeight;
while(obj=obj.offsetParent){
w.x+=obj.offsetLeft
w.y+=obj.offsetTop
}
return w;
}
function down(obj){
var div=document.createElement("div");
ox=getpos(obj).x;
oy=getpos(obj).y;
x2=w.x();y2=w.y();
ocls=obj.className;
div.className=obj.className+" opacity";
div.innerHTML=obj.innerHTML;
div.id=obj.id+'mvid',
div.style.position="absolute";
div.style.left=ox+2+"px";
div.style.top=oy+2+"px";
obj.className+=" border";
obj.innerHTML="";
document.body.appendChild(div);
mvid=document.getElementById(obj.id+'mvid');
ismv=true;
}
function move(obj){
}
function init(e){
for(var i=0;i<leng;i++){
e[i].onmousedown=function(){
down(this);
var self=this;
document.onmousemove=function(){
mvid.style.left=w.x()-(x2-ox)+"px";
mvid.style.top=w.y()-(y2-oy)+"px";
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
e[i].className+=" black";
}
}
else{
e[i].className=e[i].className.replace(/ black/g,'')
}
}
return false
}//document.onmousemove END
document.onmouseup=function(){
document.onmousemove={};
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
self.innerHTML=e[i].innerHTML;
self.className=e[i].className.replace(/ black/g,"");
self.id=e[i].id;
e[i].className=mvid.className.replace(/ opacity/g,"");
e[i].innerHTML=mvid.innerHTML;
e[i].id=mvid.id.replace(/mvid/g,'');
document.body.removeChild(mvid);
ismv=false;
break;
}
}
}//for END
if(ismv){
self.innerHTML=mvid.innerHTML;
self.className=ocls;
document.body.removeChild(mvid);
ismv=false;
}
}//document.onmouseup END
return false
}//e[i].onmousedown END
}//for END
}//init END
init(elms);
}
sang(document.getElementsByTagName('div'))
</script>
</body>
</html>
在ie、遨游、360、oper、Chrome等浏览器试过都没问题。
在火狐里面不能移动,请高手帮忙修改一下,谢谢
源码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>元素拖揽</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
.t {
background-color: #EAEAEA;
height: 200px;
width: 200px;
border: 1px solid #CCC;
float:left;
}
.t2 {
background-color: #FFC;
height: 233px;
width: 234px;
border: 1px solid #FC9;
float:left;
margin:19px;
}
.t3{
background-color:#ece;
width:130px;
height:150px;
margin:10px;
border:1px solid #eac;float:left;
}
.t4{
background-color:#efe;
width:150px;
height:180px;
margin:13px;
border:1px solid #e2c;float:left;
}
.opacity{
opacity:0.5;
filter:alpha(opacity=50);
margin:0px;
padding:0px;
cursor:move;
}
.border{
border:1px dashed #FC6;
background:#fff;
}
.black{
background:#44F4F9;
border-color:#CCC
}
</style>
</head><body> <div id="t" class="t">fdsfdsafsafdsf</div>
<div id="t2" class="t2" >ffffffff</div>
<div id="t3" class="t3" >ggggggg</div>
<div id="t4" class="t4" >bbbbbbb</div><script type="text/javascript">
/*
使用规则:1,必须在CSS样式表里添加如下CSS类:
.opacity{
opacity:0.5;
filter:alpha(opacity=50);
margin:0px;
padding:0px;
cursor:move;
}
.border{
border:1px dashed #FC6;
background:#fff;
}
.black{
background:#EEF4F9;
border-color:#CCC
}
**类的属性可自定义
**最好保持类的独立性,不要类似.a.b.c{}这样的。
**在页面加载完毕时调用或将此JS加在页面最底部,调用方式:sang(elems),elems可以是单个ID,如"id1",也可以是一个元素对象,也可以是多个ID的数组,或元素对象的集合。
**如:要控制页面中ID为 id1,id2,id3的元素,即sang(['id1','id2','id3']),如要控制某父元素下的所有子元素,可用:sang(父元素对象.document.getElementsByTagName("元素名"))
*/
var sang=function(elems){
var leng,elms=[];
var ox,oy,x2,y2,ocls,mvid,ismv=false;
if(isArray(elems)){
leng=elems.length;
for(var i=0;i<leng;i++){
elms[i]=document.getElementById(elems[i])
}
}
else if(typeof elems=="string"){
elms[0]=document.getElementById(elems);
leng=1;
}
else{
if(elems.length){
elms=elems;
leng=elems.length;
}
else{
elms[0]=elems;
leng=1;
}
}
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
var w={
x:function(){
return event.clientX+Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
},
y:function(){
return event.clientY+Math.max(document.documentElement.scrollTop,document.body.scrollTop);
}
};
function getpos(obj){
var w={};
w.x=obj.offsetLeft;
w.y=obj.offsetTop;
w.w=obj.offsetWidth;
w.h=obj.offsetHeight;
while(obj=obj.offsetParent){
w.x+=obj.offsetLeft
w.y+=obj.offsetTop
}
return w;
}
function down(obj){
var div=document.createElement("div");
ox=getpos(obj).x;
oy=getpos(obj).y;
x2=w.x();y2=w.y();
ocls=obj.className;
div.className=obj.className+" opacity";
div.innerHTML=obj.innerHTML;
div.id=obj.id+'mvid',
div.style.position="absolute";
div.style.left=ox+2+"px";
div.style.top=oy+2+"px";
obj.className+=" border";
obj.innerHTML="";
document.body.appendChild(div);
mvid=document.getElementById(obj.id+'mvid');
ismv=true;
}
function move(obj){
}
function init(e){
for(var i=0;i<leng;i++){
e[i].onmousedown=function(){
down(this);
var self=this;
document.onmousemove=function(){
mvid.style.left=w.x()-(x2-ox)+"px";
mvid.style.top=w.y()-(y2-oy)+"px";
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
e[i].className+=" black";
}
}
else{
e[i].className=e[i].className.replace(/ black/g,'')
}
}
return false
}//document.onmousemove END
document.onmouseup=function(){
document.onmousemove={};
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
self.innerHTML=e[i].innerHTML;
self.className=e[i].className.replace(/ black/g,"");
self.id=e[i].id;
e[i].className=mvid.className.replace(/ opacity/g,"");
e[i].innerHTML=mvid.innerHTML;
e[i].id=mvid.id.replace(/mvid/g,'');
document.body.removeChild(mvid);
ismv=false;
break;
}
}
}//for END
if(ismv){
self.innerHTML=mvid.innerHTML;
self.className=ocls;
document.body.removeChild(mvid);
ismv=false;
}
}//document.onmouseup END
return false
}//e[i].onmousedown END
}//for END
}//init END
init(elms);
}
sang(document.getElementsByTagName('div'))
</script>
</body>
</html>
其中浏览器判断部分的代码
function on_ini(){
String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
var agent=navigator.userAgent
window.isOpr=agent.inc("Opera")
window.isIE=agent.inc("IE") && !isOpr
window.isMoz=agent.inc("Mozilla") && !isOpr && !isIE
if(isMoz){
Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
}
basic_ini()
}
x:function(){
return event.clientX+Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
},
y:function(){
return event.clientY+Math.max(document.documentElement.scrollTop,document.body.scrollTop);
}
};
这段不兼容,firefox//
设置一个参数,var clientX,clientY;
document.onmousemove=function(evt){
evt=evt || window.event;
clientX=evt.clientX;
clientY=evt.clientY;
}
//这样应该可以了,
var clientX,clientY; //增加
var leng,elms=[];
var ox,oy,x2,y2,ocls,mvid,ismv=false;
if(isArray(elems)){
leng=elems.length;
for(var i=0;i<leng;i++){
elms[i]=document.getElementById(elems[i])
}
}
else if(typeof elems=="string"){
elms[0]=document.getElementById(elems);
leng=1;
}
else{
if(elems.length){
elms=elems;
leng=elems.length;
}
else{
elms[0]=elems;
leng=1;
}
}
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
function getEvent(evt)
{
evt=evt || window.event;
clientX=evt.clientX;
clientY=evt.clientY;
}
document.onmousemove=function(evt){
getEvent(evt);
}
var w={
x:function(){
//alert(event.clientX+'' +clientX);
return clientX+Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
},
y:function(e){
return clientY+Math.max(document.documentElement.scrollTop,document.body.scrollTop);
}
};
function getpos(obj){
var w={};
w.x=obj.offsetLeft;
w.y=obj.offsetTop;
w.w=obj.offsetWidth;
w.h=obj.offsetHeight;
while(obj=obj.offsetParent){
w.x+=obj.offsetLeft
w.y+=obj.offsetTop
}
return w;
}
function down(obj){
var div=document.createElement("div");
ox=getpos(obj).x;
oy=getpos(obj).y;
x2=w.x();y2=w.y();
ocls=obj.className;
div.className=obj.className+" opacity";
div.innerHTML=obj.innerHTML;
div.id=obj.id+'mvid',
div.style.position="absolute";
div.style.left=ox+2+"px";
div.style.top=oy+2+"px";
obj.className+=" border";
obj.innerHTML="";
document.body.appendChild(div);
mvid=document.getElementById(obj.id+'mvid');
ismv=true;
}
function move(obj){ }
function init(e){
for(var i=0;i<leng;i++){
e[i].onmousedown=function(){
down(this);
var self=this;
document.onmousemove=function(evt){
getEvent(evt); //增加
mvid.style.left=w.x()-(x2-ox)+"px";
mvid.style.top=w.y()-(y2-oy)+"px";
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
e[i].className+=" black";
}
}
else{
e[i].className=e[i].className.replace(/ black/g,'')
}
}
return false
}//document.onmousemove ENDdocument.onmouseup=function(){
document.onmousemove={};
for(var i=0;i<leng;i++){
var eW=getpos(e[i]);
var ex=eW.x,ey=eW.y,ew=eW.w,eh=eW.h;
if(w.x()>ex && w.y()>ey && w.x()<ex+ew && w.y()<ey+eh){
if(e[i].id!=mvid.id.replace(/mvid/g,"")){
self.innerHTML=e[i].innerHTML;
self.className=e[i].className.replace(/ black/g,"");
self.id=e[i].id;
e[i].className=mvid.className.replace(/ opacity/g,"");
e[i].innerHTML=mvid.innerHTML;
e[i].id=mvid.id.replace(/mvid/g,'');
document.body.removeChild(mvid);
ismv=false;
break;
}
}
}//for END
if(ismv){
self.innerHTML=mvid.innerHTML;
self.className=ocls;
document.body.removeChild(mvid);
ismv=false;
}
}//document.onmouseup END
return false
}//e[i].onmousedown END
}//for END
}//init END
init(elms);
}
sang(document.getElementsByTagName('div'))