jQuery如何实现滑入滑出效果(跟随鼠标滑入滑出蒙板)

效果图:


代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
            list-style: none
        }

        #box{
            position:relative;
            margin: 100px auto;
            width:300px;
            height:400px;
            background-image:url(img/timg1.jpg);
            background-size:300px 400px;
            border:1px solid black;
            overflow: hidden;
        }
        #zzc{
            position: absolute;
            width: 300px;
            height: 400px;
            background-color: red;
            opacity:0.4;
            left:-20000px;
        }
    </style>
</head>
<body style="height:1000px; width:1000px;">

    <div id="box">
        <div id="zzc"></div>
    </div>

</body>
</html>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
function inDirection(evt) {
    //1、拿出鼠标距离四个边的距离
    //1)、盒子left,top,right,bottom
    let left= $("#box").offset().left;
    let top = $("#box").offset().top;
    let right = left+$("#box").width();
    let bottom = top+$("#box").height();

    //2)、计算鼠标距离每个边的距离
    let leftDis =Math.abs(left-evt.pageX);
    let rightDis =Math.abs(right-evt.pageX);
    let topDis =Math.abs(top-evt.pageY);
    let bottomDis =Math.abs(bottom-evt.pageY);

    let obj = {
        "左":leftDis,
        "上":topDis,
        "右":rightDis,
        "下":bottomDis
    }
    let min = obj["左"];
    let minKey = "左";
    for(let key in obj){
        if(obj[key]<min){
            min = obj[key];
            minKey = key;
        }
    }
    return minKey;
}

function leftIn() {
    $("#zzc").css({"left":-300});
    $("#zzc").animate({"left":0,"top":0},200);
}

function rightIn() {
    $("#zzc").css({"left":300});
    $("#zzc").animate({"left":0,"top":0},200);
}

function upIn() {
    $("#zzc").css({"top":-400});
    $("#zzc").animate({"left":0,"top":0},200);
}

function bottomIn() {
    $("#zzc").css({"top":400});
    $("#zzc").animate({"left":0,"top":0},200);
}

function leftOut() {
    $("#zzc").animate({"left":-300,"top":0},200);
}

function rightOut() {
    $("#zzc").animate({"left":300,"top":0},200);
}

function upOut() {
    $("#zzc").animate({"left":0,"top":-400},200);
}

function bottomOut() {
    $("#zzc").animate({"left":0,"top":400},200);
}

let inFuncObj ={
    "左":leftIn,
    "上":upIn,
    "右":rightIn,
    "下":bottomIn
};

let outFuncObj ={
    "左":leftOut,
    "上":upOut,
    "右":rightOut,
    "下":bottomOut
};

$(function () {
    $("#box").mouseenter(function (evt) {
        let direction = inDirection(evt);
        console.log("从"+direction+"边进入");
        inFuncObj[direction]();
    });

    $("#box").mouseleave(function (evt) {
        let direction = inDirection(evt);
        console.log("从"+direction+"边出去");
        outFuncObj[direction]();
    });
});

</script>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值