简体   繁体   English

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

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

I have this css: 我有这个CSS:

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

and I have this jquery 我有这个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);
        });
    });

I have used values for top: initial,unset and auto, but it doesn't clear the top value of the element and travel to bottom again when mouse leaves. 我已经使用了top的值:initial,unset和auto,但是它不会清除元素的最高值,并且在鼠标离开时不会再次移至底部。

You can reset the top value through removing the style attribute values using .removeAttr() method. 您可以使用.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');
    });

});

DEMO 演示

I think, you should store the initial top value of the element in a variable. 我认为,您应该将元素的初始最大值存储在变量中。 So, when the mouseleave event triggers, you can add it to your element. 因此,当mouseleave事件触发时,可以将其添加到元素中。

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

and in your code: 并在您的代码中:

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