开发者

Toggle divs open and close - ensure only one div visible at any time, not all

I am using the below script to toggle open and close divs on click:

(function ($) {
    $.fn.showHide = function (options) {

    //default vars for the plugin
        var defaults = {
            speed: 1000,
            easing: '',
            changeText: 0,
            showText: 'Show',
            hideText: 'Hide',

        };
        var options = $.extend(defaults, options);

        $(this).click(function () {
             // this var stores which button you've clicked
             var toggleClick = $(this);
             // 开发者_开发技巧this reads the rel attribute of the button to determine which div id to toggle
             var toggleDiv = $(this).attr('rel');
             // here we toggle show/hide the correct div at the right speed and using which easing effect
             $(toggleDiv).slideToggle(options.speed, options.easing, function() {
             // this only fires once the animation is completed
             if(options.changeText==1){
             $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
             }
              });

          return false;

        });

    };
})(jQuery);

What would be the best method to automatically close any currently open divs before opening the next (to prevent all being open on page - only have the clicked link visible)

HTML is below:

<a href="#" class="show_hide" rel="#slidingDiv">View</a><br />
   <div id="slidingDiv" style="display:none;">
       Fill this space with really interesting content.
   </div>

<a href="#" class="show_hide" rel="#slidingDiv_2">View</a><br />
   <div id="slidingDiv_2" style="display:none;">
       Fill this space with really interesting content.
   </div> 

Im guessing just a quick check before the open is fired - but Im unable to thus far get it to work.

Many thanks in advance!


Add a slidingDivs class to all of the sliding divs and simply call:

$(".slidingDivs").hide();

I don't think its worth worrying about only hiding the visible ones, just hide them all.


This may be useful

 $(toggleDiv).siblings(".slidingDivs").hide();
 $(toggleDiv).toggle();

where toggle div is defined in your code as

var toggleDiv = $(this).attr('rel');

and just add class "slidingDivs" to divs with id "slidingDiv" and "slidingDiv_2"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜