如何复制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; }
其中一个标签表示您愿意使用jQuery。以下链接解释了如何使用slideToggle实现此效果。如果希望div或其他元素在加载第一页时隐藏,则应将其样式设置为display: none;

http://www.mkyong.com/jquery/jquery-slideup-slidedown-and-slidetoggle-example/

$("#slideToggle").click(function () {
   $('.slideTogglebox').slideToggle();
});

官方文件:http://api.jquery.com/slideToggle/