javascript - jQuery Animate height on hover toggle - Stack Overflow

Ok i must be missing something stupid here but i am lost to why this wont work $('#fleet').ho

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?

Any help would be great!

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?

Any help would be great!

Share Improve this question asked Oct 2, 2013 at 18:09 TravisTravis 2,2457 gold badges29 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Your fleet variable is defined inside of and scoped to the first anonymous function. 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.

.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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745406057a4626319.html

相关推荐

  • javascript - jQuery Animate height on hover toggle - Stack Overflow

    Ok i must be missing something stupid here but i am lost to why this wont work $('#fleet').ho

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信