jQuery插件,判断鼠标的移入移出方向
今天用jQuery封装了一个简单的插件,判断鼠标的移入移出方向,以后的项目中可能还会遇到这样一个简单的效果,就记录下来吧!
先看结构和样式:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>判断鼠标移入移出方向</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.outer {
width: 400px;
height: 300px;
border: 2px solid orange;
margin: 100px auto;
overflow: hidden;
position: relative;
}
.outer img{
width: 400px;
height: 300px;
}
.outer div {
position: absolute;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.5;
display: none;
line-height: 300px;
color: white;
}
</style>
</head>
<body>
<div class="outer">
<img src="timg.jpg">
<div class="mask">如果有来生,有没有人爱,我也要努力做一个可爱的人。</div>
</div> <script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery.hover.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
$(".outer").fangxiang(); }) </script>
</body>
</html>
下面是封装的jquery.hover.js
;(function($){
$.fn.fangxiang = function(){
var disL = $(this).offset().left;
var disR = disL + $(this).outerWidth();
var disT = $(this).offset().top;
var disB = disT + $(this).outerHeight();
$(this).hover(function(e){
var dirL = Math.abs(e.clientX-disL);
var dirR = Math.abs(e.clientX-disR);
var dirT = Math.abs(e.clientY-disT);
var dirB = Math.abs(e.clientY-disB);
var dir = Math.min(dirL,dirR,dirT,dirB);
switch(dir){
case dirL:
$(this).find("div").show().css({"left":-$(this).outerWidth(),"top":0}).stop().animate({"left":0},500);
break;
case dirR:
$(this).find("div").show().css({"left":$(this).outerWidth(),"top":0}).stop().animate({"left":0},500);
break;
case dirT:
$(this).find("div").show().css({"top":-$(this).outerHeight(),"left":0}).stop().animate({"top":0},500);
break;
case dirB:
$(this).find("div").show().css({"top":$(this).outerHeight(),"left":0}).stop().animate({"top":0},500);
break;
}
},function(e){
var dirL = Math.abs(e.clientX-disL);
var dirR = Math.abs(e.clientX-disR);
var dirT = Math.abs(e.clientY-disT);
var dirB = Math.abs(e.clientY-disB);
var dir = Math.min(dirL,dirR,dirT,dirB);
switch(dir){
case dirL:
$(this).find("div").stop().animate({"left":-$(this).outerWidth()},500);
break;
case dirR:
$(this).find("div").stop().animate({"left":$(this).outerWidth()},500);
break;
case dirT:
$(this).find("div").stop().animate({"top":-$(this).outerHeight()},500);
break;
case dirB:
$(this).find("div").stop().animate({"top":$(this).outerHeight()},500);
break;
}
})
}
})(jQuery);
jquery.hover.js
感觉写的不是很完美,需要借助我写的这个结构才能使用,请大神帮忙优化!
附上使用的图片timg.jpg

jQuery插件,判断鼠标的移入移出方向的更多相关文章
- jQuery事件-div的显示隐藏及鼠标的移入移出
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 2015.10.11(js判断鼠标进入容器的方向)
判断鼠标进入容器的方向 1.前几天在万圣节专题项目中用到了鼠标坐标page事件,随着鼠标背景图片移动形成有层次感的效果,但page事件在IE低版本不支持,所以还要做兼容.在研究page事件同时无意中想 ...
- jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)
这个插件的名字elevatezoom,网址为http://www.elevateweb.co.uk/image-zoom,在github上的项目首页为https://github.com/elevat ...
- 判断鼠标进入容器的方向小Demo
参考资料: 贤心博客:http://sentsin.com/web/112.html, Math.atan2(y,x) 解释 :http://www.w3school.com.cn/jsref/jsr ...
- JS用斜率判断鼠标进入DIV四个方向的方法 判断鼠标移入方向
本文要介绍的是一种鼠标从一个元素移入移出时,获取鼠标移动方向的思路.这个方法可以帮助你判断鼠标在移入移出时,是从上下左右的哪个方向发生的.这个思路,是我自己琢磨出来,利用了一点曾经高中学过的数学知识, ...
- jquery事件一 ---鼠标移入移出
比较一下几个jquery事件的区别 mouseover() 鼠标进入(进入子元素也触发) mouseout() 鼠标离开(离开子元素也触发) mouseenter() 鼠标进入(进入子元素不触发) m ...
- JS判断鼠标移入元素的方向
最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...
- JS实现穿墙效果(判断鼠标划入划出的方向)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Vue中通过鼠标移入移出来添加或取消class样式(active)
基础知识: 先写一下vue中鼠标移入移出的基础知识,移入的触发事件是 @mouseenter,移出的触发事件是@mouseleave,知道这两个方法就简单了 基础知识的例子 <div clas ...
随机推荐
- Keras & Theano 输出中间层结果
Keras & Theano get output of an intermediate layer 1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要 ...
- Android的onCreateOptionsMenu()创建菜单Menu
android一共有三种形式的菜单: 1.选项菜单(optinosMenu) 2.上下文菜单(ContextMenu) 3.子菜 ...
- 从CVE-2018-1273看漏洞分析
漏洞分析的边界 漏洞分析最应该关注的是漏洞相关的代码,至于其余的代码可以通过关键位置下断点,来理解大概功能. 其中最关键的就是了解数据流,找到离漏洞位置最近的 原始数据 经过的位置,然后开始往下分析, ...
- CVE-2017-17215 - 华为HG532命令注入漏洞分析
前言 前面几天国外有个公司发布了该漏洞的详情.入手的二手 hg532 到货了,分析测试一下. 固件地址:https://ia601506.us.archive.org/22/items/RouterH ...
- sql 单表查询练习
-- 工资高于3000的员工select * from emp where sal > 3000;-- 工资在2500和3000之间的员工select * from emp where sal ...
- 通过ES6实现的Ajax类
个人学习用途而已,仅供参考. class Ajax { constructor(xhr) { xhr = window.XMLHttpRequest ? new XMLHttpRequest() : ...
- LeetCode题解之Number of 1 Bits
1.题目描述 2.问题分析 使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可. 3.代码 int hammingWeight(u ...
- hdfs基本操作-python接口
安装hdfs包 pip install hdfs 查看hdfs目录 [root@hadoop hadoop]# hdfs dfs -ls -R / drwxr-xr-x - root supergro ...
- Entity Framework之DB First方式
EF(Entity Framework的简称,下同)有三种方式,分别是:DataBase First. Model First和Code First. 下面是Db First的方式: 1. 数据库库中 ...
- JAVA入门之基础语言
在上一章文章中,读者已经了解了如何搭建Java 开发环境及Java 程序的开发过程.从本篇文章开始讲解Java 的基本语言.这些基本语言的语法和其他一些编程语言相比有些是类似的,但还有很多不同之处,读 ...