jQuery实现百度图片移入移出内容提示框上下左右移动的效果

2020-05-23 06:21:00易采站长站整理

2. css


*{
padding:0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
background: skyblue;
float: left;
position: relative;
overflow: hidden;
margin:10px 10px 0 0;
}
.innerBox{
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 40px;
}
img{
width: 100%;
}
.inner{
position: absolute;
top:40px;
left: 0;
width: 100%;
height: 40px;
background: red;
}

3. js


$('.box').hover(function(e){
getIn($(this),e)
},function(e){
getOut($(this),e)
})
//获取在第几区域
function getdirection(obj,e){
var bleft=obj.offset().left;//距离左部的大小
var btop=obj.offset().top;//距离顶部的大小
var li_w=obj.width();//每个图片的宽度
var li_h=obj.height();//每个图片的高度
var evt=e||window.event;
var x=evt.pageX-bleft;//鼠标在该图片中的x坐标
var y=evt.pageY-btop;//鼠标在该图片中的y坐标
x=Math.abs(x);//这里是防止移出的时候,x的值为负(bleft的值大于pageX)
y=Math.abs(y);//与上同理
if(x>li_w){
x=li_w-(x-li_w);//这里是防止在第四部分移出的时候,pageX的值大于图片的长度,所以需要用到长度减去多余的部分就是在第四区域的对称位置
}
var Alltan=Math.atan(li_h/li_w);//这是α
var leftTan=Math.atan(y/x);//这是β
var rightTan=Math.atan(y/(li_w-x));//这是θ
if(0<=leftTan&&leftTan<=Alltan&&0<=rightTan&&rightTan<=Alltan){
console.log("在第一部分")
return 1;
}else if(Alltan<=leftTan&&leftTan<=Math.asin(1)&&0<=rightTan&&rightTan<=Alltan){
console.log("在第二部分");
return 2;
}else if(Alltan<=leftTan&&leftTan<=Math.asin(1)&&Alltan<=rightTan&&rightTan<=Math.asin(1)){
console.log("在第三部分");
return 3;
}else if(0<=leftTan&&leftTan<=Alltan&&Alltan<=rightTan&&rightTan<=Math.asin(1)){
console.log("在第四部分");
return 4;
}
}
//移入
function getIn(obj,e){
var statu=getdirection(obj,e);
var li_w=obj.width();
var that=obj.find('.inner');
var child_h=that.height();
if(statu===1){
that.css({
"left":0,
"top":-child_h
}).stop().animate({
"top":0
},200)
}else if(statu===2){
that.css({
"left":-li_w,
"top":0
}).stop().animate({
"left":0
},200)
}else if(statu===3){
that.stop().animate({
"top":0
},200)
}else if(statu===4){
that.css({
"left":li_w,
"top":0
}).stop().animate({
"left":0