简体   繁体   English

悬停切换时的jQuery Animate高度

[英]jQuery Animate height on hover toggle

Ok i must be missing something stupid here but i am lost to why this wont work 好吧,我一定在这里想念一些愚蠢的东西,但是我迷失了为什么这行不通

    $('#fleet').hover(function(){
        var fleet = '2';
        $("#fleetHover").show();
        $("#fleet").animate({ "height": '+=' + (fleet * 30) + 'px' }, "slow");
    }, function(){
        $("#fleetHover").hide();
          $("#fleet").animate({ "height": '-=' + (fleet * 30) + 'px' }, "slow");
    });

When i hover the element it expands the height that i need. 当我将元素悬停时,它会扩展所需的高度。 But when i mouse out it doesnt go back. 但是,当我将鼠标移出时,它不会返回。

But if i do this with the height as a value and not a variable it works. 但是如果我用高度作为值而不是变量来执行此操作。

    $('#fleet').hover(function(){
        var fleet = '2';
        $("#fleetHover").show();
        $("#fleet").animate({ "height": '+=' + (fleet * 30) + 'px' }, "slow");
    }, function(){
        $("#fleetHover").hide();
          $("#fleet").animate({ "height": "-=60px" }, "slow");
    });

What am i missing here? 我在这里想念什么? I am slowly learning the JS/jQuery stuff can a variable not be used more than once? 我正在慢慢学习JS / jQuery的东西,变量不能多次使用吗?

Any help would be great! 任何帮助将是巨大的!

Your fleet variable is defined inside of and scoped to the first anonymous function. 您的fleet变量在第一个匿名函数的内部定义并限定范围。 It isn't accessible to the second anonymous function. 第二个匿名函数无法访问它。 One solution would be to define the variable outside of both functions: 一种解决方案是在两个函数之外定义变量:

var fleet = '2';
$('#fleet').hover(function(){        
    $("#fleetHover").show();
    $("#fleet").animate({ "height": '+=' + (fleet * 30) + 'px' }, "slow");
}, function(){
    $("#fleetHover").hide();
    $("#fleet").animate({ "height": '-=' + (fleet * 30) + 'px' }, "slow");
});

I will remove this answer if you don't want this option. 如果您不希望使用此选项,我将删除此答案。

Just use CSS3 animation with CSS hover states. 只需在CSS悬停状态下使用CSS3动画即可。

.grow {
  height: 140px;
  width: 40px;
  position: absolute;
  text-align: center;
  bottom: 0;
  left: 100px;
  background: mediumSeaGreen;
  -webkit-transition: all .3s ease;
  -moz-transition: all .3s ease;
  -ms-transition: all .3s ease;
  -o-transition: all .3s ease;
}

.grow:hover {
  height: 200px;
}

jsfiddle jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM