简体   繁体   中英

Detect mouse speed using jQuery

I want to detect mouse speed, for example the event will be fired only if the detected number is greater than 5

I found one example which I think is something similar: http://www.loganfranken.com/blog/49/capturing-cursor-speed/

but i am not able to make it see the number which is inside div and fire the event based on the resulting number, this is my attempt:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.cursometer.1.0.0.min.js"></script>

<script>

$(function() {

var $speedometer = $('#speedometer');

$('#test-area').cursometer({
    onUpdateSpeed: function(speed) {
        $speedometer.text(speed);
    },
    updateSpeedRate: 20
});




    $("#test-area").mouseover(function(){

    if(this.value > 5) {
        console.log("asdasd");
    }

    else {
        console.log("bbb");
    }



})

});

</script>

<style>
#test-area
{
    background-color: #CCC;
    height: 300px;
}
</style>

</head>

<body>
<div id="test-area"></div>
<div id="speedometer"></div>


</body>
</html>

(I'm the author of the plug-in that's causing the trouble here)

The example isn't working because this.value isn't referring to the speed (it's undefined ). Here's an updated version of your example: http://jsfiddle.net/eY6Z9/

It would probably be more efficient to store the speed value in a variable rather than within the text value of an HTML element. Here's an updated version with that enhancement: http://jsfiddle.net/eY6Z9/2/

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM