Moving the mouse to enlarge the picture preview effect


In the mall project, there is the function of moving the mouse into the picture to enlarge. Study and realize it

<!DOCTYPE html>
<html lang="en">
        <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>Image zoom</title>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;

        #image {
            width: 300px;
            height: 300px;
            background-color: #000;
            background-image: url(;
            background-size: 300px 300px;
            background-repeat: no-repeat;

        #image[zoomed] {
            background-size: 900px 900px;
            background-position: calc(var(--x) * 100%) calc(var(--y) * 100%);
        <div id="image"></div>
        let el = document.querySelector('#image')

        //PC side operation
        el.addEventListener('mouseenter', enterHandler)
        el.addEventListener('mousemove', moveHandler)
        el.addEventListener('mouseleave', leaveHandler)

        //Mobile terminal operation
        el.addEventListener('touchstart', enterHandler)
        el.addEventListener('touchmove', moveHandler)
        el.addEventListener('touchend', leaveHandler)

        function enterHandler(e) {
  'zoomed', 1)

        function moveHandler(e) {
            //Getboundingclientrect is used to get the collection of positions of elements relative to the window. There are top, right, bottom, left and other attributes in the collection
            let rect =
            let offsetX, offsetY
            let isH5 = ['touchstart', 'touchmove', 'touchend'].includes(e.type)
            //Is the mobile terminal, and the touches event exists
            if (isH5 && e.touches[0]) {
                offsetX = e.touches[0].pageX - rect.left
                offsetY = e.touches[0].pageY -

            } else {
                //PC end
                offsetX = e.offsetX
                offsetY = e.offsetY
            //Element location information
            let x = offsetX / rect.width
            let y = offsetY / rect.height
            //Set the element attribute to calculate the position of the background position
  '--x', x)
  '--y', y)

        function leaveHandler(e) {

Copy the specific effect and open it

Recommended Today

Node learning record: mongodb

brief introduction TalkingMongoDBBefore data insertion, let’s briefly understand its data logical structure.The logical structure of mongodb is a hierarchical structure, which is mainly composed of: document, collectionDatabaseThese three parts are composed of. Document: composed of key / value pairs, like{a:1};{s:”abc”}It is the core unit of mongodb, the document of mongodb, which is equivalent to a […]