简体   繁体   English

jQuery切换替代方式?

[英]jQuery toggle alternative way?

Since toggle is deprecated I used this to toogle div: 由于不赞成使用切换功能,因此我使用它来切换div:

$("#syndicates_showmore").on("click", function () {

    if (!clicked) {
        $('#syndicates').fadeTo('slow', 0.3, function () {
            $(this).css(
            {
                'height': 'auto',
                'overflow': 'none'
            });
        }).fadeTo('slow', 1);

        setTimeout(function () {
            $("#syndicates_showmore").text("Show less");
        }, 500);

        clicked = true;
    }
    else {
        $('#syndicates').fadeTo('slow', 0.3, function () {
            $(this).css(
            {
                'height': '290px',
                'overflow': 'hidden'
            });
        }).fadeTo('slow', 1);

        setTimeout(function () {
            $("#syndicates_showmore").text("Show more");
        }, 500);

        clicked = false;
    }
});

Is there any cleaner way to do this? 有没有更清洁的方法可以做到这一点?

According to the jQuery 1.9 Upgrade Guide : 根据jQuery 1.9升级指南

.toggle(function, function, ... ) removed .toggle(function,function,...)已删除

This is the "click an element to run the specified functions" signature of .toggle() . 这是.toggle()的“单击元素以运行指定的功能”签名。 It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. 不应将它与不建议使用的.toggle()的“更改元素的可见性”相混淆。 The former is being removed to reduce confusion and improve the potential for modularity in the library. 删除了前者,以减少混乱并提高库中模块化的可能性。 The jQuery Migrate plugin can be used to restore the functionality. jQuery Migrate插件可用于恢复功能。

In other words, you can still use .toggle like this: 换句话说,您仍然可以像这样使用.toggle

var clicked = false;
$("#syndicates_showmore").on("click", function () {
    clicked = !clicked;
    var showText = "Show " + (clicked ? "more" : "less");
    $('#syndicates').toggle(function () {
        $("#syndicates_showmore").text(showText);
    });
});

The best alternative is to use .toggleClass() : 最好的替代方法是使用.toggleClass()

$("#syndicates_showmore").on("click", function () {
    $('#syndicates').toggleClass("newClass, 1000", "easeInOutBounce")
});

jQuery .toggleClass() API Documentation : jQuery .toggleClass()API文档

Description: Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument. 说明:从匹配元素集中的每个元素中添加或删除一个或多个类,具体取决于类的存在或switch参数的值。

If using with jQuery UI you can easily animate it with using different easing options. 如果使用jQuery UI ,则可以使用不同的缓动选项轻松对其进行动画处理。

Easings: 渐变效果:

Easing functions specify the speed at which an animation progresses at different points within the animation. 缓动功能指定动画在动画内不同点处进行的速度。 jQuery UI provides several additional easing functions, ranging from variations on the swing behavior to customized effects such as bouncing. jQuery UI提供了一些附加的缓动功能,范围从挥杆行为的变化到自定义效果(如弹跳)。

Example online 在线示例

Taken from jQuery API 取自jQuery API

$("#clickme" ).click(function() {
   $( "#book" ).toggle( "slow", function() {
     // Animation complete.
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM