【问题标题】:How do I decrease a height of a div onclick? [duplicate]如何减少 div onclick 的高度? [复制]
【发布时间】:2013-06-24 20:10:11
【问题描述】:

我正在使用以下方法来增加点击时 div 的高度,但如果再次点击链接,我需要将高度恢复为原始高度。

代码:

$(document).ready(function() {
    $('.anchor_clicker').click(function(){
    $('#increaseth_the_height').animate({height:'930'})
})
    });

【问题讨论】:

标签: javascript jquery onclick jquery-animate


【解决方案1】:

尝试将其存储在元素上并基于此进行切换。这假设您只有 1 个带有 .anchor_clicker 类的元素:

$(document).ready(function() {
  $('.anchor_clicker').click(function(){
    if( $('.anchor_clicker').data('stored-height') == 930 ) {
      $('.anchor_clicker').data('stored-height','100');
      $('#increaseth_the_height').animate({height:'100'})
    } else {
      $('.anchor_clicker').data('stored-height','930');
      $('#increaseth_the_height').animate({height:'930'})
    }
  })
});

【讨论】:

    【解决方案2】:

    类似这样的:

    var isExpanded=false;
    $(document).ready(function() {
        $('.anchor_clicker').click(function(){
        if(isExpanded==false)
        {
             $('#increaseth_the_height').animate({height:'930'})
             isExpanded=true
        } else
        {
             $('#increaseth_the_height').animate({height:'ORIGANAL'})
             isExpanded=false
        }
    })
        });
    

    【讨论】:

      【解决方案3】:

      一种方法是记住“点击”状态并执行if 来确定是缩小还是扩大 div...

      $(document).ready(function() {
          var clicked = 0;
          $('.anchor_clicker').click(function(){
              if(clicked === 0){
                   $('#increaseth_the_height').animate({height:'930'})
                   clicked = 1;
              }
              else {
                  $('#increaseth_the_height').animate({height:'300'}) //or whatever the orig height was
                  clicked = 0;   
              }
          })
      });
      

      【讨论】:

        【解决方案4】:

        我将添加一个,只是为了在那里有效率参数:

        $(function(){
            function animateHeight($item,ht,spd){
                $item.stop().animate({height:ht},spd);
            }
        
            $("#topbar-show").click(function(){
                $(this).toggle(function(){
                    animateHeight($(this),40,200);
                },function(){
                    animateHeight($(this),10,200);
                });
            });
        });
        

        我添加了.stop() 方法来防止动画排队,并从动画中制作了一个函数,可以灵活地在任意数量的对象上使用动画。

        如果您不喜欢 .toggle() 函数,您可以随时使用类来代替:

        $(function(){
            function animateHeight($item,ht,spd){
                $item.stop().animate({height:ht},spd);
            }
        
            $("#topbar-show").click(function(){
                if($(this).hasClass('clicked')){            
                    $(this).removeClass('clicked');
                    animateHeight($(this),10,200);
                } else {                  
                    $(this).addClass('clicked');      
                    animateHeight($(this),40,200);
                }
            });
        });
        

        我个人更喜欢类方法,但每个人都有自己的方法。

        【讨论】:

        • 从 1.8 开始不推荐使用这种调用 toggle() 的方式,并且在 1.9+ 中不存在 See here
        • 是的,我不知道他使用的是哪个版本。我添加了基于类的方法,我也明确表示我更喜欢这种方法。
        猜你喜欢
        • 2018-05-23
        • 1970-01-01
        • 2017-11-01
        • 1970-01-01
        • 2013-03-06
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多