js----事件案例----滚轮缩放图片

发布于:2022-12-14 ⋅ 阅读:(373) ⋅ 点赞:(0)

<!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>

    <style>

        *{

            margin: 0;

            padding: 0;

            list-style: none;

        }

        body{

            height: 100vh;

            display: flex;

            justify-content: center;

            align-items: center;

        }

        img{

            width: 200px;

        }

    </style>

</head>

<body>

    <img src="宝宝.webp" alt="">

</body>

<script src="js/myWheel.js"></script>

<script>

    var img=document.querySelector("img")

    var nowScale=1.0  //当前的缩放倍数

    var maxScale=3.0  //最大的缩放倍数

    var minScale=0.25 //最小的缩放倍数

    // 规定:向上滑动 放大;向下滑动 缩小

    myWheel(img,function(isDown){

         if(isDown){

            // 缩小

            nowScale-=0.05

            nowScale=nowScale<minScale?minScale:nowScale

         }else{

            // 放大

            nowScale+=0.05

            nowScale=nowScale>maxScale?maxScale:nowScale

         }

        img.style.transform=`scale(${nowScale})`

    },false)

</script>

</html>

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到