javascript div定位

标签 javascript html

我正在使用此脚本在鼠标悬停时将图像显示为弹出窗口。我面临的困难是它在不同的显示器中定位不佳。这一定与分辨率有关。

function LargeImage(obj, e)
{
  var imgbtn=document.getElementById('<%=imgbtn1.ClientID%>');  
  imgbtn.src=obj;//source of the image
  document.getElementById('imgbox').style.visibility="visible";
  document.getElementById('imgbox').style.position="absolute";
  document.getElementById('imgbox').style.left=e.clientX-150 + "px";
  document.getElementById('imgbox').style.top=225 +"px"; 
} 

<div id="imgbox"><asp:imagebutton id="imgbtn1" runat="server" OnClick="ImageButton4_Click"/></div>

谢谢。

最佳答案

你可以这样做

document.getElementById('imgbox').style.position="fixed";
document.getElementById('imgbox').style.left=e.clientX + "px";
document.getElementById('imgbox').style.top= e.clientY + "px"; 

这将在窗口 中的鼠标位置显示图片(如果用户滚动,弹出窗口将保持不变)。

否则你需要补偿文档滚动,比如

编辑:修复滚动值(对于 firefox)

document.getElementById('imgbox').style.position="absolute";
document.getElementById('imgbox').style.left=String(e.clientX+document.documentElement.scrollLeft)+"px";
document.getElementById('imgbox').style.top=String(e.clientY+document.documentElement.scrollTop)+"px"; 

您可以在这里找到一个演示,该演示确定要通过浏览器读取哪个属性以获取滚动偏移量 http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

关于javascript div定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2737272/

相关文章:

Javascript:控制台日志记录

javascript - 将 php 变量传递给 angular

javascript 添加带有 onmouseover 属性的区域元素

javascript - 当元素隐藏时如何禁用 `accesskey`?

c# - 如何像 Stackoverflow 上那样创建 "drag and select"图表?

javascript - Mouseup 事件在 Swiper 中不起作用

javascript - 为什么在给定的 div 中不应用 css?

javascript - jQuery datepicker 未显示,初始化调用已执行

html - 选项卡大小设置不正确

UIWebView 中的 HTML 表格宽度