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>
JS实现穿墙效果(判断鼠标划入划出的方向)的更多相关文章
- jQuery鼠标划入划出
今天来简单的谈谈jQuery的一个划入划出的方法,.首先划入划出能想到的东西有哪些呢,. 1:hover 2:mouseenter/mouseleave 3:mouseover/mouseout. 一 ...
- JS用斜率判断鼠标进入DIV四个方向的方法 判断鼠标移入方向
本文要介绍的是一种鼠标从一个元素移入移出时,获取鼠标移动方向的思路.这个方法可以帮助你判断鼠标在移入移出时,是从上下左右的哪个方向发生的.这个思路,是我自己琢磨出来,利用了一点曾经高中学过的数学知识, ...
- CSS动画划入划出酷炫
HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...
- 通信录分组并且分组标签悬停划入划出(包含错误信息及修改)--第三方开源--PinnedSectionListView
PinnedSectionListView在github上的链接地址是:https://github.com/beworker/pinned-section-listview . 下载下来后直接将Pi ...
- NGUI中 鼠标划出屏幕后,停止对 UIDragScrollView 的 press
using UnityEngine; /// <summary> /// NGUI中 鼠标划出屏幕后,停止对 UIDragScrollView 的 press /// </summa ...
- JS判断鼠标移入元素的方向
最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...
- JS判断鼠标从什么方向进入一个容器
偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题.首先想到的是给容器的四个边添加几个块,然后看鼠标进入的时候哪个块先监听到鼠标事件.不过这样麻烦太多了.google了一下找到了一个不错的解决方 ...
- JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题
1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...
- js中判断鼠标滚轮方向的方法
前 言 LiuDaP 最近无聊,在做自己的个人站,其中用到了一个关于鼠标滚轮方向判断的方法,今天闲来无聊,就给大家介绍一下吧!!!! 在介绍鼠标事件案例前,让我们先稍微了解一下js中的event ...
随机推荐
- Webhook
Webhook就是用户通过自定义回调函数的方式来改变Web应用的一种行为,这些回调函数可以由不是该Web应用官方的第三方用户或者开发人员来维护,修改.通过Webhook,你可以自定义一些行为通知到指定 ...
- SFINAE 模板替换失败而非报错的应用
体会这一个例子,检查是否是一个类:P187
- 游戏server之server优化思路
本文仅仅是提供一些游戏server优化思路,当中一些思路是用在不同场合的,不是同个架构的.须要依据应用场景选用合适方式. 本文的引用的文章都是在自己写的在本博客内的.也都是上线开几百个服的成熟项目的. ...
- FFmpeg的HEVC解码器源码简单分析:解码器主干部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法
我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上 ...
- C#结构函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- js闭包注意事项
/关于闭包使用应该注意的 // 1 闭包在父函数每次调用都会产生不同的闭包 // 2 闭包中子函数使用父函数变量的变量不是复制 是引用 // 3 闭包在循环中使用
- Kinect 开发 —— 面部识别
EmguCV库也能用来进行面部识别(face identify).实际的面部识别,就是将一张图像上的人物的脸部识别出来,这是个很复杂的过程,具体过程我们这里不讨论.对一幅影像进行处理来找到包含脸部的那 ...
- hello world! hello cnbog
第一次开通博客,以后见证我的成长吧!
- JS原生方法被覆盖后的恢复办法
alert 被覆盖 今天装修博客园,调试了下JS代码发现 alert() 方法被官方覆盖了,查看源码得知 alert 的功能被替换成了 console.log. 恢复 var _frame = doc ...