试试这个小提琴:
html:
<button id="button">Click here</button><br/>
Status: <span id="status"></span>
Javascript:
observeTriState("#button", function(state) {
var states = { '0':'Push', '1':'Release', '2':'Hold Down' };
$("#status").text(states[state]);
}, 500);
function observeTriState(selector, callback, holdDelay) {
var mouseDown = false;
var mouseIn = false;
var interval;
function checkStatus() {
if (mouseDown && mouseIn) {
callback(2)
}
}
$(selector).mousedown(function() {
callback(0);
mouseDown = true;
interval = setInterval(checkStatus, holdDelay);
}).mouseup(function() {
mouseDown = false;
callback(1);
clearInterval(interval);
}).mouseenter(function() {
mouseIn = true;
}).mouseleave(function() {
mouseIn = false;
mouseDown = false;
clearInterval(interval);
callback(1);
});
}