使用ie的setCapture方法之后,这个方法可以把一个对象“牢牢抓住”。我一直再找非ie情况下的替代方法但是一直都没找到。所以,请大家指点一下了。我先给出一段代码: 这段代码如果运行在ie下,当你用鼠标点击那个蓝色的div时,你不松鼠标,可以把它拖拽到ie浏览器外的地方。 如果这段代码运行在ff下,鼠标离开浏览器时,div就脱离了控制了。代码如下: <body style="width:100%;height:100%;">
<div id="boat" style="position:absolute;background-color:#003366;width:200px;height:200px;"></div>
</body>
<script>
if(document.all){
document.getElementById('boat').onclick=function(){
document.getElementById('boat').setCapture();
};
document.body.onmousemove=function(ep){
document.getElementById('boat').style.left=mouseCoords(ep).x+"px";
}
}else{
document.getElementById('boat').addEventListener("click",function(){
window.captureEvents(Event.Click);
},true);
document.body.addEventListener("mousemove",function(ep){
document.getElementById('boat').style.left=ep.clientX;
//alert(ep.clientX);
},true);
}
function mouseCoords(ev) {
ev = ev || window.event;
if (ev.pageX || ev.pageY) {
return {
x : ev.pageX,
y : ev.pageY
};
}
return {
x : ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y : ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
</script>所以大家帮帮想想办法,怎么才能找到非ie下,牢牢抓住对象的方法,以解决我说的这个问题为准。如果能解决,再开贴加100分。
<div id="boat" style="position:absolute;background-color:#003366;width:200px;height:200px;"></div>
</body>
<script>
if(document.all){
document.getElementById('boat').onclick=function(){
document.getElementById('boat').setCapture();
};
document.body.onmousemove=function(ep){
document.getElementById('boat').style.left=mouseCoords(ep).x+"px";
}
}else{
document.getElementById('boat').addEventListener("click",function(){
window.captureEvents(Event.Click);
},true);
document.body.addEventListener("mousemove",function(ep){
document.getElementById('boat').style.left=ep.clientX;
//alert(ep.clientX);
},true);
}
function mouseCoords(ev) {
ev = ev || window.event;
if (ev.pageX || ev.pageY) {
return {
x : ev.pageX,
y : ev.pageY
};
}
return {
x : ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y : ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
</script>所以大家帮帮想想办法,怎么才能找到非ie下,牢牢抓住对象的方法,以解决我说的这个问题为准。如果能解决,再开贴加100分。
if(Obj.setCapture){
Obj.setCapture();
}else if(window.captureEvents){
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
<body style="width:100%;height:100%;">
<div id="boat" style="position:absolute;background-color:#003366;width:200px;height:200px;"></div>
</body>
<script>
var o=document.getElementById('boat');function mousedown(){
if(o.setCapture){
o.setCapture();
}else if(window.captureEvents){
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
}function move1(ev){
o.style.left=mouseCoords(ev).x+"px";
}
function mouseup(){
if(o.releaseCapture){
o.releaseCapture();
}else if(window.captureEvents){
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}}document.onmousedown=mousedown;
document.onmousemove=function(event){move1(event)}
document.onmouseup=mouseup;
function mouseCoords(ev) {
ev = ev || window.event;
if (ev.pageX || ev.pageY) {
return {
x : ev.pageX,
y : ev.pageY
};
}
return {
x : ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y : ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>dragDiv.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
font-family:Verfana;
font-size:11px;
color:#333333;
}
#win {
position:absolute;
left:50px;
top:50px;
width:200px;
height:150px;
border:1px solid #000000;
background:yellow;
}
</style>
<script type="text/javascript">
var win;
var left = 50;
var top = 50;
var move = false;
function init() {
win = document.getElementById("win");
win.onmousedown = startDrag;
win.onmousemove = drag;
win.onmouseup = stopDrag;
}
window.onload = init;
function startDrag(event) {
event = event || window.event;
var x = event.pageX || event.x;
var y = event.pageY || event.y;
left = x - left;
top = y - top;
win.style.background = "red";
move = true;
}
function drag(event) {
if(move) {
event = event || window.event;
win.style.background = "blue";
var x = event.pageX || event.x;
var y = event.pageY || event.y;
win.style.left = x - left + "px";
win.style.top = y - top + "px";
//captureEvents();
//win.setCapture();
if (!window.captureEvents) {
win.setCapture();
} else {
captureEvents();
//window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
}
}
function stopDrag(event) {
event = event || window.event;
win.style.background="yellow";
var x = event.pageX || event.x;
var y = event.pageY || event.y;
left = x - left;
top = y - top;
move = false;
//routeEvent();
//win.releaseCapture();
if (!window.releaseEvents) {
win.releaseCapture();
} else {
releaseEvents();
//window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
}
</script>
</head>
<body>
<div id="win">
</div>
</body>
</html>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/IBM_hoojo/archive/2010/07/02/5708697.aspx
解决方法:
IE:
obj.setCapture();
obj.releaseCapture();
FF:
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
if (!window.captureEvents) {
o.setCapture();
}else {
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
if (!window.captureEvents) {
o.releaseCapture();
}else {
window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
document.addEventListener("mousemove",function(ep){
你要用document来绑定
不是body
我是楼主,你的代码,运行后,鼠标如果跑的快了,div会跟不上,那么我先结贴了,你如果有异议,我们再商量。