层拖拽效果的一点问题,高分求,大家看看啊!!

时光瞄 2009-04-24 09:07:13
var Drag = {

obj : null,

init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
{
o.onmousedown = Drag.start;

o.hmode = bSwapHorzRef ? false : true ;
o.vmode = bSwapVertRef ? false : true ;

o.root = oRoot && oRoot != null ? oRoot : o ;

if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left = "0px";
if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = "0px";
if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

o.minX = typeof minX != 'undefined' ? minX : null;
o.minY = typeof minY != 'undefined' ? minY : null;
o.maxX = typeof maxX != 'undefined' ? maxX : null;
o.maxY = typeof maxY != 'undefined' ? maxY : null;

o.xMapper = fXMapper ? fXMapper : null;
o.yMapper = fYMapper ? fYMapper : null;

o.root.onDragStart = new Function();
o.root.onDragEnd = new Function();
o.root.onDrag = new Function();
},

start : function(e)
{
var o = Drag.obj = this;
e = Drag.fixE(e);
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
o.root.onDragStart(x, y);

o.lastMouseX = e.clientX;
o.lastMouseY = e.clientY;

if (o.hmode) {
if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
} else {
if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
}

if (o.vmode) {
if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
} else {
if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
}

document.onmousemove = Drag.drag;
document.onmouseup = Drag.end;

//return false;
},

drag : function(e)
{
e = Drag.fixE(e);
var o = Drag.obj;

var ey = e.clientY;
var ex = e.clientX;
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
var nx, ny;

if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

if (o.xMapper) nx = o.xMapper(y)
else if (o.yMapper) ny = o.yMapper(x)

Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
Drag.obj.lastMouseX = ex;
Drag.obj.lastMouseY = ey;

Drag.obj.root.onDrag(nx, ny);
return false;
},

end : function()
{
document.onmousemove = null;
document.onmouseup = null;
Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
Drag.obj = null;
},

fixE : function(e)
{
if (typeof e == 'undefined') e = window.event;
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
return e;
}
}

dom_drag.js
这个是项目中的一个js,主要用到Drag.init(divObj)方法,使层具有拖动的能力
现在要求传入的层(里面还有嵌套层的)使层标题部分拖动,正文部分能全选,输入框支持tab键,不拖动
就像QZone里面,某某好友上传照片,然后点击图片的放大层,标题可以拖动,下面图片可以选中的
这段js我看不懂,层的拖拽原理是什么,我也不清楚
现在的问题是上面这段js只能实现整个层的拖动,无法全选。。。
大家帮忙,最好在原有js上做修改,实现功能,分数不够我在加,谢谢!!
现在的效果是整个
...全文
384 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenzhenNBA 2009-04-25
  • 打赏
  • 举报
回复
没弄过,关注中。。。怎么个对层引用啊?给个例子
luojihaidao 2009-04-25
  • 打赏
  • 举报
回复
9494 还要设置position是绝对。
xiaojing7 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yihaijian1 的回复:]
但是还是整个层作为一个整体来拖动的 啊
[/Quote]

要想什么拖动,直接传入ID就可以了。例如你的文章,直接传送文章所在的ID就可以使文章可以拖动啊
时光瞄 2009-04-25
  • 打赏
  • 举报
回复
但是还是整个层作为一个整体来拖动的 啊
时光瞄 2009-04-25
  • 打赏
  • 举报
回复
hank31713@163.com
dadi5566 2009-04-25
  • 打赏
  • 举报
回复
我有个示例要的话可以给你
daimi5566@126.com
时光瞄 2009-04-25
  • 打赏
  • 举报
回复
其实我想要的就是CSDN的效果,比如你想回帖,然后点击那个登陆,弹出窗口,标题部分用于拖动整个层,
然后下面的输入框可以输入,内容都可以全选,不用于拖动。。。就这样
xiaojing7 2009-04-24
  • 打赏
  • 举报
回复
DragClass.js

 
/*
Auth: jingliangliang
Date: 2008-5-5
Email: KuJing.Work@Gmail.com
*/
(function(){
if(typeof Drag != "undefined")
{
var _Drag = Drag;
}
//此处声明Drag类
//--elementid:要移动元素的ID
var Drag = window.Drag = function(elementid){
var thisDrag = this;
this.DifWidth = 0;
this.DifHeight = 0;
this.thisDivDrag = document.getElementById(elementid);
this.thisDivDrag.onmousedown = function(event){
var theevent;
var theSrcevent;
if(window.event)
{
theevent = window.event;
theSrcevent = window.event.srcElement;
}
else
{
theevent =event;
theSrcevent =event.target;
}
thisDrag.DifWidth= theevent.clientX - theSrcevent.offsetLeft;
thisDrag.DifHeight = theevent.clientY - theSrcevent.offsetTop;
document.body.onmousemove =function(event){
var theevent;
if(window.event)
{
theevent = window.event;
}
else
{
theevent =event;
}
thisDrag.thisDivDrag.style.left = theevent.clientX -thisDrag.DifWidth ;
thisDrag.thisDivDrag.style.top = theevent.clientY -thisDrag.DifHeight ;
};
document.body.onmouseup =function(event)
{
document.body.onmousemove = "";
};
};
};
})();
xiaojing7 2009-04-24
  • 打赏
  • 举报
回复
DragClass.js

[/code=JScript]
/*
Auth: jingliangliang
Date: 2008-5-5
Email: KuJing.Work@Gmail.com
*/
(function(){
if(typeof Drag != "undefined")
{
var _Drag = Drag;
}
//此处声明Drag类
//--elementid:要移动元素的ID
var Drag = window.Drag = function(elementid){
var thisDrag = this;
this.DifWidth = 0;
this.DifHeight = 0;
this.thisDivDrag = document.getElementById(elementid);
this.thisDivDrag.onmousedown = function(event){
var theevent;
var theSrcevent;
if(window.event)
{
theevent = window.event;
theSrcevent = window.event.srcElement;
}
else
{
theevent =event;
theSrcevent =event.target;
}
thisDrag.DifWidth= theevent.clientX - theSrcevent.offsetLeft;
thisDrag.DifHeight = theevent.clientY - theSrcevent.offsetTop;
document.body.onmousemove =function(event){
var theevent;
if(window.event)
{
theevent = window.event;
}
else
{
theevent =event;
}
thisDrag.thisDivDrag.style.left = theevent.clientX -thisDrag.DifWidth ;
thisDrag.thisDivDrag.style.top = theevent.clientY -thisDrag.DifHeight ;
};
document.body.onmouseup =function(event)
{
document.body.onmousemove = "";
};
};
};
})();
[/code]

--调用

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script type="text/javascript" src="DragClass.js"></script>
<script type="text/javascript">
window.onload=function(){
__Drag = new Drag("div1");
};
</script>
</HEAD>

<BODY>
<div id="div1" style="background-color:red;width:100px;height:100px;border:1px solid #666666;position: absolute;">test</div>
</BODY>
</HTML>

87,914

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧