Home > Web Front-end > JS Tutorial > body text

JQuery uses mousedown and mouseup to determine whether the mouse press and release positions are the same

黄舟
Release: 2018-05-22 09:31:33
Original
2506 people have browsed it

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{
            //位置不同的操作
        }
    })
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!