gpt4 book ai didi

javascript - jQuery 仅在单击“按住”时监听

转载 作者:行者123 更新时间:2023-12-02 14:01:15 28 4
gpt4 key购买 nike

在我的控制台中,我的以下脚本不仅在单击鼠标滚轮时开始记录,还在您停止按住鼠标滚轮按钮后继续记录。

var mPosX,
mPosY;

$(document).on('mousedown', function (e) {
"use strict";
if( e.which === 2 ) {
e.preventDefault();
mPosX = event.pageX;
mPosY = event.pageX;
$(document).mousemove(function(event){
var CmPosX = event.pageX,
CmPosY = event.pageX;
console.log('Original X: ' + mPosX + ', New X: ' + CmPosX + ' | Original Y: ' + mPosY + ', New Y: ' + CmPosY);
});
}
});

如何让我的脚本仅在按住鼠标滚轮时记录并在松开鼠标滚轮时停止?

最佳答案

您可以在 mousedownmouseup 中存储一些内容,您可以在 mousemove 中访问这些内容

$(document).on({
mousedown: function(e) {
e.preventDefault();

if (e.which === 2)
$(window).data('isDown', true).data('mPosX', e.pageX).data('mPosY', e.pageY);
},
mouseup: function(e) {
e.preventDefault();

if (e.which === 2)
$(window).data('isDown', false);
},
mousemove: function(e) {
if ($(window).data('isDown')) {
var CmPosX = e.pageX,
CmPosY = e.pageY,
mPosX = $(window).data('mPosX'),
mPosY = $(window).data('mPosY');

console.log('Original X: ' + mPosX +
', New X: ' + CmPosX +
' | Original Y: ' + mPosY +
', New Y: ' + CmPosY);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hold down mousewheel and move ...</p>

关于javascript - jQuery 仅在单击“按住”时监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40384466/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com