繁体   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