Jquery Ajax beforeSend loading animation

Jquery Ajax beforeSend loading animation

本文关键字:animation loading beforeSend Ajax Jquery      更新时间:2023-09-26

大家好,我的 ajax 加载动画有一个问题。问题是当我悬停在任何图像上时,加载动画在所有图像下处于活动状态。

像这个小提琴

我想在悬停第一张图像时制作,然后.ajax-loading仅为该图像激活。 如果我悬停第二张图像,那么.ajax-loading仅对第二张图像扩展处于活动状态。

有人可以在这里帮助我吗?.CSS

.ajax-loading {
    transition: all 0.3s;
    opacity: 0;
    z-index: -1;    
}
.ajax-loading.active {
    opacity: 1;
    z-index: 100;
}

和阿贾克斯

$(document).ready(function () {
    function showProfileTooltip(e, id) {
        //send id & get info from get_profile.php 
        $.ajax({
            url: '/echo/html/',
            data: {
                html: response,
                delay: 0
            },
            method: 'post',
            beforeSend: function() {
                $('.ajax-loading').addClass('active');    
            },
            success: function (returnHtml) {
                e.find('.user-container').empty().html(returnHtml).promise().done(function () {
                    $('.the-container').addClass('loaded');
                });
            }
        }).complete(function() {
            $('.ajax-loading').removeClass('active');    
        });
    }
    function hideProfileTooltip() {
        $('.the-container').removeClass('loaded');
    }
    $('.the-container').hover(function (e) {
        var id = $(this).find('.summary').attr('data-id');
        showProfileTooltip($(this), id);
    }, function () {
        hideProfileTooltip();
    });
});

问题是 beforeSend 函数激活了类active 具有类.ajax-loading的所有元素。

由于您将接收悬停的 jQuery 对象传递给函数,因此您可以利用这一点并将类active仅添加到其下具有类.ajax-loading的元素。

beforeSend: function() {
    $('.ajax-loading',e).addClass('active');// Note the ,e that I added
},

小提琴

希望对您有所帮助。