js回到记录的xy坐标位置
时间: 2023-09-19 08:07:08 浏览: 37
您可以使用 `window.scrollTo(x, y)` 方法将页面滚动到指定的坐标位置。下面是一个简单的示例代码,可以回到之前记录的x、y坐标位置:
```html
<!DOCTYPE html>
<html>
<head>
<title>Go back to recorded position</title>
<script>
var xCoordinate;
var yCoordinate;
function recordClick(event) {
xCoordinate = event.clientX;
yCoordinate = event.clientY;
}
function goBack() {
window.scrollTo(xCoordinate, yCoordinate);
}
</script>
</head>
<body onclick="recordClick(event)">
<p>Click anywhere on the page to record the click coordinates.</p>
<button onclick="goBack()">Go back to the click location</button>
<p>Scroll down and click the button to go back to the recorded position.</p>
<button onclick="goBack()">Go back to the recorded position</button>
</body>
</html>
```
在这个示例代码中,当用户单击页面上的任意位置时,页面会记录鼠标单击的x、y坐标。然后,当用户单击“回到记录位置”按钮时,页面将滚动到之前记录的x、y坐标位置。