Skip to content

Instantly share code, notes, and snippets.

@plugn
Created November 24, 2025 23:20
Show Gist options
  • Select an option

  • Save plugn/3a1b1e1275441d0603e4278a3b3bf4bb to your computer and use it in GitHub Desktop.

Select an option

Save plugn/3a1b1e1275441d0603e4278a3b3bf4bb to your computer and use it in GitHub Desktop.
body {
min-height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
}
div {
width: 105px;
height: 105px;
background: #ccddff;
padding: 5px;
}
<div id="scaling-box">Scale me with your mouse wheel.</div>
/**
* https://developer.mozilla.org/en-US/docs/Web/API/Element/wheel_event#examples
*/
let scale = 1
const el = document.querySelector('#scaling-box')
function applyZoom(event) {
event.preventDefault()
scale += event.deltaY * -0.01
// Restrict scale
scale = Math.min(Math.max(0.125, scale), 4)
// Apply scale transform
el.style.transform = `scale(${scale})`
}
el.onwheel = applyZoom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment