2015-01-26 88 views
0

我有點難以解決這個CSS自動高度的過渡問題,並想知道其他人是否有任何線索和建議。提前致謝。自動高度不工作的CSS切換過渡動畫

  1. 這裏是我試圖讓工作自動高度的一個例子,但過渡不工作,但自動高度理想與自適應網站:

http://jsfiddle.net/z5x6abog/

的Javascript :

$(document).ready(function(){ 
    $(".button").click(function(){ 
    $(".test").toggleClass("collapse"); 
    }); 
}); 

CSS:

.test.collapse ul { 
height: auto; 
  • 這裏是與特定像素的特定高度的例子,但請注意的部位是敏感和高度將總是由於浮動列表改變這種不理想:
  • http://jsfiddle.net/obn34ufx/

    CSS:

    .test.collapse ul { 
    height: 500px; 
    

    會有人知道的方法,使例#1的工作?

    謝謝!

    +1

    這可能幫助:http://stackoverflow.com/questions/3508605/css-transition-height-0-to-height -auto – Pevara 2015-01-26 23:03:49

    回答

    1

    感謝大家的反饋。下面是我用哪去了最終的解決方案允許響應和平滑切換:

    http://jsfiddle.net/jhqvL4q0/4/

    $(document).ready(function(){ 
        $(".button").click(function(){ 
         $(".collapse-list").toggleClass("collapse"); 
        }); 
    }); 
    
    +0

    謝謝。當我將它設置爲'max-height:9999px',但不能使用'max-height:999px'時... – Moe 2017-02-22 14:46:02

    0

    嘗試類似下面。走LI和高度,這樣就不會有任何東西被切斷。根據需要調整。

    $(document).ready(function(){ 
     
        $(".button").click(function(){ 
     
        $(".test").toggleClass("expand", 5000); 
     
        }); 
     
    });
    .test ul { 
     
    width:50%; 
     
    overflow:hidden; 
     
    height:auto; 
     
    -webkit-transition-duration: 0.8s; 
     
    -moz-transition-duration: 0.8s; 
     
    -o-transition-duration: 0.8s; 
     
    transition-duration: 0.8s; 
     
    } 
     
    
     
    .test li { 
     
    float:left; 
     
    width:100px; 
     
    } 
     
    
     
    .test li:not(:nth-of-type(-n+10)) { 
     
    width:100px; 
     
        display: none; 
     
    } 
     
    
     
    .test.collapse ul { 
     
    width:50%; 
     
    height: 500px; 
     
    -webkit-transition-duration: 0.8s; 
     
    -moz-transition-duration: 0.8s; 
     
    -o-transition-duration: 0.8s; 
     
    transition-duration: 0.8s; 
     
    } 
     
    
     
    
     
    .test.expand ul li { 
     
        display: block; 
     
        
     
        
     
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <div class="test"> 
     
        <ul> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
         <li>Malesuada</li> 
     
         <li>Pellentesque</li> 
     
         <li>ulum tortor</li> 
     
         <li>amet quam</li> 
     
        </ul> 
     
    \t <a href="#" class="button">Toggle</a> 
     
    </div>

    +0

    感謝圓滑極客,不幸的是它似乎沒有出現在Safari上。我一定會更深入地看看你的代碼,但是想提一下,我可以在Pevara的評論幫助下解決這個問題。這裏是一個例子:http://jsfiddle.net/jhqvL4q0/ – Chumtarou 2015-01-27 02:13:55

    1

    這是不可能的使用具有高度的轉換:汽車。 最大高度技巧是一個相當不錯的解決方案,但有一些不便之處,尤其是如果最大高度比真實高度高得多的奇怪延遲。

    下面是我使用的技巧:http://jsfiddle.net/g56jwux4/2/ 基本上它是兩個相反方向翻譯的鱗片DIV。看一下jsfiddle的代碼,因爲我的英文不夠好,不足以清楚地解釋它。

    HTML部分:

    <body> 
        <p>Technicaly this dropdown menu looks like a simple height transition.</p> 
        <p>But this pure css code also works this a variable number of choices in menu, working around the "height:auto" not taken into account by css transitions.</p> 
        <input type="checkbox" id="menuOpen"></input> 
        <label id="bouton" for="menuOpen"><span>Click on me !</span> 
         <div id="menu"> 
          <div id="masque"> 
           <div class="choix" id="choix1">Choix 1</div> 
           <div class="choix" id="choix2">Choix 2</div> 
           <div class="choix" id="choix3">Choix 3 tr&egrave;s tr&egrave;s long pour voir la taille finale du menu</div> 
           <div class="choix" id="choix4">Choix 4</div> 
           <div class="choix" id="choix5">Choix 5</div> 
           <div class="choix" id="choix6">Choix 6</div> 
          </div> 
         </div> 
        </label> 
    </body> 
    

    CSS部分:

    body { 
        font-family: sans-serif; 
    } 
    #menuOpen { 
        display: none; 
    } 
    #bouton { 
        position: absolute; 
        left: 100px; 
        top: 100px; 
        width: 200px; 
        height: 30px; 
        background-color: lightgray; 
        cursor: pointer; 
    } 
    #bouton > span { 
        color: black; 
        padding: 6px; 
        line-height: 30px; 
    } 
    #menu { 
        position: absolute; 
        top: 100%; 
        overflow: hidden; 
        min-width: 100%; 
        transition: transform 0.3s linear 0s, visibility 0.3s linear 0s; 
        transform: translateY(-100%); 
        visibility: hidden; 
        color: white; 
    } 
    #menuOpen:checked + #bouton > #menu { 
        transform: translateY(0%); 
        visibility: visible; 
        transition: transform 0.3s linear 0s, visibility 0s linear 0s; 
    } 
    #menuOpen:checked + #bouton > #menu > #masque { 
        transform: translateY(0%); 
    } 
    #masque { 
        position: relative; 
        background-color: gray; 
        transform: translateY(100%); 
        transition: transform 0.3s linear 0s; 
    } 
    .choix { 
        white-space: nowrap; 
        padding: 3px 6px; 
    } 
    .choix:hover { 
        background-color: darkgray; 
    }