jquery animate line height - possible?

2020-04-16 18:07发布

问题:

quick question about the the animate property in jquery, Is it possible to animate

I cant figure out how? here is my html

<nav>
  <a href="">Page 1</a>
  <a href="">Page 2</a>
  <a href="">Page 3</a>
  <a href="">Page 4</a>
  <a href="">Page 5</a>
  <a href="">Page 6</a>
</nav>
<div style="height:9000px;"></div>

and javascript:

$(function(){
    $('nav').data('size','big');
});

$(window).scroll(function(){
    var $nav = $('nav');
    var $a = $('nav > a');
    if ($('body').scrollTop() > 0) {
        if ($nav.data('size') == 'big') {
            $nav.data('size','small').stop().animate({
                height:'40px',
                line-height:'40px'
            }, 300);
            $a.data('size','small').stop().animate({
                height:'20px'
            }, 300);
        }
    } else {
        if ($nav.data('size') == 'small') {
            $nav.data('size','big').stop().animate({
                height:'100px',
                line-height:'40px'
            }, 300);
            $a.data('size','big').stop().animate({
                height:'40px'
            }, 300);


        }  
    }
});

Also is there a way to get the animation for the nav and the a tags to be in sync.

Thanks

ps sorry if its a really basic question - im new to jquery

http://jsfiddle.net/jamesmstone/c7nLB/32/

回答1:

Here you go, it was your line-height in animate function it should have been 'line-height'

$(function(){
   $('nav').data('size','big');
});

$(window).scroll(function(){
    var $nav = $('nav');
    var $a = $('nav > a');
    if ($('body').scrollTop() > 0) {
        if ($nav.data('size') == 'big') {
            $nav.data('size','small').stop().animate({
                height:'40px',
                'line-height':'40px'
            }, 300);
            $a.data('size','small').stop().animate({
                height:'20px'
            }, 300);
        }
    } else {
        if ($nav.data('size') == 'small') {
            $nav.data('size','big').stop().animate({
                height:'100px',
                'line-height':'40px'
            }, 300);
            $a.data('size','big').stop().animate({
                height:'40px'
            }, 300);


        }  
    }
});