我正在爲書籍發佈網站中的作者頁面設計摘要容器。一些作者有更多的摘要內容,而其他作者的內容較少。我想在div容器的高度越過截斷高度(180px)時動態地啓用/禁用show more/less按鈕。所以,事實證明,動態控制div容器的高度(180px和原始高度)。我需要一段能夠在所有瀏覽器中完美運行的代碼。如何在瀏覽器調整大小時觸發基於div大小的顯示或隱藏操作
我有實施了代碼在這裏:
http://jsfiddle.net/rraagghhu/9dgs6432/3/
HTML:
<div class = "container">
<div class="info-wrapper">
<div class="info">
Chetan Bhagat is the author of six blockbuster books.These include five novels—Five Point Someone (2004), One Night @ the Call Center (2005), The 3 Mistakes of My Life (2008), 2 States (2009),
Revolution 2020 (2011), the non-fiction title What Young India Wants (2012) and Half Girlfriend (2014). Chetan’s books have remained bestsellers since their release.
Four out his five novels have been already adapted into successful Bollywood films and the others are in process of being adapted as well. The New York Times called him the ‘the biggest selling English language novelist in India’s history’. Time magazine named him amongst the ‘100 most influential people in the world’ and Fast Company, USA, listed him as one of the world’s ‘100 most creative people in business’. Chetan writes columns for leading English and Hindi newspapers, focusing on youth and national development issues. He is also a motivational speaker and screenplay writer. Chetan quit his international investment banking career in 2009 to devote his entire time to writing and make change happen in the country. He lives in Mumbai with his wife, Anusha, an ex-classmate from IIM-A, and his twin boys, Shyam and Ishaan. You can email him at [email protected] or fill in the Guestbook with your feedback. You can also follow him on twitter (@chetan_bhagat) or like his Facebook fanpage (https://www.facebook.com/chetanbhagat.fanpage).
</div>
<a href="#" class="more">(more)</a>
<a href="#" class="less">(less)</a>
</div>
<div class = "footer"> THIS IS FOOTER </div>
</div>
CSS:
.container{
background-color: yellow;
}
.footer{
background-color: yellow;
}
.info-wrapper {
height: auto;
position: relative;
width: auto;
padding: 0 0 2.5em 0;
background-color: red;
}
.info {
max-height: 180px;
height: auto;
width: auto;
overflow: hidden;
position: relative;
text-align: justify;
}
.info:after, .aftershadow {
bottom: 0;
width: auto;
height: auto;
}
.info-wrapper a {
left: 50%;
position: relative;
font: 700 .67em/1.25em Arial;
text-align: center;
text-decoration: underline;
cursor: pointer;
}
.less { height: auto; display: none; }
.more { display: none; }
的Jquery:
if($(".info")[0].scrollHeight > $(".info").height()) {
$("a.more").show();
}
$("a.more").click(function(e){
e.preventDefault();
$(".info").css({"overflow": "visible"});
$("a.less").show();
$("a.more").hide();
return false;
});
$("a.less").click(function(e){
e.preventDefault();
$(".info").css({"overflow": "hidden"});
$("a.more").show();
$("a.less").hide();
return false;
});
正如您所看到的,頁腳停留在絕對位置。如果更多按鈕被點擊,則(更少)應該降低,低於應該來自頁腳。當瀏覽器收縮/展開時,是否可以動態啓用/禁用show more/less按鈕?
謝謝你幫助我。現在,我請求你做下面的事情。 1.轉到上面給出的更新的JSFiddle鏈接。 2.展開結果屏幕儘可能寬。 3.運行腳本。 4.現在,縮小結果屏幕。 隨着你越來越近,你可以看到最後的一些文字不見了。這是因爲您正在調整運行時的屏幕寬度。是否有可能編寫代碼來解決這個問題呢?謝謝你幫助我。 :-) –
你的意思是當'info'高度低於'180px'時隱藏'show more'按鈕,當調整窗口大小時'info'高度超過'180px'時顯示然後按鈕? –
檢查我的JSFiddle,我已經更新它。謝謝;) –