2015-01-17 81 views
1

這就是我的codepen:http://codepen.io/helloworld/pen/pvPzLajQuery的切換幻燈片不正確不僅滑動首次

當您按下左/右V形按鈕到div滑不滑向右但是從右邊到左邊第一次。

我知道原因是我最初設置隱藏/摺疊的可見性屬性。

而當用戶單擊幻燈片按鈕我刪除div的可見性屬性被滑動。

我該如何解決錯誤的行爲?

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

     }); 
    }); 

回答

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類。

+0

感謝toggleClass提示。我昨天也發現了它。有2 toogleClass爲chevron +旋轉,但你做了更好的; - ) – Pascal