jquery切换幻灯片仅在第一次才能正确滑动

时间:2015-01-17 17:45:14

标签: jquery html css jquery-ui jquery-slide-effects

这就是我的代码:http://codepen.io/helloworld/pen/pvPzLa

当您按左/右V形按钮滑动时,div不会在第一次从右向左滑动到右侧。

我知道原因是我最初将visibility属性设置为hidden / collapse。

当用户点击幻灯片按钮时,我会从要删除的div中删除visibility属性。

如何修复错误行为?

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);

         });
   });

1 个答案:

答案 0 :(得分:0)

您可能希望将display: none而不是visibility属性设置为#availableOptionsContainer


仅供参考:使用jQuery,您可以toggle multiple classes。而不是你的演示中的这个烂摊子

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"); 
}

你可以做到

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

并且不要忘记在HTML上设置初始rotateIn课程。