javascript - 根据单击的鼠标位置定位 div
问题描述
我需要显示一个弹出窗口,其位置与角度 4 中单击的鼠标坐标相关。一个完美的例子是在谷歌日历中创建事件
解决方案
在您的 HTML 中:
<div class="click-container" (click)="onMouseClick($event)"></div>
在您的 TS 中:
onMouseClick(e: MouseEvent) {
console.log(e);
//e.pageX will give you offset from left screen border
//e.pageY will give you offset from top screen border
//determine popup X and Y position to ensure it is not clipped by screen borders
const popupHeight = 400, // hardcode these values
popupWidth = 300; // or compute them dynamically
let popupXPosition,
popupYPosition
if(e.clientX + popupWidth > window.innerWidth){
popupXPosition = e.pageX - popupWidth;
}else{
popupXPosition = e.pageX;
}
if(e.clientY + popupHeight > window.innerHeight){
popupYPosition = e.pageY - popupHeight;
}else{
popupYPosition = e.pageY;
}
}
然后您需要使用相关代码初始化弹出组件。
推荐阅读
- solidity - 如何在使用 web3 进行测试时将我的参数传递给构造函数
- python - 使用python添加取决于2 df的列
- java - 将 Hibernate 升级到 5.4.1 后如何导出架构?
- python - 如何在 keras 中实现高斯噪声攻击?
- validation - graphql apollographql 验证错误(嵌套错误 obj)
- javascript - 如何在反应中使用多个`导出默认`
- hash - 为什么 Redis dict 中的负载因子设置为 1
- xslt - 如何在 xsl:if 中创建自增数
- superuser - Django 2 oauth2 工具包以编程方式注册应用程序
- java - 显示隐藏的html代码的按钮?