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

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

  1. jQuery鼠标划入划出

    今天来简单的谈谈jQuery的一个划入划出的方法,.首先划入划出能想到的东西有哪些呢,. 1:hover 2:mouseenter/mouseleave 3:mouseover/mouseout. 一 ...

  2. JS用斜率判断鼠标进入DIV四个方向的方法 判断鼠标移入方向

    本文要介绍的是一种鼠标从一个元素移入移出时,获取鼠标移动方向的思路.这个方法可以帮助你判断鼠标在移入移出时,是从上下左右的哪个方向发生的.这个思路,是我自己琢磨出来,利用了一点曾经高中学过的数学知识, ...

  3. CSS动画划入划出酷炫

    HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...

  4. 通信录分组并且分组标签悬停划入划出(包含错误信息及修改)--第三方开源--PinnedSectionListView

    PinnedSectionListView在github上的链接地址是:https://github.com/beworker/pinned-section-listview . 下载下来后直接将Pi ...

  5. NGUI中 鼠标划出屏幕后,停止对 UIDragScrollView 的 press

    using UnityEngine; /// <summary> /// NGUI中 鼠标划出屏幕后,停止对 UIDragScrollView 的 press /// </summa ...

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

    最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...

  7. JS判断鼠标从什么方向进入一个容器

    偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题.首先想到的是给容器的四个边添加几个块,然后看鼠标进入的时候哪个块先监听到鼠标事件.不过这样麻烦太多了.google了一下找到了一个不错的解决方 ...

  8. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

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

      前  言 LiuDaP 最近无聊,在做自己的个人站,其中用到了一个关于鼠标滚轮方向判断的方法,今天闲来无聊,就给大家介绍一下吧!!!! 在介绍鼠标事件案例前,让我们先稍微了解一下js中的event ...

随机推荐

  1. 使用scatter画散点图

    刚开始接触Python,照着例子写的代码,百度注释的. from numpy import * import matplotlib import matplotlib.pyplot as plt im ...

  2. vue踩坑- 报错npm ERR! cb() never called!

    在vue项目中引入饿了么elementUI组件的步骤之中,出现以下的错误: D:\my-project-first>npm i element-ui -S Unhandled rejection ...

  3. 【CS Round #37 (Div. 2 only) A】Boring Number

    [Link]:https://csacademy.com/contest/round-37/task/boring-number/ [Description] 让你找离平均数最近的一个数的下标; [S ...

  4. 洛谷 P1719 最大加权矩形

    P1719 最大加权矩形 题目描述 为了更好的备战NOIP2013,电脑组的几个女孩子LYQ,ZSC,ZHQ认为,我们不光需要机房,我们还需要运动,于是就决定找校长申请一块电脑组的课余运动场地,听说她 ...

  5. [Python] Understand Mutable vs. Immutable objects in Python

    In this lesson, you will learn what mutable and immutable objects are, and the difference between th ...

  6. 測试password强度

    <html> <!--激情在最后面.请看最后面红色字 这是是个计算password强度的实例 网上有非常多这种样例 只是呢,都不怎么好 这是我写的一个完整的效果,能够通用, new一 ...

  7. Android中关于Volley的使用(十)对Request和Reponse的认识

    我们知道,在网络Http通信中.一定会有一个Request.相同的,也一定会有一个Response.而我们在Volley中利用RequestQueue来加入请求之前,一定会先创建一个Request对象 ...

  8. 关于cook操作

    http://www.cnblogs.com/fishtreeyu/archive/2011/10/06/2200280.html

  9. Gym 100952 B. New Job

    B. New Job time limit per test 1 second memory limit per test 64 megabytes input standard input outp ...

  10. POJ 1279 Art Gallery 半平面交/多边形求核

    http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...