[英]How to get the position of screen window of browser when scroll down jquery
[英]How to get the coords of browser window when scroll
當用戶滾動我的頁面時,我嘗試獲取瀏覽器窗口的坐標。
有沒有辦法通過滾動事件獲得窗口坐標?
mousemove
事件時,它工作正常,但是當我使用 scroll 事件時,它給了我undefined
。deltaX
、 clientX
、 pageX
事件一起使用。但是如何使用滾動事件來獲取和元素的坐標呢?
這是我的代碼'''javascript
window.addEventListener("scroll",function(e){
var x = event.clientX;
var y = event.clientY;
var coords = "X coords: " + x + ", Y coords: " + y;
console.log(coords);
});
'''
如果我將scroll
更改為諸如mousemove
之類的任何鼠標事件,它可以正常工作,但是在使用滾動時,它會給我未定義
因此,當用戶滾動我的頁面時,我需要任何幫助來獲取坐標。
我試過scrollHeight
、 deltaX|Y
、 pageX|Y
和clientX|Y
但沒有任何效果。
好的,試試這個
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.5.0.js"></script> <title></title> </head> <body> <div id="res"></div> <script type="text/javascript"> for (var i = 100; i >= 0; i--) { let x = document.createElement("h1"); x.innerHTML = "just text"; document.body.append(x) } $(window).scroll(function showCoords(event) { console.log(window.scrollY); }) </script> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.