YouTube の「もっと見る」機能を自分のサイトで再現したいと考えています。各動画の下に、「もっと見る」リンクがあります。そのリンクをクリックすると、下に移動し、以前は非表示だったテキストが表示されます。これどうやってするの?
2 に答える
私が行った CSS のみのバージョン: http://dabblet.com/gist/3157489
Chrome、Firefox、Safari、Opera、IE9+ で動作します。表示/非表示は IE9 で機能しますが、トランジションはありません (グラデーション フェードは追加の要素を使用してエミュレートされます)。
HTML 構造は次のようなものです。
<div class="info-wrapper">
<input type="checkbox" id="showhide">
<label for="showhide">
<div class="more">Show more</div>
<div>Show less</div>
</label>
<div class="info">
<p><!-- text, text, more text --></p>
<!--[if lte IE 9]><div class="aftershadow"></div><![endif]-->
</div>
</div>
CSS:
body {
background: #ccc;
}
.info-wrapper {
height: auto;
width: 500px;
margin: 4em auto;
padding: 0 0 2em 0;
position: relative;
}
.info {
max-height: 120px;
height: auto;
padding: .5em 0;
border-bottom: solid 1px #fff;
border-radius: 0 0 1em 1em;
overflow: hidden;
position: relative;
transition: 1s;
}
p { margin: 1em; }
.info:after, .aftershadow {
bottom: 0;
width: 100%;
height: 3em;
border-radius: 0 0 1em 1em;
position: absolute;
background: linear-gradient(rgba(192,192,192,0), #ccc);
content: '';
}
.aftershadow {
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00cccccc, endColorstr=#ffcccccc);
}
.info-wrapper input[type=checkbox] {
display: none;
}
.info-wrapper label {
left: 50%;
bottom: 1.5em;
width: 9em;
height: 1.25em;
margin: 0 0 0 -2.5em;
border-bottom: solid 1px #fff;
border-radius: 0 0 1em 1em;
overflow: hidden;
position: absolute;
font: 700 .67em/1.25em Arial;
text-align: center;
text-shadow: 0 1px 0 #fff;
cursor: pointer;
}
.info-wrapper label .more { margin: -.1em 0 .35em; transition: 1s; }
.info-wrapper input[type=checkbox]:checked ~ .info {
max-height: 15em;
}
.info-wrapper input[type=checkbox]:checked + label .more {
margin-top: -1.65em;
}
アイデアは次のとおりです。HTML 構造には、非表示のチェックボックスがありますが、その をクリックしてチェック/チェックを外すことができますlabel
。
最初、情報は圧縮され、チェックボックスはオフになっています。[もっと見る] ( の中にある)をクリックするlabel
と、チェックボックスにチェックが入り、疑似クラスと一般的な兄弟セレクター。label
.info
:checked
これはそれを行う部分です:
.info-wrapper input[type=checkbox]:checked ~ .info {
max-height: 15em;
}
.info-wrapper input[type=checkbox]:checked + label .more {
margin-top: -1.65em;
}
IE8でも機能する別の方法は、チェックボックスとその疑似クラスの代わりにリンクとその:focus
/:active
疑似クラスを使用すること:checked
です。この方法の欠点は、ページ内の他の場所をクリックするとすぐに、.info
再び圧縮されることです。
デモ: http://jsfiddle.net/thebabydino/U7Cyk/
HTML はあまり変わりません。チェックボックスとラベルを 2 つのリンクに置き換えただけです。
<div class="info-wrapper">
<a href="#" tabindex="1" class="more">Show more</a>
<a href="#" tabindex="1" class="less">Show less</a>
<div class="info">
<p><!--stuff, not wasting space here with it--></p>
<!--[if lte IE 9]><div class="aftershadow"></div><![endif]-->
</div>
</div>
CSSはほとんど同じです。input
&に関連するものはすべてlabel
出ており、代わりに次のものがあります。
.info-wrapper a {
left: 50%;
bottom: 1.5em;
width: 9em;
height: 1.25em;
margin: -.1em 0 .35em -4.5em;
border-bottom: solid 1px #fff;
border-radius: 0 0 1em 1em;
display: block;
overflow: hidden;
position: absolute;
color: #000;
font: 700 .67em/1.25em Arial;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 0 #fff;
transition: 1s;
cursor: pointer;
}
.info-wrapper a:focus { outline: none; }
.info-wrapper .less {
margin-top: -1.5em;
opacity : 0;
z-index: -1;
}
.info-wrapper .more:focus ~ .info,
.info-wrapper .more:active ~ .info {
max-height: 15em;
}
.info-wrapper .less:focus ~ .info,
.info-wrapper .less:active ~ .info {
max-height: 120px;
}
.info-wrapper .more:focus,
.info-wrapper .more:active {
opacity: 0;
}
.info-wrapper .more:focus + .less,
.info-wrapper .more:active + .less {
opacity: 1;
z-index: 1;
}
jQuery の使用 - デモhttp://jsfiddle.net/thebabydino/yDfTq/
$('.more').click(function(){
$(this).animate({'opacity': '0'}, 1000);
$('.less').animate({
'opacity': '1',
'z-index': '1'
}, 1000);
$('.info').animate({'height': '15em'}, 1000);
});
$('.less').click(function(){
$(this).animate({
'opacity': '0',
'z-index': '-1'
}, 1000);
$('.more').animate({'opacity': '1'}, 1000);
$('.info').animate({'height': '120px'}, 1000);
});
HTML は同じで、CSS もほとんど同じです。
.info {
height: 120px;
padding: .5em 0;
border-bottom: solid 1px #fff;
border-radius: 0 0 1em 1em;
overflow: hidden;
position: relative;
}
p { margin: 1em; }
.info:after, .aftershadow {
bottom: 0;
width: 100%;
height: 3em;
border-radius: 0 0 1em 1em;
position: absolute;
background: linear-gradient(rgba(192,192,192,0), #ccc);
content: '';
}
.aftershadow {
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00cccccc, endColorstr=#ffcccccc);
}
.info-wrapper a {
left: 50%;
bottom: 1.5em;
width: 9em;
height: 1.25em;
margin: -.1em 0 .35em -4.5em;
border-bottom: solid 1px #fff;
border-radius: 0 0 1em 1em;
display: block;
overflow: hidden;
position: absolute;
color: #000;
font: 700 .67em/1.25em Arial;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 0 #fff;
cursor: pointer;
}
.info-wrapper a:focus { outline: none; }
.info-wrapper .less { margin-top: -1.5em; opacity: 0; z-index: -1; }
タグの 1 つは、jQuery を使用する意思があることを示しています。次のリンクでは、slideToggle を使用してこの効果を実現する方法について説明しています。display: none;
最初のページ読み込み時に非表示にする場合は、div またはその他の要素のスタイルを設定する必要があります。
http://www.mkyong.com/jquery/jquery-slideup-slidedown-and-slidetoggle-example/
$("#slideToggle").click(function () {
$('.slideTogglebox').slideToggle();
});
公式ドキュメント: http://api.jquery.com/slideToggle/