jquery - 鼠标事件名称
问题描述
您可能会发现这很简单……我只是不知道我要查找的事件的名称。
现在,我存储图像的宽度和高度。我在单击的位置存储图像的宽度和高度。
当“在特定 div 中停止按下单击按钮”时,我想做同样的事情,以便获得单击的 x,y 开始位置和单击的 x,y 结束位置,以便能够拥有所有构建的矩形位置。
这就是我现在所拥有的
$(document).on("click", "#img_to_deal_with", function(e){
let maxH = $('#img_to_deal_with').height();
let maxL = $('#img_to_deal_with').width();
let posX = e.pageX - this.offsetLeft;
let posY = e.pageY - this.offsetTop;
console.log("Height : " + maxH);
console.log("Width : " + maxL);
console.log("Current Height : "+ posY);
console.log("Current Width :" + posX);
});
感谢你的协助
解决方案
mousedown:当用户按下鼠标按钮时触发。
mouseup:当用户释放鼠标按钮时触发。
click:当同一元素上发生 mousedown 和 mouseup 事件时触发。
来源
因此,您可能希望将mousedown和mouseup中的单击分开:
$(document).on("mousedown", "#img_to_deal_with", function(e){
let maxH = $('#img_to_deal_with').height();
let maxL = $('#img_to_deal_with').width();
let posX = e.pageX - this.offsetLeft;
let posY = e.pageY - this.offsetTop;
console.log("Height : " + maxH);
console.log("Width : " + maxL);
console.log("Down Height : "+ posY);
console.log("Down Width :" + posX);
}).on("mouseup", "#img_to_deal_with", function(e){
let maxH = $('#img_to_deal_with').height();
let maxL = $('#img_to_deal_with').width();
let posX = e.pageX - this.offsetLeft;
let posY = e.pageY - this.offsetTop;
console.log("Height : " + maxH);
console.log("Width : " + maxL);
console.log("Up Height : "+ posY);
console.log("Up Width :" + posX);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="http://lorempixel.com/output/abstract-q-c-300-150-9.jpg" id="img_to_deal_with" draggable="false">
推荐阅读
- go - html 模板返回文本而不是 html
- java - 更好:为什么资产图像目录包含我在设计时没有放在那里的文件?
- windows - 通过powershell脚本在任务管理器中固定powershell图标
- mysql - 检索按列分组的最后 2 条记录
- python - 仅将最后 X 行 csv 转换为 json
- html - 为一个元素分配了不同类别的引导程序,但是当分辨率改变时它没有响应
- reactjs - 对多播响应做出反应订阅
- javascript - undefined 不是对象(评估 'post.profile_picture' )
- liquibase - 如何从数据库中删除最后执行的 liquibase 变更集
- azure-active-directory - Power Automate 自定义连接器 - 令牌无效、无效受众