JS判断鼠标从哪个方向进入DIV容器
写的不够高大上 , 不要介意哦。。。
Js:
//进去
$(".flash").bind("mouseenter",function(e){ /** the width and height of the current div **/
var w = $(this).width();
var h = $(this).height(); /** calculate the x and y to get an angle to the center of the div from that x and y. **/
/** gets the x value relative to the center of the DIV and "normalize" it **/
var x = (e.pageX - this.offsetLeft - (w/)) * ( w > h ? (h/w) : );
var y = (e.pageY - this.offsetTop - (h/)) * ( h > w ? (w/h) : ); /** the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);**/
/** first calculate the angle of the point,
add 180 deg to get rid of the negative values
divide by 90 to get the quadrant
add 3 and do a modulo by 4 to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/
var direction = Math.round((((Math.atan2(y, x) * ( / Math.PI)) + ) / ) + ) % ; /** do your animations here **/
switch(direction) {
case :
$('.showD').css({
'top':'-200px',
'left':'',
'opacity':'',
}).stop().animate({
'top':'',
'left':'',
'opacity':'',
},)
/** animations from the TOP **/
break;
case :
$('.showD').css({
'top':'',
'left':'200px',
'opacity':'',
}).stop().animate({
'top':'',
'left':'',
'opacity':'',
},)
/** animations from the RIGHT **/
break;
case :
$('.showD').css({
'top':'200px',
'left':'',
'opacity':'',
}).stop().animate({
'left':'',
'top':'',
'opacity':'',
},)
/** animations from the BOTTOM **/
break;
case :
$('.showD').css({
'top':'',
'left':'-200px',
'opacity':'',
}).stop().animate({
'left':'',
'right':'',
'opacity':'',
},)
/** animations from the LEFT **/
break; }}); //出来
$(".flash").bind("mouseleave",function(e){ /** the width and height of the current div **/
var w = $(this).width();
var h = $(this).height(); /** calculate the x and y to get an angle to the center of the div from that x and y. **/
/** gets the x value relative to the center of the DIV and "normalize" it **/
var x = (e.pageX - this.offsetLeft - (w/)) * ( w > h ? (h/w) : );
var y = (e.pageY - this.offsetTop - (h/)) * ( h > w ? (w/h) : ); /** the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);**/
/** first calculate the angle of the point,
add 180 deg to get rid of the negative values
divide by 90 to get the quadrant
add 3 and do a modulo by 4 to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/
var direction = Math.round((((Math.atan2(y, x) * ( / Math.PI)) + ) / ) + ) % ; /** do your animations here **/
switch(direction) {
case :
$('.showD').css({
'right':'',
}).stop().animate({
'right':'',
'top':'-200px',
},)
/** animations from the TOP **/
break;
case :
$('.showD').css({
'top':'',
}).stop().animate({
'top':'',
'left':'200px',
},)
/** animations from the RIGHT **/
break;
case :
$('.showD').css({
'top':'',
'left':'',
}).stop().animate({
'left':'',
'top':'200px',
},)
/** animations from the BOTTOM **/
break;
case :
$('.showD').css({
'top':'',
'left':'',
}).stop().animate({
'left':'-200px',
'right':'',
},)
/** animations from the LEFT **/
break; }});
HTML:
<div class="flash">
<img class="pic_bg" src="http://img0.imgtn.bdimg.com/it/u=261025820,3584077840&fm=21&gp=0.jpg" style="width:100%;height:100%"/>
<div class="showD">
</div>
</div>
Css:
.flash{
width:200px;
height:200px;
margin-left:%;
background-color:red;
position:relative;
overflow:hidden;
}
.showD{
background: #469acb;
position: absolute;
width:%;
height:200px;
}
JS判断鼠标从哪个方向进入DIV容器的更多相关文章
- JS判断鼠标从什么方向进入一个容器
偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题.首先想到的是给容器的四个边添加几个块,然后看鼠标进入的时候哪个块先监听到鼠标事件.不过这样麻烦太多了.google了一下找到了一个不错的解决方 ...
- js判断鼠标位置是否在某个div中
div的onmouseout事件让div消失时,会出现这样的情况,就是当鼠标移至div中的其它内容时,此时也判定为离开div,会触发 onmouseout事件,这样div中的内容就不能操作了.解决的办 ...
- js判断鼠标滑轮滚动方向并根据滚动的方向触发不同的事件
<script> var scrollFunc = function (e) { var direct = 0; e = e || window.event; if (e.wheelDel ...
- JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题
1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...
- JS 判断鼠标滚轮的上下滚动
JS 判断鼠标滚轮的上下滚动 <script type="text/javascript"> var scrollFunc = function (e) { e = ...
- JS判断鼠标移入元素的方向
最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...
- 关于js判断鼠标移入元素的方向--解释
一开始我是这么想的,将待移入的元素分割四块,用mousemove获取第一次鼠标落入的区域来判断鼠标是从哪个方向进去的. 所以只要写个算法来判断鼠标的值落入该元素的区域就可以得出鼠标移入的方向,如下图: ...
- 关于js判断鼠标移入元素的方向——上下左右
一开始我是这么想的,将待移入的元素分割四块,用mousemove获取第一次鼠标落入的区域来判断鼠标是从哪个方向进去的. 所以只要写个算法来判断鼠标的值落入该元素的区域就可以得出鼠标移入的方向,如下图: ...
- 2015.10.11(js判断鼠标进入容器的方向)
判断鼠标进入容器的方向 1.前几天在万圣节专题项目中用到了鼠标坐标page事件,随着鼠标背景图片移动形成有层次感的效果,但page事件在IE低版本不支持,所以还要做兼容.在研究page事件同时无意中想 ...
随机推荐
- [Javascript] Promise
Promise 代表着一个异步操作,这个异步操作现在尚未完成,但在将来某刻会被完成. Promise 有三种状态 pending : 初始的状态,尚未知道结果 fulfilled : 代表操作成功 r ...
- haffman树c实现
#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 100#define M 2*N-1t ...
- 《algorithm puzzles》——谜题
这篇文章开始正式<algorithm puzzles>一书中的解谜之旅了! 狼羊菜过河: 谜题:一个人在河边,带着一匹狼.一只羊.一颗卷心菜.他需要用船将这三样东西运至对岸,然而,这艘船空 ...
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- Freemarker数字转时间
使用freemarker模板,展示一个时间字段,数据库保存的是毫秒.在网上找了许多文章,发现都是针对date或者是直接类似"1999-09-09"这样已经成型字符串进行操作的,心中 ...
- 什么是mata标签
<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任何内容.&l ...
- evernote出现“Sync failed due to unexpected problem at server side”的问题
继上次的"Invalid username and/or password"问题之后,evernote又出现了“Sync failed due to unexpected prob ...
- POJ 2431 Expedition (STL 优先权队列)
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8053 Accepted: 2359 Descri ...
- 初识AM335X
TI 的AM335X,linux 操作系统,全都是陌生的东东,一点一点来熟悉吧. 拿的TI代理的一开发板,直接看文档.环境由于之前一同事已经装好了,公司条件受限,没法让我们一人一台ubuntu的机子来 ...
- ubuntu sublime安装及配置
安装sublime-text-2: sudo add-apt-repository ppa:webupd8team/sublime-text-2 sudo apt-get update sudo ap ...