我把代码重新修改了一下,加了点debug的东西在movepopup函数里面,显示两次执行的时间间隔,发现每次都不一样,而且还有时间为0的,奇怪,并非按100ms的间隔执行! <html> <head> <title>try</title> <script language="javascript"> /** showpopup.js V 1.0 3/17/2006 Copyright Jacky Huang [email protected] */function hidePopup() { // hide popup window (when rollout...) var popup = document.getElementById('popup'); popup.style.visibility='hidden'; window.clearTimeout(id); } function isArrived(popup){ // check if the popup window is moved to the target place, mouse location // return true if it is arrived, otherwise else. var x = popup.offsetTop; var y = popup.offsetLeft; return ((x==popup.targetX)&&(y==popup.targetY)); }function getIEBody(){ return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body } function showPopup(evt,content){ //show popup window with the content
//get the event handle, and fit the IE and MF compatibility evt = evt ? evt : (window.event ? window.event : null); //get the current mouse position nowX = MF ? evt.pageX : evt.clientX + getIEBody().scrollLeft; nowY = MF ? evt.pageY : evt.clientY + getIEBody().scrollTop; //get the handle of popup window var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : ""; popup.innerHTML = content; //set the target position of the popup window to the mouse position popup.targetX = nowX - popup.clientWidth - hPadding; popup.targetY = nowY + vPadding; //set the popup window visible popup.style.visibility = 'visible';
而且每一次movePopup函数都与上次无关,怎么会两速度不一样呢?
var nowx = parseInt(popup.offsetLeft);
var nowy = parseInt(popup.offsetTop);
你试试
会不会是时间函数的问题?setTimeout??会不会产生了很多个settimeout?
<html>
<head>
<title>try</title>
<script language="javascript">
/**
showpopup.js
V 1.0
3/17/2006
Copyright Jacky Huang
[email protected]
*/function hidePopup() {
// hide popup window (when rollout...)
var popup = document.getElementById('popup');
popup.style.visibility='hidden';
window.clearTimeout(id);
}
function isArrived(popup){
// check if the popup window is moved to the target place, mouse location
// return true if it is arrived, otherwise else.
var x = popup.offsetTop;
var y = popup.offsetLeft;
return ((x==popup.targetX)&&(y==popup.targetY));
}function getIEBody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function showPopup(evt,content){
//show popup window with the content
//get the event handle, and fit the IE and MF compatibility
evt = evt ? evt : (window.event ? window.event : null); //get the current mouse position
nowX = MF ? evt.pageX : evt.clientX + getIEBody().scrollLeft;
nowY = MF ? evt.pageY : evt.clientY + getIEBody().scrollTop;
//get the handle of popup window
var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : "";
popup.innerHTML = content; //set the target position of the popup window to the mouse position
popup.targetX = nowX - popup.clientWidth - hPadding;
popup.targetY = nowY + vPadding;
//set the popup window visible
popup.style.visibility = 'visible';
movePopup();}function movePopup(){
//move popup window
//get popup window handle
var popup = document.getElementById("popup");
//get current position
var nowx = parseInt(popup.offsetLeft);
var nowy = parseInt(popup.offsetTop);
if(!isArrived(popup)){
// if not arrived, move
if(nowx < popup.targetX){
//nowx++;
nowx=parseInt(nowx)+1;
}
if(nowx > popup.targetX){
//nowx--;
nowx=parseInt(nowx)-1;
}
if(nowy < popup.targetY){
//nowy++;
nowy=parseInt(nowy)+1;
}
if(nowy > popup.targetY){
//nowy--;
nowy=parseInt(nowy)-1;
}
//str =nowx+","+nowy+"<br />";
t2 = t1;
var d = new Date();
t1 = d.getTime();
str = str + (t1-t2) + "<br />";
//str = str + "<br/>" + (nowx-oldx)+","+(nowy-oldy);
document.getElementById("output").innerHTML =str;
popup.style.left = parseInt(nowx);
popup.style.top = parseInt(nowy);
id = window.setTimeout("movePopup()",100);
}
}//init IE and MF browser tags
var IE = document.all;
var MF = document.getElementById && !document.all;var id;
var str='';
var t1 = 0;
var t2 = 0;var nowX = 0;
var nowY = 0;var hPadding = 25;
var vPadding = 25;</script></head>
<body>
<div id="popup" style="position:absolute;left:0; top:0; visibility:hidden; background-color:#FFFFFF; border-color:#FF0000; border-style:solid; border-width:1; padding:3px; width:100px; font-family:Verdana; font-size:11px; z-index:100;"></div>
<div style="position:absolute;top:100;left:100">
<a onmousemove="showPopup(event,'hello')" onmouseout="hidePopup()">test1</a>
</div>
<div style="position:absolute;top:500;left:400">
<a onmousemove="showPopup(event,'hello')" onmouseout="hidePopup()">test2</a>
</div>
<div id="output" style="position:absolute;left:500;top100;background-color:#FFFFFF; border-color:#FF0000; border-style:solid; border-width:1; padding:3px; width:300px; height:500px; font-family:Verdana; font-size:11px; z-index:100;"></div>
</body>
</html>
function movePopup(){
clearSetTimeout(oID)
//move popup window//get popup window handle
var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : "";//get current position
var nowx = popup.offsetLeft;
var nowy = popup.offsetTop;if(!isArrived(popup)){
// if not arrived, move
if(nowx < popup.targetX){
nowx++;
}
if(nowx > popup.targetX){
nowx--;
}
if(nowy < popup.targetY){
nowy++;
}
if(nowy > popup.targetY){
nowy--;
}popup.style.left = nowx;
popup.style.top = nowy;oID = window.setTimeout("movePopup()",100);
}
}
不过这个解决了,但是速度特别慢,即使我设置时间间隔为1也比较慢,能不能有什么办法让它再快点?改步进值吗?