如何复制YouTube;s”;显示更多“;功能
How do I reproduce YouTube's "Show More" functionality?
我想在我的网站上重现YouTube的"显示更多"功能。每个视频下面都有一个"显示更多"链接。如果你点击该链接,它会向下移动,并显示一些以前隐藏的文本。我该怎么做?
我刚刚做的一个纯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
内部)勾选复选框,您现在可以使用:checked
伪类和通用同级选择器更改HTML中与其同级和其后的元素的样式(在本例中为label
和.info
)。
这就是做到这一点的部分:
.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没有太大变化,我只是用两个链接替换复选框和标签
<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基本相同。与CCD_ 10&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; }
display: none;
。
http://www.mkyong.com/jquery/jquery-slideup-slidedown-and-slidetoggle-example/
$("#slideToggle").click(function () {
$('.slideTogglebox').slideToggle();
});
官方文件:http://api.jquery.com/slideToggle/
- OpenLayers在悬停时高亮显示功能,并在单击时选择
- 显示功能导致 ng 重复
- 模态无法访问其显示功能
- 单击时隐藏和显示功能
- 如何在 OpenLayers 3 中隐藏和显示功能?(重绘?
- 为什么这个隐藏显示功能不起作用
- 如何在以下情况下使隐藏/显示功能顺利进行
- ng显示功能不起作用
- 在移动浏览器中通过javascript加载图像显示功能
- d3js气泡图中的气泡突出显示功能
- jQuery脚本的顺序正确吗?隐藏/显示功能在实时版本中不起作用,但可以作为独立Fiddle使用
- 谷歌分析-显示功能与事件点击一起发送
- 为HTML样式添加隐藏/显示功能(不在CSS文档中)
- javascript的隐藏/显示功能在Firefox中不起作用
- 将显示功能应用于ajax成功创建的元素
- 隐藏和显示功能在Safari中不工作
- Javascript文本高亮显示功能
- 隐藏/显示功能一次只工作一个?——Jquery
- Javascript幻灯片显示功能一次打开所有幻灯片
- JQuery的隐藏和显示功能并不适用于所有的滑块