John the Painter
John the Painter

Reputation: 2615

Issue with slideUp/slideDown JS

I have been using this code here:

$(document).ready(function(){

$('a[href*=#]').bind('click', function(){
    var $href = $(this).attr('href')
    $('html,body').animate({scrollTop: $($(this).attr('href')).offset().top}, 1200, 'easeInOutExpo');
    return false;
});

$(".category-slider-trigger").click(function(e){
    e.preventDefault;
    $this = $(this);
    var isVisible = $this.next().is(":visible");

    $(".category-slider-content").stop(true,true).slideDown('slow');
    $(".trigger-symbol").attr('src', '/images/plus.png');

    if (!isVisible) {
        $this.find(".trigger-symbol").attr('src', '/images/minus.png');
        $this.next().stop(true,true).slideUp('slow');
    }

    return false;
}); 

});

On this html:

<div class="category-slider-content">
<div class="grid_1">&nbsp;</div>
<div class="grid_10 category-filter-area">
    <a class="stock-latest-additions" href="/stock/latest-additions">Latest additions</a>
    <?
    foreach ($stock_categories as $cat) {
        echo("<h2 class=\"stock-category-title\">".$cat['title'].":</h2>");
        echo("<ul class=\"stock-categories-list\">");
        foreach ($cat['sub_categories'] as $sub_cat) {
            echo("<li><a href=\"/stock/".$util->formatCategoryParam($sub_cat['title'])."\">".$sub_cat['title']."</a></li>");
        }
        echo("</ul>");
        echo("<div class=\"clear\"></div>");
    }
    ?>
</div>
<div class="grid_1">&nbsp;</div>

Show/Hide Categories

But I'm having a problem with the functionality. When the link is clicked, the categories reveal themselves from the top and slide down, but it won't slide back up.

I've set up a jsFiddle here... http://jsfiddle.net/CwP8w/

Any suggestions?

Thanks, R

Upvotes: 1

Views: 353

Answers (3)

adeneo
adeneo

Reputation: 318362

I really don't get what the first function is supposed to do, but anyway:

$(function() {
    $('a[href*="#"]').on('click', function(e) {
        e.preventDefault;        
        $('html,body').animate({scrollTop: $(this.href).offset().top}, 1200, 'easeInOutExpo');
    });

    $(".category-slider-trigger").on('click', function(e) {
        e.preventDefault;
        $(".category-slider-content").stop(true, true).slideToggle('slow');
        $(".trigger-symbol").attr('src', $(this).is(':visible')?'/images/plus.png':'/images/minus.png');
    });
});​

FIDDLE

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 206689

DEMO

$('a[href*=#]').bind('click', function(){
    var $href = $(this).attr('href');
    $('html,body').animate({scrollTop: $($(this).attr('href')).offset().top}, 1200, 'easeInOutExpo');
    return false;
});

$(".category-slider-trigger").click(function(e){
    e.preventDefault;
    $this = $(this);
    var isVisible = $('.category-slider-content').is(":visible");
        
    if (!isVisible) {
        $('.category-slider-content').slideDown();
        $this.find(".trigger-symbol").attr('src', '/images/minus.png');
    }else{
        $('.category-slider-content').slideUp();
        $this.find(".trigger-symbol").attr('src', '/images/plus.png');
    }
                    
    return false;
});    

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34117

Working demo http://jsfiddle.net/GmpZb/3/

Good API: http://api.jquery.com/slideToggle/

you can avoid the full isVisible logic if you where only doing that for show and hide by using slideToggle.

This will help. B-)

code changed

$(".category-slider-content").stop(true,true).slideToggle('slow');

full code

$(document).ready(function() {

    $('a[href*=#]').bind('click', function() {
        var $href = $(this).attr('href')
        $('html,body').animate({
            scrollTop: $($(this).attr('href')).offset().top
        }, 1200, 'easeInOutExpo');
        return false;
    });

    $(".category-slider-trigger").click(function(e) {
        e.preventDefault;
        $this = $(this);
        var isVisible = $this.next().is(":visible");

        $(".category-slider-content").stop(true, true).slideToggle('slow');
        $(".trigger-symbol").attr('src', '/images/plus.png');

        if (!isVisible) {

            $this.find(".trigger-symbol").attr('src', '/images/minus.png');
            //$this.next().stop(true,true).slideUp('slow');
        }

        return false;
    });

});​

Upvotes: 1

Related Questions