Moving the mouse to enlarge the picture preview effect

Time:2022-1-2

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">
    <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>Image zoom</title>
    </head>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        #image {
            width: 300px;
            height: 300px;
            background-color: #000;
            background-image: url(https://placekitten.com/900/900);
            background-size: 300px 300px;
            background-repeat: no-repeat;
        }

        #image[zoomed] {
            background-size: 900px 900px;
            background-position: calc(var(--x) * 100%) calc(var(--y) * 100%);
        }
    </style>
    <body>
        <div id="image"></div>
    </body>
    <script>
        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) {
            e.target.setAttribute('zoomed', 1)
            moveHandler(e)
        }

        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 = e.target.getBoundingClientRect()
            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 - rect.top

                e.preventDefault()
            } 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
            e.target.style.setProperty('--x', x)
            e.target.style.setProperty('--y', y)
        }

        function leaveHandler(e) {
            e.target.removeAttribute('zoomed')
            moveHandler(e)
        }
    </script>
</html>

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 […]