JS实现穿墙效果(判断鼠标划入划出的方向)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>判断鼠标移入移出方向</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        .outer {
            width: 400px;
            height: 400px;
            border: 2px solid orange;
            margin: 100px auto;
            overflow: hidden;

        }

        .outer div {
            width: 100%;
            height: 100%;
            background-color: yellow;
            opacity: 0.5;
            display: none;
        }
    </style>
</head>
<body>
<div id="box" class="outer">
    <div id="mask" style="left: 0;top:0;"></div>
</div>

<script type="text/javascript">

    var oDiv = document.getElementById(‘box‘);
    var oMask = document.getElementById(‘mask‘);

    oDiv.disL = oDiv.offsetLeft;
    oDiv.disT = oDiv.offsetTop;
    oDiv.disR = oDiv.disL + oDiv.offsetWidth;
    oDiv.disB = oDiv.disT + oDiv.offsetHeight;

    oDiv.addEventListener(‘mouseenter‘, moveIn, false);
    oDiv.addEventListener(‘mouseleave‘, moveOut, false);

    function moveIn(e) {
        var resL = Math.abs(e.clientX - oDiv.disL),//距离左边
                resT = Math.abs(e.clientY - oDiv.disT),//距离上边
                resR = Math.abs(e.clientX - oDiv.disR),//距离右边
                resB = Math.abs(e.clientY - oDiv.disB);//距离下边
        var min = Math.min(resL, resB, resR, resT);

        //console.log(resL, resB, resR, resT, min);
        if (min === resL) {
            console.log(‘左边移入‘);
            maskIn(‘left‘);
        } else if (min === resT) {
            console.log(‘上边移入‘);
            maskIn(‘top‘);
        } else if (min === resR) {
            console.log(‘右边移入‘);
            maskIn(‘right‘);
        } else {
            console.log(‘下边移入‘);
            maskIn(‘bottom‘);
        }
    }
    function moveOut(e) {
        var resL = Math.abs(e.clientX - oDiv.disL),//距离左边
                resT = Math.abs(e.clientY - oDiv.disT),//距离上边
                resR = Math.abs(e.clientX - oDiv.disR),//距离右边
                resB = Math.abs(e.clientY - oDiv.disB);//距离下边
        var min = Math.min(resL, resB, resR, resT);

        //console.log(resL, resB, resR, resT, min);
        if (min === resL) {
            //console.log(‘左边移出‘);
            maskOut(‘left‘);
        } else if (min === resT) {
            //console.log(‘上边移出‘);
            maskOut(‘top‘);
        } else if (min === resR) {
            //console.log(‘右边移出‘);
            maskOut(‘right‘);
        } else {
            //console.log(‘下边移出‘);
            maskOut(‘bottom‘);
        }

    }
    function maskIn(direction) {
        var attr = ‘marginTop‘, otherAttr = ‘marginLeft‘, step = -15, maxDis = ‘offsetHeight‘;
        if (direction === ‘left‘ || direction === ‘top‘) {
            step = 15;
        }
        if (direction === ‘left‘ || direction === ‘right‘) {
            attr = ‘marginLeft‘;
            maxDis = ‘offsetWidth‘;
            otherAttr = ‘marginTop‘;
        }
        clearInterval(oMask.timer);
        step < 0 ? oMask.style[attr] = oDiv[maxDis] + ‘px‘ : oMask.style[attr] = -oDiv[maxDis] + ‘px‘;
        oMask.style[otherAttr] = 0;
        oMask.style.display = ‘block‘;
        oMask.timer = setInterval(function () {
            var disL = parseFloat(oMask.style[attr]) + step;
            if (step > 0) {
                if (disL >= 0) {
                    oMask.style.marginLeft = 0;
                    oMask.style.marginTop = 0;
                    clearInterval(oMask.timer);
                } else {
                    oMask.style[attr] = disL + ‘px‘;
                }
            } else {
                if (disL <= 0) {
                    oMask.style.marginLeft = 0;
                    oMask.style.marginTop = 0;
                    clearInterval(oMask.timer);
                } else {
                    oMask.style[attr] = disL + ‘px‘;
                }
            }

        }, 10);
    }

    function maskOut(direction) {
        clearInterval(oMask.timer);
        oMask.style.marginLeft = 0;
        oMask.style.marginTop = 0;
        var attr = ‘marginTop‘, step = 15, maxDis = ‘offsetHeight‘;
        if (direction === ‘left‘ || direction === ‘top‘) {
            step = -15;
        }
        if (direction === ‘left‘ || direction === ‘right‘) {
            attr = ‘marginLeft‘;
            maxDis = ‘offsetWidth‘;
        }
        oMask.timer = setInterval(function () {
            var disL = parseFloat(oMask.style[attr]) + step;
            if (step < 0) {
                if (disL <= -oDiv[maxDis]) {
                    oMask.style.display = ‘none‘;
                    clearInterval(oMask.timer);
                } else {
                    oMask.style[attr] = disL + ‘px‘;
                }
            } else {
                if (disL >= oDiv[maxDis]) {
                    oMask.style.display = ‘none‘;
                    clearInterval(oMask.timer);
                } else {
                    oMask.style[attr] = disL + ‘px‘;
                }
            }
        }, 10);
    }
</script>
</body>
</html>
时间: 2024-08-29 17:27:30

JS实现穿墙效果(判断鼠标划入划出的方向)的相关文章

jQuery鼠标划入划出

今天来简单的谈谈jQuery的一个划入划出的方法,.首先划入划出能想到的东西有哪些呢,. 1:hover 2:mouseenter/mouseleave 3:mouseover/mouseout. 一共是三中方法,先来说说他们的使用方法 $(".evo").hover(function(){ $(this).addClass("red"); },function(){ $(this).removeClass("red"); }) 这样写有点乱,那

判断鼠标进入一个元素的方向(上下左右)

<style> html,body{margin:0;padding:0;} #wrap{width:300px;height:300px;background:#33aa00;margin:50px;display:inline-block;font-size:50px;text-align:center;line-height:300px;}</style> <div id="wrap"> 方向反馈 </div><script

判断鼠标从div的哪一方向划入-------Day74

发现一种很有意思的效果,感觉很漂亮很华丽,想实现一下,大体效果来介绍下: 鼠标从左端划入,则遮盖层从左边划入遮盖div,而从上方划入,则遮盖层从上方划入遮盖div,同理对于右方和下方,而同样从哪个方向鼠标移出div,遮盖层也就从哪个方向消失,你能想象的出么?如果还想象不出效果,可以看你一下"拉勾网",我就从上面发现的这个效果. 其实单纯说遮盖层划出的效果,我们大约知道好几种方法,如果遮盖层是需要新创建div,那么javascript完全可以实现,至于划出效果也可以实现,遮盖层的widt

JS判断鼠标移入元素的方向

最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { var w = this.offsetWidth; var h = this.offsetHeight; var x = e.pageX - this.getBoundingClientRect().left - w/2; var y = e.pageY - this.getBoundingClientRect

JS实现文字链接感应鼠标的解密效果

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>JS实现文字链接感应鼠标的解密效果丨石家庄展柜制作|石家庄铜门</TITLE> <META content="text/html; charset=gb2312" http-equiv=Content-Type> <STYLE&

js中判断鼠标滚轮方向的方法

  前  言 LiuDaP 最近无聊,在做自己的个人站,其中用到了一个关于鼠标滚轮方向判断的方法,今天闲来无聊,就给大家介绍一下吧!!!! 在介绍鼠标事件案例前,让我们先稍微了解一下js中的event对象 一.JS中的Event对象 Event对象:它代表的是事件的状态,例如可以表示鼠标的位置.鼠标按钮的状态.键盘按键的状态等等. >>>事件通常与函数结合使用,函数不会在事件发生前被执行! 二.JS中如何判断鼠标滚轮方向 判断鼠标滚轮的方向,有着两个派别:一是谷歌.IE派别(这次IE没有

js判断鼠标是否停止移动

本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0:timer可以实现每过n秒就判断鼠标状态,然后把imouse重置为0: 2.div监听onmouseover.当鼠标进入区域时,就设置定时器: 3.div监听onmousemove.当鼠标移动时,设置imouse值为1: 4.div监听onmouseout.当鼠标离开时,清除定时器timer <ht

js判断鼠标位置是否在某个div中

div的onmouseout事件让div消失时,会出现这样的情况,就是当鼠标移至div中的其它内容时,此时也判定为离开div,会触发 onmouseout事件,这样div中的内容就不能操作了.解决的办法是当触发onmouseout事件时,先判断鼠标是否在div内,如果在,说明鼠 标并没有离开div,就不删除div,否则,删除之.OK,现在问题解决了. 就是找到该div左上角和右下角坐标,判断鼠标的坐标是否在这一区域就可以了. [javascript] view plaincopyprint? d

JS学习笔记(一): 使用原生JS 实现导航栏下多级分类弹出效果

在给静态页面静添加交互效果时遇到的问题 : 鼠标划入二级菜单时 一级菜单样 ":hover" 式无法保持 情景如下: 解决思路: 利用二级菜单的onmouseover/out事件 重新构建一级菜单 ".hover" 样式类 代码如下: CSS部分: 在原来的目标:hover样式中 增加 .hover状态 li.app_jd a:hover,li.app_jd a.hover{ background-position: -126px -397px; } li.serv