这个功能怎么实现请各位高手帮忙 功能需求是这样的:有个矩形框(div)里面有个图片(地图图片img)拖拽IMG移动 不出这个矩形框DIV 就像web地图那样 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html> <head> <style type="text/css"> #box { position:absolute; top:100px; left:100px; width:400px; height:400px; border-style:solid; border-width:2px; border-color:red; } #box div { position:absolute; background-color:#a8b8c8; } </style> <script type="text/javascript"> function $(id) { return document.getElementById(id); } function initialize() { $("e1").style.left="0"; $("e1").style.top="0"; $("e1").style.width="50px"; $("e1").style.height="50px"; } function areaDrag(box,element) { document.onmousemove=startDrag; document.onmouseup=stopDrag; var deltaX=event.clientX-(parseInt(box.currentStyle.left)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.left)); var deltaY=event.clientY-(parseInt(box.currentStyle.top)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.top)); function startDrag() { if(event.clientX-deltaX>=parseInt(box.currentStyle.left) && event.clientX-deltaX<=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2+parseInt(element.style.width)) { element.style.left=event.clientX-parseInt(box.currentStyle.left)-deltaX; } else { if(event.clientX-deltaX<parseInt(box.currentStyle.left)) { element.style.left="0"; } else { element.style.left=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.width); } } if(event.clientY-deltaY>=parseInt(box.currentStyle.top) && event.clientY-deltaY<=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2+parseInt(element.style.height)) { element.style.top=event.clientY-parseInt(box.currentStyle.top)-deltaY; } else { if(event.clientY-deltaY<parseInt(box.currentStyle.top)) { element.style.top="0"; } else { element.style.top=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.height); } } } function stopDrag() { document.onmousemove=""; } } </script> </head> <body onload="initialize()"> <div id="box"> <div onmousedown="areaDrag($('box'),this)" id="e1"></div> </div> </body></html> 这位大哥 现谢谢你了!!不过这个不是我想要的 你实现了 拖拽 不出 div 我的是 里面是个全图片 但是 div中显示的是这个大图片的局部 然后 拖拽移动来看其它部分 <html> <head> <style type="text/css"> #box { filter:progid:DXImageTransform.Microsoft.BasicImage(Mask=1,MaskColor=0x00A8B8C8); position:absolute; top:100px; left:100px; width:550px; height:380px; z-index:100; border-style:solid; border-width:0; border-color:#244915; } #box div { position:absolute; background-color:#a8b8c8; border-style:solid; border-width:1px; z-index:101; } img { position:absolute; top:100px; left:100px; width:550px; height:380px; z-index:10; } </style> <script type="text/javascript"> function $(id) { return document.getElementById(id); } function initialize() { $("e1").style.left="0"; $("e1").style.top="0"; $("e1").style.width="100px"; $("e1").style.height="100px"; $("e1").onselectstart=function(){return false;}; } function areaDrag(box,element) { document.onmousemove=startDrag; document.onmouseup=stopDrag; var deltaX=event.clientX-(parseInt(box.currentStyle.left)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.left)); var deltaY=event.clientY-(parseInt(box.currentStyle.top)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.top)); function startDrag() { if(event.clientX-deltaX>=parseInt(box.currentStyle.left) && event.clientX-deltaX<=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2) { element.style.left=event.clientX-parseInt(box.currentStyle.left)-deltaX; } else { if(event.clientX-deltaX<parseInt(box.currentStyle.left)) { element.style.left="0"; } else { element.style.left=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.width); } } if(event.clientY-deltaY>=parseInt(box.currentStyle.top) && event.clientY-deltaY<=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2) { element.style.top=event.clientY-parseInt(box.currentStyle.top)-deltaY; } else { if(event.clientY-deltaY<parseInt(box.currentStyle.top)) { element.style.top="0"; } else { element.style.top=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.height); } } } function stopDrag() { document.onmousemove=""; } } </script> </head> <body onload="initialize()"> <div id="box"> <div onmousedown="areaDrag($('box'),this)" id="e1"></div> </div> <img src="http://www.cnblogs.com/images/cnblogs_com/random/world.jpg" /> </body></html> js如何拿flash版本号? 用javascript在网页上显示时间的问题 如何做到自适应 [急]求正则表达式? 高手请帮忙! 请问如何判断客户端某个文件或文件夹是否存在(在线给高分) 框架内,一个页面如何调用另一个页面的js函数 file上传文件的事件 这个应该很难了吧,关于动态调整iframe高度 这个表达式怎么理解? 已知程序文件的完整路径,怎么用 javascript打开它呢?拜托了………… JS怎么调用SQL里面的数据信息? 高分求助!!!!!!!!!!!!!!!!!!!!!!
<html>
<head>
<style type="text/css">
#box
{
position:absolute;
top:100px;
left:100px;
width:400px;
height:400px;
border-style:solid;
border-width:2px;
border-color:red;
}
#box div
{
position:absolute;
background-color:#a8b8c8;
}
</style> <script type="text/javascript">
function $(id)
{
return document.getElementById(id);
} function initialize()
{
$("e1").style.left="0";
$("e1").style.top="0";
$("e1").style.width="50px";
$("e1").style.height="50px";
} function areaDrag(box,element)
{ document.onmousemove=startDrag;
document.onmouseup=stopDrag;
var deltaX=event.clientX-(parseInt(box.currentStyle.left)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.left));
var deltaY=event.clientY-(parseInt(box.currentStyle.top)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.top));
function startDrag()
{
if(event.clientX-deltaX>=parseInt(box.currentStyle.left) && event.clientX-deltaX<=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2+parseInt(element.style.width))
{
element.style.left=event.clientX-parseInt(box.currentStyle.left)-deltaX;
}
else
{
if(event.clientX-deltaX<parseInt(box.currentStyle.left))
{
element.style.left="0";
}
else
{
element.style.left=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.width);
}
}
if(event.clientY-deltaY>=parseInt(box.currentStyle.top) && event.clientY-deltaY<=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2+parseInt(element.style.height))
{
element.style.top=event.clientY-parseInt(box.currentStyle.top)-deltaY;
}
else
{
if(event.clientY-deltaY<parseInt(box.currentStyle.top))
{
element.style.top="0";
}
else
{
element.style.top=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.height);
}
}
} function stopDrag()
{
document.onmousemove="";
}
}
</script>
</head>
<body onload="initialize()">
<div id="box">
<div onmousedown="areaDrag($('box'),this)" id="e1"></div>
</div>
</body>
</html>
不过这个不是我想要的 你实现了 拖拽 不出 div
我的是 里面是个全图片 但是 div中显示的是这个大图片的局部
然后 拖拽移动来看其它部分
<html>
<head>
<style type="text/css">
#box
{
filter:progid:DXImageTransform.Microsoft.BasicImage(Mask=1,MaskColor=0x00A8B8C8);
position:absolute;
top:100px;
left:100px;
width:550px;
height:380px;
z-index:100;
border-style:solid;
border-width:0;
border-color:#244915;
}
#box div
{
position:absolute;
background-color:#a8b8c8;
border-style:solid;
border-width:1px;
z-index:101;
}
img
{
position:absolute;
top:100px;
left:100px;
width:550px;
height:380px;
z-index:10;
}
</style> <script type="text/javascript">
function $(id)
{
return document.getElementById(id);
} function initialize()
{
$("e1").style.left="0";
$("e1").style.top="0";
$("e1").style.width="100px";
$("e1").style.height="100px";
$("e1").onselectstart=function(){return false;};
} function areaDrag(box,element)
{ document.onmousemove=startDrag;
document.onmouseup=stopDrag;
var deltaX=event.clientX-(parseInt(box.currentStyle.left)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.left));
var deltaY=event.clientY-(parseInt(box.currentStyle.top)+parseInt(box.currentStyle.borderWidth)+parseInt(element.style.top));
function startDrag()
{
if(event.clientX-deltaX>=parseInt(box.currentStyle.left) && event.clientX-deltaX<=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2)
{
element.style.left=event.clientX-parseInt(box.currentStyle.left)-deltaX;
}
else
{
if(event.clientX-deltaX<parseInt(box.currentStyle.left))
{
element.style.left="0";
}
else
{
element.style.left=parseInt(box.currentStyle.width)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.width);
}
}
if(event.clientY-deltaY>=parseInt(box.currentStyle.top) && event.clientY-deltaY<=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2)
{
element.style.top=event.clientY-parseInt(box.currentStyle.top)-deltaY;
}
else
{
if(event.clientY-deltaY<parseInt(box.currentStyle.top))
{
element.style.top="0";
}
else
{
element.style.top=parseInt(box.currentStyle.height)-parseInt(box.currentStyle.borderWidth)*2-parseInt(element.style.height);
}
}
} function stopDrag()
{
document.onmousemove="";
}
}
</script>
</head>
<body onload="initialize()">
<div id="box">
<div onmousedown="areaDrag($('box'),this)" id="e1"></div>
</div>
<img src="http://www.cnblogs.com/images/cnblogs_com/random/world.jpg" />
</body>
</html>