Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

css - 如何转换高度:0; 达到高度:自动; 使用CSS?(How can I transition height: 0; to height: auto; using CSS?)

I am trying to make a <ul> slide down using CSS transitions.

(我正在尝试使用CSS过渡使<ul>滑落。)

The <ul> starts off at height: 0;

(<ul>height: 0;)

.

(。)

On hover, the height is set to height:auto;

(悬停时,将高度设置为height:auto;)

.

(。)

However, this is causing it to simply appear, not transition,

(但是,这导致它只是显示而不是过渡,)

If I do it from height: 40px;

(如果我从height: 40px;)

to height: auto;

(达到height: auto;)

, then it will slide up to height: 0;

(,那么它将向上滑动到height: 0;)

, and then suddenly jump to the correct height.

(,然后突然跳到正确的高度。)

How else could I do this without using JavaScript?

(不使用JavaScript,我还能怎么做?)

 #child0 { height: 0; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease; } #parent0:hover #child0 { height: auto; } #child40 { height: 40px; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease; } #parent40:hover #child40 { height: auto; } h1 { font-weight: bold; } 
 The only difference between the two snippets of CSS is one has height: 0, the other height: 40. <hr> <div id="parent0"> <h1>Hover me (height: 0)</h1> <div id="child0">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div> </div> <hr> <div id="parent40"> <h1>Hover me (height: 40)</h1> <div id="child40">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div> </div> 

  ask by Hailwood translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use max-height in the transition and not height .

(在过渡中使用max-height ,而不要使用height 。)

And set a value on max-height to something bigger than your box will ever get.

(并将max-height的值设置为比您的盒子所能达到的更大的值。)

See JSFiddle demo provided by Chris Jordan in another answer here.

(请参阅Chris Jordan提供的JSFiddle演示 ,在此处还有另一个答案 。)

 #menu #list { max-height: 0; transition: max-height 0.15s ease-out; overflow: hidden; background: #d5d5d5; } #menu:hover #list { max-height: 500px; transition: max-height 0.25s ease-in; } 
 <div id="menu"> <a>hover me</a> <ul id="list"> <!-- Create a bunch, or not a bunch, of li's to see the timing. --> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul> </div> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...