jQuery event.pageY 屬性



jQuery 的 event.pageY 屬性用於查詢滑鼠指標相對於文件頂部邊緣的垂直位置。

此 jQuery 屬性在觸發諸如:click、mousemove 等事件時提供滑鼠指標的 Y 座標。

語法

以下是 jQuery event.pageY 屬性的語法:

event.pageY

引數

此屬性不接受任何引數。

返回值

此屬性返回滑鼠指標的垂直位置。

示例 1

以下示例演示了 jQuery event.pageY 屬性的使用:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <p>Click the mouse pointer relative to the top edge of the document</p>
    <p>Mouse pointer y coordinate: <span></span></p>
    <script>
        $(document).click(function(){
            $('span').text(event.pageY);
        })
    </script>
</body>
</html>

輸出

以上程式在單擊時返回滑鼠指標的 Y 座標,相對於文件頂部邊緣,例如:


示例 2

以下是使用 jQuery event.pageY 屬性的另一個示例。我們使用此屬性查詢特定區域的滑鼠指標的 Y 座標:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        div{
            width: 300px;
            height: 300px;
            background-color: green;
        }
    </style>
</head>
<body>
    <p>Move the mouse pointer relative to the top edge in the below box</p>
    <p>Mouse pointer y coordinate: <span></span></p>
    <div>

    </div>
    <script>
        $('div').mousemove(function(){
            $('span').text(event.pageY);
        })
    </script>
</body>
</html>

輸出

執行以上程式後,將顯示一個帶有綠色背景的框。當滑鼠指標在框內移動時,將顯示 Y 座標,例如:


示例 3

讓我們結合 event.pageXevent.pageY 屬性來同時查詢 X 和 Y 座標,它們表示滑鼠指標的位置:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        span{
            color: green;
            font-size: 20px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <p>Move the mouse pointer on your browser screen</p>
    <p>X coordinate: Y coordinates <span></span></p>
    <div></div>
    <script>
        $(document).mousemove(function(){
            $x = event.pageX;
            $y = event.pageY;
            $('span').text($x + " : " + $y);
        })
    </script>
</body>
</html>

輸出

當滑鼠指標在瀏覽器螢幕上移動時,X 和 Y 座標將顯示在螢幕上,例如:


jquery_ref_events.htm
廣告

© . All rights reserved.