根据滚动条位置显示div

时间:2011-08-29 11:06:42

标签: javascript jquery

我想使用jquery animate显示弹出窗口(div)(根据用户的情况,'top'和'left')

我希望根据页面中的用户位置显示此弹出窗口(顶部位置)(如果用户向下滚动页面,则poup将显示在此页面上用户的位置)

我尝试使用鼠标坐标,在Firefox中它工作正常,因为鼠标的位置是绝对到屏幕顶部,但在IE中鼠标的坐标是相对于屏幕顶部的用户的位置是。

这是获取鼠标坐标的代码

function getMouseXY(e) {

if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;

tempY = event.clientY + document.body.scrollTop;

} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
// catch possible negative values in NS4
if (tempX < 0) { tempX = 0 }
if (tempY < 0) { tempY = 0 }

}

如何相对于IE中的用户位置使用动画顶部和左侧?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果可以检测窗口大小与滚动条状态,则无需知道鼠标指针位置。

function getPosition(){
  var width = 100; // popup width
  var height = 100; // popup height

  return {
    top: (($(window).height()-height)/2)+$(document).scrollTop(),
    left: (($(window).width()-width)/2)+$(document).scrollLeft()
  }
}