簡體   English   中英

清除jquery.animate()中元素的最高值

[英]Clear top value of an element in jquery.animate()

我有這個CSS:

.hacer .contenido .caja .texto {
    position: absolute;
    bottom: -30px;
}

我有這個jQuery的

 var cajasHacer = $('.hacer').find('.contenido').find('.caja');

        cajasHacer.each(function () {

        $(this).mouseenter(function() {
            $(this).find('.texto').animate({top: '40px'} , 300);
        });

        $(this).mouseleave(function() {
            $(this).find('.texto').animate({ top: 'auto'} , 300);
        });
    });

我已經使用了top的值:initial,unset和auto,但是它不會清除元素的最高值,並且在鼠標離開時不會再次移至底部。

您可以使用.removeAttr()方法通過刪除樣式屬性值來重置頂部值。

var cajasHacer = $('.hacer').find('.contenido').find('.caja');

    cajasHacer.each(function () {

    $(this).mouseenter(function() {
        $(this).find('.texto').animate({top: '40px'} , 300);
    });

    $(this).mouseleave(function() {
        $(this).find('.texto').removeAttr('style');
    });

});

演示

我認為,您應該將元素的初始最大值存儲在變量中。 因此,當mouseleave事件觸發時,可以將其添加到元素中。

var topPos = $(element).offset().top;

並在您的代碼中:

var cajasHacer = $('.hacer').find('.contenido').find('.caja');

        cajasHacer.each(function () {

        $(this).mouseenter(function() {
            $(this).find('.texto').animate({top: '40px'} , 300);
        });

        $(this).mouseleave(function() {
            $(this).find('.texto').animate({ top: topPos} , 300);
        });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM