Thuộc tính window.screenX trả về tọa độ ngang (horizontal) của cửa sổ trình duyệt so với màn hình. Hỗ trợ tất cả trình duyệt hiện đại.
Cú pháp
window.screenX
Ví dụ
<!DOCTYPE html>
<html>
<style>
body { color: #000; height: 100vh; background-color: #8BC6EC; background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%); text-align: center; }
.btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
.show { font-size: 1.2rem; }
</style>
<body>
<h1>Demo window.screenX</h1>
<button onclick="showScreenX()" class="btn">Xem tọa độ X màn hình</button>
<div class="show"></div>
<script>
function showScreenX() {
document.querySelector('.show').innerHTML = 'Tọa độ ngang cửa sổ so với màn hình: ' + window.screenX + 'px';
}
</script>
</body>
</html>
Kết quả
Nhấn nút → hiển thị khoảng cách từ cạnh trái màn hình đến cửa sổ trình duyệt.
Kết hợp screenX + screenY = vị trí tuyệt đối của cửa sổ trên màn hình. Dùng cho: định vị popup, multi-window apps, analytics.