布尔函数来知道鼠标按钮的位置
boolean function to know position of mousebutton
是否可以检查鼠标按钮的状态(按下或释放)。我知道这就是jquery中事件处理的目的,但我想知道,如果不需要更改按钮的位置(按下按钮即可释放,反之亦然)就不按下鼠标按钮,是否可以执行功能?
尝试以下代码,DEMO此处
$(document).mousedown (function () {
$('#result').text('Pressed');
}).mouseup (function () {
$('#result').text('Released');
});
$(function () {
//setup flag for the state of the mouse button and create an object that converts an event type into a boolean for the flag
var mousePressed = false,
mouseConvert = {
mousedown : true,
mouseup : false
};
//bind to the document's mousedown and mouseup events to change the flag when necessary
$(document).on('mousedown mouseup', function (event) {
//set the flag to the new state of the mouse button
mousePressed = mouseConvert[event.type];
//if you only want to watch one mouse button and not all of them then
//you can create an if/then statement here that checks event.which
//and only updates the flag if it is the button you want, check-out Rob W.'s
//answer to see the different values for event.which with respect to the mouse
});
//now you can check the value of the `mousePressed` variable, if it is equal to `true` then the mouse is currently pressed
});
您可以使用mousedown
和mouseup
事件来检测更改的鼠标事件。此事件应绑定到window
演示:http://jsfiddle.net/uwzbn/
var mouseState = (function(){
var mousestatus = 0;
$(window).mousedown(function(e) {
mousestatus = e.which;
}).mouseup(function(e){
mousestatus = 0;
});
return function() {
return mousestatus;
}
})();
此函数返回四个可能的值:
0 Mouse released false
1 Left click true
2 Middle click true
3 Right click true
在布尔上下文中,当鼠标未按下时,函数返回的值计算为false。当按下鼠标时,您可以读取按下的鼠标键(作为奖励)。
相关文章:
- jquery试图按名称获取按钮位置
- 更改后退按钮的指向位置
- 在TWebBrowser(谷歌地图)中显示位置,而无需单击按钮
- JavaScript幻灯片显示,每个幻灯片的下一个按钮都有不同的位置
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 按下后退按钮时,如何保持可滚动区域的滚动位置
- 流星应用程序中的按钮位置错误
- 获得点击按钮的正确位置(涟漪效应)
- HTML 按钮以在页面上的其他位置提交表单
- 使用地理位置创建一个显示我当前位置的按钮
- 当我使用html5画布单击按钮时,要更改矩形的位置
- 我希望每次单击按钮时都能将每张图片更改为显示在屏幕上的同一位置
- 点击一个按钮,谷歌地图应该至少适合3个离你当前位置点最近的标记
- 使用Window_handles在位置弹出窗口中单击按钮,并带有守夜功能
- 为什么jqueryUI按钮和按钮集的位置不同
- 创建存储按钮单击位置的数组
- 浏览器的“上一个”按钮位置更改
- 导出按钮位置更改 IE 11 中的高图表
- 如果按钮位置发生变化,ZClip SWF不会与按钮位置对齐
- 浏览器后退按钮位置问题