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事件同时无意中想 ...
随机推荐
- socket(tcp)互发信息
一:有图有真相,很简单 a, b, Thread 构造函数(ParameterizedThreadStart)初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. 参数star ...
- android webview无法加载网页
主要原因是没有在AndroidManifest.xml里面设置如下: <user-permission android:name="android.permission.INTERNE ...
- [基础] Python问题
1.中文字符被自动转成ASCII码,然后无论如何编解码都没法被某些函数识别,已然放弃,以后坚决不再使用中文路径 2.缺少什么工具就pip install xxx一下,很好用,就是有个下列Warning ...
- MySQL的InnoDB表如何设计主键索引-转自淘宝MySQL经典案例
创建a表 id主键 CREATE TABLE `a` (`id` bigint(20) NOT NULL AUTO_INCREMENT ,`message_id` int(11) NOT NULL,` ...
- Linux Shell编程学习笔记
打算在学习过程中将每个写过的程序一个个的往上贴; 2015-07-03 1. 鸟叔第三版13.2.1节“利用日期进行文件的创建” 源代码 #!/bin/bashPATH=/bin:/sbin:/usr ...
- PHP简单利用token防止表单重复提交(转)
<?php/* * PHP简单利用token防止表单重复提交 */function set_token() { $_SESSION['token'] = md5(microtime(true)) ...
- QT 内存泄露 检测
一:问题出现 最近几天在做一个QT程序,IPX的检测控制程序.需要全天候运行.自己做完了,然后就运行.使用 top|grep TP2 来动态检测程序的CPU,内存占用律.不幸的是,一晚 ...
- [转] 使用CodeViz生成C/C++函数调用关系图
运行环境:虚拟机下的Ubuntu 11.04 结合Graphviz工具,使用CodeViz可以生成直观和漂亮的C/C++程序函数之间的调用关系图. 1.安装graphviz 在安装CodeViz之前, ...
- 网络环境场景以及模拟工具netem
网络环境场景包括: 延迟(Lag),把数据包缓存一段时间后再发出,这样能够模拟网络延迟的状况. 掉包(Drop),随机丢弃一些数据. 节流(Throttle),把一小段时间内的数据拦截下来后再在之后的 ...
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 : bindServ ...