Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jQuery 2.0中切换动画?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jQuery 2.0中切换动画?

Javascript 如何在jQuery 2.0中切换动画?,javascript,jquery,Javascript,Jquery,我想使用jQuery事件来切换动画(展开/折叠),问题是我使用的是jQuery 2.0,其中的切换在jQuery 1.8中被弃用,在jQuery 1.9+中被删除 是否有一种方法可用于此动画的切换 $("#expand").on("click", function(){ var el = $("#text"), curHeight = el.height(), autoHeight = el.css('height', 'auto').height();

我想使用jQuery事件来切换动画(展开/折叠),问题是我使用的是jQuery 2.0,其中的切换在jQuery 1.8中被弃用,在jQuery 1.9+中被删除

是否有一种方法可用于此动画的切换

$("#expand").on("click", function(){
        var el = $("#text"), curHeight = el.height(), 
        autoHeight = el.css('height', 'auto').height();
        el.height(curHeight).animate({height: autoHeight}, 1000);
});

您可以切换一个类,例如打开的
来重新表示控件的当前状态,如果元素有或没有该类,则相应地更改代码

代码:


演示:

您可以使用
.slideToggle()
代替:


给你,记住一个状态和初始高度

check=false;
bla=false;
$("#expand").on("click", function(){
    if(!check){
        check=true;
        var el = $("#text"), curHeight = el.height(),
        autoHeight = el.css('height', 'auto').height();
        bla=curHeight;
        el.height(curHeight).animate({height: autoHeight}, 1000);
    }else{
        var el = $("#text");
        el.animate({height: bla}, 1000);
        check=false;
    }
});

您可以使用HTML5的数据API来完成点击切换。这只会在每次单击结束时切换标志,并根据div当前是否打开设置动画到一个高度:

$("#expand").data("open",false).on("click", function(){
    var $this = $(this), flag = $this.data("open");
    var el = $("#text"), curHeight = el.height(), 
    autoHeight = el.css('height', 'auto').height();
    el.height(curHeight).animate({height: flag?24:autoHeight}, 1000);
    $this.data("open",!flag);
});

始终可以使用带有标志和if语句的单击事件来100%模拟的功能。但是,使用两个事件进行切换可能有一种更简单的方法来实现您的目标。很高兴能够提供帮助。我可以建议在动画之前添加一个
.stop(true,false)
(:
check=false;
bla=false;
$("#expand").on("click", function(){
    if(!check){
        check=true;
        var el = $("#text"), curHeight = el.height(),
        autoHeight = el.css('height', 'auto').height();
        bla=curHeight;
        el.height(curHeight).animate({height: autoHeight}, 1000);
    }else{
        var el = $("#text");
        el.animate({height: bla}, 1000);
        check=false;
    }
});
$("#expand").data("open",false).on("click", function(){
    var $this = $(this), flag = $this.data("open");
    var el = $("#text"), curHeight = el.height(), 
    autoHeight = el.css('height', 'auto').height();
    el.height(curHeight).animate({height: flag?24:autoHeight}, 1000);
    $this.data("open",!flag);
});