<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.container{
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
overflow: hidden;
position: relative;
}
.box{
width:50px;
height: 50px;
position: absolute;
background: green;
}
</style>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
<script>
let tag = document.querySelector('.box');
let con = document.querySelector('.container');
tag.onmousedown = () =>{
con.onmousemove = (e)=>{
tag.style.top = tag.offsetTop + e.movementY +'px';
tag.style.left = tag.offsetLeft + e.movementX +'px';
}
}
window.onmouseup = () =>{
con.onmousemove = null;
}
tag.addEventListener('wheel',(e)=>{
let scal = 1;
if(e.deltaY<0){
//向上滚动
scal = 1.1
}else{
//向下滚动
scal = 0.9
}
tag.style.width = tag.offsetWidth*scal +'px'
tag.style.height = tag.offsetHeight*scal +'px'
tag.style.left = tag.offsetLeft + e.offsetX*(1-scal) +'px'
tag.style.top = tag.offsetTop + e.offsetY*(1-scal) +'px'
})
</script>
</html>
js 拖拽和以鼠标为中心的滚动缩放
于 2024-04-16 15:43:53 首次发布
本文介绍了如何使用HTML、CSS和JavaScript实现一个可拖动的绿色方块,同时通过鼠标滚轮控制方块的大小和位置变化。
摘要由CSDN通过智能技术生成