JS穿墙效果(根据弧度,判断方向)

 

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

        }

        div {

            width: 200px;

            height: 200px;

            margin: 250px auto;

            border: 1px solid #333;

            position: relative;

            overflow: hidden;

        }

        span {

            width: 100%;

            height: 100%;

            background-color: yellow;

            opacity: 0.5;

            position: absolute;

            left: 0;

            top: 100%;

            pointer-events: none;

        }

    </style>

</head>

<body>

    <div>

        <span></span>

    </div>

    <script>

        let oDiv = document.querySelector('div');

        let oMask = document.querySelector('span');

        oDiv.οnmοuseοver=handler;

        oDiv.οnmοuseοut=handler;

        function handler (e) {

            //写出相对于css原点坐标

            let mouseX = e.offsetX;

            let mouseY = e.offsetY;

            // console.log('相对于css原点:', mouseX, mouseY);

            //写出虚拟的原点左边(在正方形的中心)

            let originX = Math.round(oDiv.offsetWidth / 2);

            let originY = Math.round(oDiv.offsetHeight / 2);

            // console.log('伪原点坐标为', originY, originX);

            //写出真实的坐标点,也就是真坐标-伪坐标

            let x = mouseX - originX;

            let y = mouseY - originY;

            // console.log('真实的坐标为',x,y);

            //接下来求弧度,弧度就是当一个点在圆周长上走一个半径的距离时,其夹角叫1弧度

            let h = Math.atan2(y, x);

            //该公式为求弧度公式,第一个参数是y,第二个参数是x

            // console.log(h)


 

            //利用拿到的弧度,转换成相对应的角度

            let de = Math.round(h * 180 / Math.PI);

            // console.log(de);

            //为了方便计算角度,讲旋转的坐标转,再旋转回来

            let deg = de + 180

            // console.log(deg)

            //再次简化讲四个方向转化为四个数值

            let dir = Math.round(deg / 90) % 4;

            // console.log(dir);



 

            if (e.type === 'mouseover')oMask.style.transition = 'none';

           

            switch (dir) {

                case 0:

                    oMask.style.left = '-100%';

                    oMask.style.top = 0;

                    break

                case 1:

                    oMask.style.left = 0;

                    oMask.style.top = '-100%';

                    break

                case 2:

                    oMask.style.left = '100%';

                    oMask.style.top = 0;

                    break

                case 3:

                    oMask.style.left = 0;

                    oMask.style.top = '100%';

                    break;

            }

            //制作四个case,判断那个方向进入,就实施哪个范围的定位,记得写break打断,不然会直接穿透到最后一个方向(case3);

            if (e.type === 'mouseover') {

                setTimeout(function () {

                    oMask.style.transition = 'all .3s';

                    oMask.style.left = 0;

                    oMask.style.top = 0;

                }, 30)

            }

            //给鼠标进入设置延迟

        }
 

    </script>


 

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值