PHP前端开发

HTML5实践-使用css装饰图片画廊的代码分享(二)

百变鹏仔 6个月前 (10-18) #H5教程
文章标签 画廊

在上一讲中,我们的解决方案使用到了jquery去创建一个span标签。在这一讲中我们将用一种更好的解决方式,使用:before 和 :after 伪类。:before经常会用到,他可以用来添加额外的元素。 

HTML

下面是一个ul列表代表的图片画廊。

<ul class="gallery clip">    <li>        <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-1.jpg" alt="image">    </li>    <li>        <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-2.jpg" alt="image">    </li>    <li>        <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-1.jpg" alt="image">    </li></ul>

CSS

下面是为.gallery设置的css,这里需要注意的一点是,我们需要为.gallery下面的a标签设置position: relative。

.gallery {    margin: 0 0 25px;    text-align: center;}.gallery li {    display: inline-block;    margin: 5px;    list-style: none;}.gallery a {    position: relative;    display: inline-block;}

:before元素

我们将会为 :before 元素指定一个30 x 60px大小的曲别针背景图片。注意到我将css的content属性设为空值。没有空的content属性,容器就不会显示。

立即学习“前端免费学习笔记(深入)”;


.clip a:before {    position: absolute;    content: &#39; &#39;;    top: -5px;    left: -4px;    width: 30px;    height: 60px;    background: url(http://webdesignerwall.com/wp-content/uploads/2012/09/paper-clip.png) no-repeat;}

艺术边框

利用这种技术,你可以再图片上添加任意的遮罩效果。下面的例子,我把图片背景换成了艺术边框。

.frame a:before {    position: absolute;    content: &#39; &#39;;    top: -22px;    left: -23px;    width: 216px;    height: 166px;    background: url(http://webdesignerwall.com/wp-content/uploads/2012/09/frame.png) no-repeat;}

HTML5画廊

我们可以使用html5标签,创造更高级的画廊。下面的例子,我们使用包装图片,包含图片标题。

<ul class="gallery tape">    <li>        <figure>             <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-4.jpg" alt="image">            <figcaption>Image Caption</figcaption>        </figure>    </li>    <li>        <figure>            <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-5.jpg" alt="image">            <figcaption>Image Caption</figcaption>        </figure>    </li>    <li>        <figure> <img src="http://webdesignerwall.com/wp-content/uploads/2012/09/sample-6.jpg" alt="image">            <figcaption>Image Caption</figcaption>        </figure>    </li></ul>

CSS

css中我添加了两个:before,一个针对元素,另一个针对

  • 元素。遮罩图片overlay.png被用在了figure:before上面,胶带图片用在了 a:before上面。


    .tape li {    width: 170px;    padding: 5px;    margin: 15px 10px;    border: solid 1px #cac09f;    background: #fdf8e4;    text-align: center;    box-shadow: inset 0 1px rgba(255,255,255,.8), 0 1px 2px rgba(0,0,0,.2);}.tape figure {    position: relative;    margin: 0;}.tape a:before {    position: absolute;    content: &#39; &#39;;    top: 0;    left: 0;    width: 100%;    height: 100%;    background: url(http://webdesignerwall.com/wp-content/uploads/2012/09/overlay.png) no-repeat;}.tape figcaption {    font: 100%/120% Handlee, Arial, Helvetica, sans-serif;    color: #787568;}.tape a:before {    position: absolute;    z-index: 2;    content: &#39; &#39;;    top: -12px;    left: 50%;    width: 115px;    height: 32px;    margin-left: -57px;    background: url(http://webdesignerwall.com/wp-content/uploads/2012/09/tape.png) no-repeat;}

    CSS3 Transform

    在这个例子中,我使用了软木纹饰背景,并使用transform属性转变图片。

    .transform {    background: url(http://webdesignerwall.com/wp-content/uploads/2012/09/cork-bg.png);    padding: 25px;    border-radius: 10px;    box-shadow: inset 0 1px 5px rgba(0,0,0,.4);}.transform li {    border: none;}

    Nth-of-Type

    为了让图片旋转的更随机和自然,我使用nth-of-type去筛选图片,为不同图片设置不同的旋转角度。


    .transform li:nth-of-type(4n+1) {    -webkit-transform: rotate(2deg);}.transform li:nth-of-type(2n) {    -webkit-transform: rotate(-1deg);}.transform li:nth-of-type(4n+3) {    -webkit-transform: rotate(2deg);}

    好了,今天的教程到此为止。