简体   繁体   English

带有多个slideToggle的jQuery

[英]jQuery with multiple slideToggle

I have a few forms that are initially hidden on a page. 我有一些最初隐藏在页面上的表格。 When a user clicks on a specific link on the page, the corresponding form shows using the jQuery slideToggle. 当用户单击页面上的特定链接时,将使用jQuery slideToggle显示相应的表单。 The way that I am doing it right now seems convaluted and there has to be a more succinct way of doing it. 我现在的操作方式似乎很有价值,并且必须有一种更为简洁的方式。 Can anyone help me be more efficient in doing this, ie, less code, best practice, etc.? 有人可以帮助我更有效地做到这一点,例如减少代码,最佳实践等吗?

    // Show & Hide the forms on the "We need your help" page
        jQuery('.contribute-form').hide();
        jQuery('.translate-form').hide();
        // Contribute Form
        jQuery('.contribute').on('click', function(){
            if(jQuery('.translate-form').css('display', 'block')){
                jQuery('.translate-form').slideToggle('slow');
                jQuery('.contribute-form').slideToggle('slow');
            } else if(jQuery('.donate-form').css('display', 'block')){
                jQuery('.donation-form').slideToggle('slow');
                jQuery('.contribute-form').slideToggle('slow');
            } else {
                jQuery('.contribute-form').slideToggle('slow');
            }
        });
        // Translate Form
        jQuery('.translate').on('click', function(){
            if(jQuery('.donate-form').css('display', 'block')){
                jQuery('.donate-form').slideToggle('slow');
                jQuery('.translate-form').slideToggle('slow');
            } else if(jQuery('.contribute-form').css('display', 'block')){
                jQuery('.contribute-form').slideToggle('slow');
                jQuery('.translate-form').slideToggle('slow');
            } else {
                jQuery('.translate-form').slideToggle('slow');
            }
        });
       // Donate Form
        jQuery('.donate').on('click', function(){
            if(jQuery('.translate-form').css('display', 'block')){
                jQuery('.translate-form').slideToggle('slow');
                jQuery('.donate-form').slideToggle('slow');
            } else if(jQuery('.contribute-form').css('display', 'block')){
                jQuery('.contribute-form').slideToggle('slow');
                jQuery('.donate-form').slideToggle('slow');
            } else {
                jQuery('.donate-form').slideToggle('slow');
            }
        });

First, use the dollar alias for cleaner code. 首先,将美元别名用于更清洁的代码。 Then, combine selectors in single statements. 然后,在单个语句中组合选择器。 Finally, use the is() method with :visible rather than fiddling with CSS. 最后,将is()方法与:visible而不是摆弄CSS。

jQuery(function($) {
    $('.contribute').on('click', function(){
        if ( $('.translate-form').is(':visible') ) {
            $('.translate-form, .contribute-form').slideToggle('slow');
        } else if ( $('.donate-form').is(':visible') ) {
            $('.donation-form, .contribute-form').slideToggle('slow');
        } else {
            $('.contribute-form').slideToggle('slow');
        }
    });
});

I'm sure your logic could be simplified further using classes and DOM traversal. 我确信使用类和DOM遍历可以进一步简化您的逻辑。 If you'd like to put your code in a demo at http://jsfiddle.net we could offer more suggestions. 如果您想将代码放在http://jsfiddle.net的演示中,我们可以提供更多建议。

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

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