js 悬浮框 光标位置 页面滚动后的光标位置
想要写一个悬浮框,当划过某个区域的时候,这个悬浮框显示,光标在哪,悬浮框就在哪。
思路是:定义一个DIV,初始化时使之display: none,在光标划过指定位置时,将此DIV设置为display: block。使用onmouseover事件和onmouseout事件。
1.定义DIV
1 <div id="operationDiv" onmouseover="showDetail();" onmouseout="hideDetail();"> 2 <asp:CheckBoxList ID="cblComments" runat="server" OnSelectedIndexChanged="cblComments_SelectedIndexChanged" AutoPostBack="true"> 3 <asp:ListItem Text="Agree" Value="Agree" onclick="CheckCBLSimpleSelect(this)"></asp:ListItem> 4 <asp:ListItem Text="Disagree" Value="Disagree" onclick="CheckCBLSimpleSelect(this)"></asp:ListItem> 5 </asp:CheckBoxList> 6 </div>
其中onmouseover和onmouseout是我们需要用的事件,绑定对应的js方法。
2.悬浮框的显示和隐藏 js方法
悬浮框内显示的内容我已赋在hidShowDetail内。
1 //Display checked CheckBoxList number 2 function showDetail() { 3 var displayStr = document.getElementById("hidShowDetail").value; 4 var pointPosition = getCursorPosition(window.event);//get point relative position 5 document.getElementById("TotalSelectDIV").style.display = "block"; 6 document.getElementById("TotalSelectDIV").style.left = pointPosition[0] + "px"; 7 document.getElementById("TotalSelectDIV").style.top = pointPosition[1] + "px"; 8 document.getElementById("TotalSelectNum").innerText = displayStr; 9 10 } 11 12 //Hide checked CheckBoxList number 13 function hideDetail() { 14 document.getElementById("TotalSelectNum").innerText = ""; 15 document.getElementById("TotalSelectDIV").style.display = "none"; 16 }
其中getCursorPosition方法是为了获取当前光标位置,以前是用window.event.clientX + "px"和 window.event.clientY + "px"来表示光标的坐标,但因为页面滚动,获取的位置不对。后来查到了一个,不管页面如何滚动,都可以获取到实际光标的位置getCursorPosition方法。参考:https://blog.csdn.net/liangshui999/article/details/95059015
1 function getCursorPosition(event) { 2 var position = [0, 0]; 3 var scrollingPosition = getScrollingPosition(); 4 if (typeof event == "undefined") { 5 event = window.event; 6 } 7 if (typeof event.pageX != 'undefined') { 8 position = [event.pageX, event.pageY]; 9 } else { 10 position = [event.clientX + scrollingPosition[0], event.clientY + scrollingPosition[1]]; 11 } 12 return position; 13 } 14 15 function getScrollingPosition() { 16 var position = [0, 0]; 17 if (typeof window.pageXOffset != 'undefined') { 18 position = [window.pageXOffset, window.pageYOffset]; 19 } else if (typeof document.documentElement.scrollTop != 'undefined' && 20 (document.documentElement.scrollTop > 0 || document.documentElement.scrollLeft > 0)) { 21 position = [document.documentElement.scrollLeft, document.documentElement.scrollTop]; 22 } else if (typeof document.body.scrollLeft != 'undefined') { 23 position = [document.body.scrollLeft, document.body.scrollTop]; 24 } 25 return position; 26 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2021-11-30 C# .net 控制台应用程序 执行.ps1 PowerShell文件