简体   繁体   English

如何更改div文本中的展开/折叠效果的方向?

[英]How can I change the direction of the expand/collapse effect in a div text?

I'd like to get the text opened with the animation from the bottom and not from the top as I did: 我想从底部而不是从顶部开始用动画打开文本:

 function showHide(shID) { if (document.getElementById(shID)) { if (document.getElementById(shID+'-show').style.display != 'none') { document.getElementById(shID+'-show').style.display = 'none'; document.getElementById(shID+'-hide').style.display = 'inline'; document.getElementById(shID).style.height = '100px'; } else { document.getElementById(shID+'-show').style.display = 'inline'; document.getElementById(shID+'-hide').style.display = 'none'; document.getElementById(shID).style.height = '0px'; } } } 
 #example { background: red; height: 0px; overflow: hidden; transition: height 2s; -moz-transition: height 2s; /* Firefox 4 */ -webkit-transition: height 2s; /* Safari and Chrome */ -o-transition: height 2s; /* Opera */ } a.showLink, a.hideLink { text-decoration: none; background: transparent url('down.gif') no-repeat left; } a.hideLink { background: transparent url('up.gif') no-repeat left; } 
 Here is some text. <div class="readmore"> <a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">Read more</a> <div id="example" class="more"> <div class="text"> Here is some more text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae urna nulla. Vivamus a purus mi. In hac habitasse platea dictumst. In ac tempor quam. Vestibulum eleifend vehicula ligula, et cursus nisl gravida sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </div> <p> <a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide</a> </p> </div> </div> 

jsFiddle jsFiddle

If I'm interpreting your request correctly, adding position: absolute, bottom: 0 to the .text CSS, and position: relative to its container, should do what you want. 如果我正确地解释了您的请求,请在.text CSS上添加position:absolute,bottom:0和相对于其容器的position:相对应的内容。

You'll have to play around with the position of "Hide" afterwards for it to look neat. 之后,您必须摆弄“隐藏”的位置才能使它看起来很整洁。

 function showHide(shID) { if (document.getElementById(shID)) { if (document.getElementById(shID+'-show').style.display != 'none') { document.getElementById(shID+'-show').style.display = 'none'; document.getElementById(shID+'-hide').style.display = 'inline'; document.getElementById(shID).style.height = '100px'; } else { document.getElementById(shID+'-show').style.display = 'inline'; document.getElementById(shID+'-hide').style.display = 'none'; document.getElementById(shID).style.height = '0px'; } } } 
 #example { background: red; height: 0px; overflow: hidden; transition: height 2s; -moz-transition: height 2s; /* Firefox 4 */ -webkit-transition: height 2s; /* Safari and Chrome */ -o-transition: height 2s; /* Opera */ position: relative; } a.showLink, a.hideLink { text-decoration: none; background: transparent url('down.gif') no-repeat left; } a.hideLink { background: transparent url('up.gif') no-repeat left; } .text { position: absolute; bottom: 0; } 
 Here is some text. <div class="readmore"> <a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">Read more</a> <div id="example" class="more"> <div class="text"> Here is some more text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae urna nulla. Vivamus a purus mi. In hac habitasse platea dictumst. In ac tempor quam. Vestibulum eleifend vehicula ligula, et cursus nisl gravida sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </div> <p> <a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide</a> </p> </div> </div> 

Another interpretation is that you want the whole box to open from the bottom. 另一种解释是,您希望整个框从底部打开。 In that case, you need the box to be positioned at the bottom of something for it to be visible. 在这种情况下,您需要将框放置在某物的底部以使其可见。 In this case, it's #example that has position:absolute; bottom:0 在这种情况下, #example position:absolute; bottom:0 position:absolute; bottom:0

 function showHide(shID) { if (document.getElementById(shID)) { if (document.getElementById(shID+'-show').style.display != 'none') { document.getElementById(shID+'-show').style.display = 'none'; document.getElementById(shID+'-hide').style.display = 'inline'; document.getElementById(shID).style.height = '135px'; } else { document.getElementById(shID+'-show').style.display = 'inline'; document.getElementById(shID+'-hide').style.display = 'none'; document.getElementById(shID).style.height = '0px'; } } } 
 #example { background: red; height: 0px; overflow: hidden; transition: height 2s; -moz-transition: height 2s; /* Firefox 4 */ -webkit-transition: height 2s; /* Safari and Chrome */ -o-transition: height 2s; /* Opera */ position: absolute; bottom: 0; width: 100%; } a.showLink, a.hideLink { text-decoration: none; background: transparent url('down.gif') no-repeat left; z-index: 100; position: relative; } a.hideLink { background: transparent url('up.gif') no-repeat left; } 
 Here is some text. <div class="readmore"> <a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">Read more</a> <div id="example" class="more"> <div class="text"> Here is some more text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae urna nulla. Vivamus a purus mi. In hac habitasse platea dictumst. In ac tempor quam. Vestibulum eleifend vehicula ligula, et cursus nisl gravida sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </div> <p> <a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide</a> </p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM