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事件同时无意中想 ...
随机推荐
- 【贪心】Codeforces 704B & 705D Ant Man
题目链接: http://codeforces.com/problemset/problem/704/B 题目大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的 ...
- Sort List ——LeetCode
Sort a linked list in O(n log n) time using constant space complexity. 链表排序,要求时间复杂度O(nlgn),我写的归并排序. ...
- HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 看到这个时间,我懵逼了... 果然,J ...
- bzoj2124 等差子序列(hash+线段树)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 719 Solved: 261[Submit][Status][Discuss] ...
- Fire Net(深搜 和一前不一样的深搜)
/* http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1162 本题妙处: 用一个数对行取商是 ...
- [转]浏览器如何和Web服务器通信
http://hi.baidu.com/ywqme/item/b5297014b2e58f4e6826bb74 概述 普通网民打开网页,访问网站,并不需要了解所谓HTTP协议.作为软件工程师,了解一下 ...
- 意大利进口的衬衫面料pH值严重超标·都市快报
意大利进口的衬衫面料pH值严重超标·都市快报 意大利进口的衬衫面料pH值严重超标 董捷 2007-03-24 通讯员 浙 检 记 者 董 捷 ...
- angularJS服务一
一 认识服务 1.服务这个概念其实并不陌生,在其他语言中如java便有这样的概念,其作用就是对外提供某个特定的功能,如消息服务,文件压缩服务等,是一个独立的模块.ng的服务是一个单例对象或函数,对外提 ...
- SCOI2014 方伯伯的OJ onlinejudge
终于用自己的方法水过去了. 本地测慢的一组要三四秒,一共要十几秒,BZOJ貌似一共只让跑6s,于是就还T着的. 一开始没看n<=1e8,想的直接splay+map(splay维护名次,map维护 ...
- Android项目实战--手机卫士20--拿到已经安装了的程序以及程序管理主界面
好了,之前我们就讲了高级工具里面的短信备份与还原,那么我们高级工具里面的功能就基本上完成的啦,还有一个叫程序锁的功能而已,但我们今天先不做它先,我们先把我们的程序管理这个功能完成先. 先让大家看一下我 ...