Home >Web Front-end >JS Tutorial >JQuery uses mousedown and mouseup to determine whether the mouse press and release positions are the same
In JQuery, we can use mousedown and mouseup to track the mouse press and release events of the page.
How to obtain the mouse position information? The pageX and pageY attributes of the event allow us to obtain the specific position information of the mouse on the page, which is equivalent to the xy coordinates in the mathematical coordinate system.
So we can first use a global variable to record the position information of the mouse press, and then judge the pressed position and release in the mouse release function Check whether the positions are consistent, and then perform operations based on the results.
The sample code is as follows:
var x,y; $(document).mousedown(function(event){ //获取鼠标按下的位置 x = event.pageX; y = event.pageY; }); $(document).mouseup(function(event){//鼠标释放 var newX = event.pageX; var newY = event.pageY; if(x==newX && y==newY){ //位置相同的操作 } else{ //位置不同的操作 } })
The above is the detailed content of JQuery uses mousedown and mouseup to determine whether the mouse press and release positions are the same. For more information, please follow other related articles on the PHP Chinese website!