最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Animate height on hover toggle - Stack Overflow

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论