js 实现遮罩层随鼠标移动的方向移入而移出

DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .li {
            width: 200px;
            height: 200px;
            background: #00C475;
            border-radius: 10px;
            margin: 10px auto;
            position: relative;
            overflow: hidden;
            margin: 50px;
        }

        .li article {
            width: 100%;
            height: 100%;
            position: absolute;
            background: red;
            z-index: 1;
        }
    style>
head>

<body>
    <div class="li">
        <article>1article>
    div>
    <div class="li">
        <article>2article>
    div>
    <div class="li">
        <article>3article>
    div>
    <div class="li">
        <article>4article>
    div>
    <div class="li">
        <article>5article>
    div>
    <div class="li">
        <article>6article>
    div>
    <div class="li">
        <article>7article>
    div>

body>
<script src="js/jquery-1.10.2.js">script>
<script>
    
    (function ($) {
        "use strict"
        var Str = {
            tmhover: null,
            hoverDelay: 0,
            reverse: false,
            obj:{},
            init: async function (options) {
                await this.onStyle();
                Str.obj = options;
                this.onEvent(Str.obj.id);
            },
            getDir: function (jQueryel, coordinates) {
                var w = jQueryel.width(),
                    h = jQueryel.height(),
                    x = (coordinates.x - jQueryel.offset().left - (w / 2)) * (w > h ? (h / w) : 1),
                    y = (coordinates.y - jQueryel.offset().top - (h / 2)) * (h > w ? (w / h) : 1),
                    direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;
                return direction;
            },
            getClasses: function (direction) {
                var fromClass, toClass;
                switch (direction) {
                    case 0:
                        (!Str.reverse) ? fromClass = 'da-slideFromTop' : fromClass = 'da-slideFromBottom';
                        toClass = 'da-slideTop';
                        break;
                    case 1:
                        (!Str.reverse) ? fromClass = 'da-slideFromRight' : fromClass = 'da-slideFromLeft';
                        toClass = 'da-slideLeft';
                        break;
                    case 2:
                        (!Str.reverse) ? fromClass = 'da-slideFromBottom' : fromClass = 'da-slideFromTop';
                        toClass = 'da-slideTop';
                        break;
                    case 3:
                        (!Str.reverse) ? fromClass = 'da-slideFromLeft' : fromClass = 'da-slideFromRight';
                        toClass = 'da-slideLeft';
                        break;
                };
                return { from: fromClass, to: toClass };
            },
            onEvent: function (element) {
                $(element).on('mouseenter.hoverdir, mouseleave.hoverdir', function (event) {
                    var jQueryel = $(this),
                        evType = event.type,
                        jQueryhoverElem = jQueryel.find(Str.obj.dom),
                        direction = Str.getDir(jQueryel, { x: event.pageX, y: event.pageY }),
                        hoverClasses = Str.getClasses(direction);
                    jQueryhoverElem.removeClass();
                    if (evType === 'mouseenter') {
                        jQueryhoverElem.hide().addClass(hoverClasses.from);
                        clearTimeout(Str.tmhover);
                        Str.tmhover = setTimeout(function () {
                            jQueryhoverElem.show(0, function () {
                                jQuery(this).addClass('da-animate').addClass(hoverClasses.to);
                            });
                        }, Str.hoverDelay);
                    }
                    else {
                        jQueryhoverElem.addClass('da-animate');
                        clearTimeout(Str.tmhover);
                        jQueryhoverElem.addClass(hoverClasses.from);

                    }
                });
            },
            onStyle: function () {
                var html =
                    ``;
                    document.head.insertAdjacentHTML('beforeend', html);
            }
        };
        $.fn.onInit = function (element) {
            let ele = element;
            $(element.dom).addClass('da-slideFromRight');
            Str.init(element);
        }
    })(jQuery);

    jQuery().onInit({
        'id':'.li',
        'dom':'article'
    });

script>

html>

你可能感兴趣的:(JavaScript,jq,前端,javascript,开发语言,ecmascript)