jqueryhtmlcssjquery-uijquery-slide-effects

jquery toggle slide does not slide correctly only the first time


Thats my codepen: http://codepen.io/helloworld/pen/pvPzLa

When you press the left/right chevron button to slide the div does not slide to the right BUT from the right to the left the first time.

I know the cause is that I set the visibility property to hidden/collapse initially.

And when the user clicks the slide button I remove the visibility attribute from the div to be slided.

How can I fix the wrong behavior?

HTML

  <div id="availableOptionsSidebar" style="background-color:pink;" class="col-lg-1">
        <div id="availableOptionSidebarContainer" style="text-align:center;display: table;z-index:100000;background-color:orange;">
            <span id="availableOptionSidebarContainerOpener" style="cursor:pointer;font-size:2em;display:table-cell;vertical-align:middle;" class="glyphicon glyphicon-chevron-left"></span>
            <div style="position:relative;height:100%;">
                <div id="availableOptionsContainer" style="position:absolute;height:100%;width:180px;border:black solid 1px;background-color:red;">
                    <div>
                        <span style="cursor:pointer;font-size:2em;display:table-cell;vertical-align:middle;" class="glyphicon glyphicon-chevron-left"></span>
                        <span style="cursor:pointer;font-size:2em;display:table-cell;vertical-align:middle;" class="glyphicon glyphicon-chevron-right"></span>
                    </div>
                    <div>
                        <div class="row">
                            <div>1</div>
                            <div>2</div>
                            <div>3</div>
                        </div>
                        <div class="row">
                            <div>1</div>
                            <div>2</div>
                            <div>3</div>
                        </div>
                        <div class="row">
                            <div>1</div>
                            <div>2</div>
                            <div>3</div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
</div>

JAVASCRIPT

   $(function () {

        $('#parameterContainer').height(400);
        $('#availableOptionSidebarContainer').height(400);
        $('#availableOptionsContainer').css('visibility', 'hidden');

        $('#availableOptionSidebarContainer').click(function () {

        $('#availableOptionsContainer').css('visibility', "");


            if ($("#availableOptionSidebarContainerOpener").is(".glyphicon-chevron-left")) { // sidebar is closed
                $("#availableOptionSidebarContainerOpener").removeClass("glyphicon-chevron-left");
                $("#availableOptionSidebarContainerOpener").addClass("glyphicon-chevron-right");

            }
            else { // sidebar is openend
                $("#availableOptionSidebarContainerOpener").removeClass("glyphicon-chevron-right");
                $("#availableOptionSidebarContainerOpener").addClass("glyphicon-chevron-left");

            }


            // Set the effect type
            var effect = 'slide';

            // Set the options for the effect type chosen
            var options = { direction: 'left' };

            // Set the duration (default: 400 milliseconds)
            var duration = 500;

            $('#availableOptionsContainer').toggle(effect, options, duration);

         });
   });

Solution

  • Probably you want to set display: none instead of visibility property to #availableOptionsContainer.


    FYI: with jQuery you can toggle multiple classes. Instead of this mess from your demo

    if ($("#availableOptionSidebarContainerOpener").is(".glyphicon-chevron-left")) {
        // sidebar is closed
        $("#availableOptionSidebarContainerOpener").removeClass("glyphicon-chevron-left");
        $("#availableOptionSidebarContainerOpener").addClass("glyphicon-chevron-right");
        $("#availableOptionSidebarContainerOpener").removeClass("rotateIn");
        $("#availableOptionSidebarContainerOpener").addClass("rotateOut"); 
    }
    else {
        // sidebar is openend
        $("#availableOptionSidebarContainerOpener").removeClass("glyphicon-chevron-right");
        $("#availableOptionSidebarContainerOpener").addClass("glyphicon-chevron-left");
        $("#availableOptionSidebarContainerOpener").addClass("rotateIn");
        $("#availableOptionSidebarContainerOpener").removeClass("rotateOut"); 
    }
    

    you can just do

    $("#availableOptionSidebarContainerOpener").toggleClass("glyphicon-chevron-right glyphicon-chevron-left rotateIn rotateOut");
    

    and don't forget to set initial rotateIn class at your HTML.