<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
#drag {
width: 200px;
height: 200px;
border: 1px solid #000;
}
</style>
<body>
<div>练习</div>
<div draggable="true" id="drag"></div>
<!-- <script src="./index.js"></script> -->
<script>
function throttle(fn, delay = 100) {
let timer = null
return function () {
if(timer) {
return
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
$drag = document.getElementById('drag')
$drag.addEventListener('drag', throttle(function(e) {
console.log(e.offsetX, e.offsetY)
}))
</script>
</body>
</html>
js 节流 throttle
最新推荐文章于 2024-07-20 14:49:34 发布