CSS3实现炫酷的切片式图片轮播效果

约定不等于承诺〃 2021-11-01 18:58 423阅读 0赞

轮播的构成
HTML由三个主要部分组成:单选按钮和标签、包含四个面板的容器及其每张图片的“切片”以及标题。设置为class=”cr-bgimg”的容器将为每个图片划分四个面板,其中,在每个面板里通过background-position属性将背景图片定位在合适的位置上。所以,第一个面板将有四个切片,每个切片都有一个图片作为背景,并且位于容器最左边的位置。第二个面板也有四个切片,它位于第一个面板的右侧,用于显示图片的第二个切片部分:

  1. <section class="cr-container">
  2. <!-- 单选按钮和label标签 -->
  3. <input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1" checked/>
  4. <label for="select-img-1" class="cr-label-img-1">1</label>
  5. <input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2" />
  6. <label for="select-img-2" class="cr-label-img-2">2</label>
  7. <input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3" />
  8. <label for="select-img-3" class="cr-label-img-3">3</label>
  9. <input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4" />
  10. <label for="select-img-4" class="cr-label-img-4">4</label>
  11. <div class="clr"></div>
  12. <!-- 面板 -->
  13. <div class="cr-bgimg">
  14. <div>
  15. <span>Slice 1 - Image 1</span>
  16. <span>Slice 1 - Image 2</span>
  17. <span>Slice 1 - Image 3</span>
  18. <span>Slice 1 - Image 4</span>
  19. </div>
  20. <div>
  21. <span>Slice 2 - Image 1</span>
  22. <span>Slice 2 - Image 2</span>
  23. <span>Slice 2 - Image 3</span>
  24. <span>Slice 2 - Image 4</span>
  25. </div>
  26. <div>
  27. <span>Slice 3 - Image 1</span>
  28. <span>Slice 3 - Image 2</span>
  29. <span>Slice 3 - Image 3</span>
  30. <span>Slice 3 - Image 4</span>
  31. </div>
  32. <div>
  33. <span>Slice 4 - Image 1</span>
  34. <span>Slice 4 - Image 2</span>
  35. <span>Slice 4 - Image 3</span>
  36. <span>Slice 4 - Image 4</span>
  37. </div>
  38. </div>
  39. <!-- 标题 -->
  40. <div class="cr-titles">
  41. <h3>
  42. <span>Serendipity</span>
  43. <span>What you've been dreaming of</span>
  44. </h3>
  45. <h3>
  46. <span>Adventure</span>
  47. <span>Where the fun begins</span>
  48. </h3>
  49. <h3>
  50. <span>Nature</span>
  51. <span>Unforgettable eperiences</span>
  52. </h3>
  53. <h3>
  54. <span>Serenity</span>
  55. <span>When silence touches nature</span>
  56. </h3>
  57. </div>
  58. </section>

h3元素中包含两个span元素,一个是主标题,一个是子标题。
CSS
为了代码的简洁,文章的CSS中都省略了浏览器前缀,但是你获取到的代码是包含完整的。
我们的第一个目标是实现点击label元素的时候触发对应的单选按钮,方法很简单,就是将label元素的for属性值与单选按钮的ID值对应起来就可以了。
第二步,我们要将所有的背景图片定位在正确的位置上。第三步,在单击label标签时,显示对应的图片切片和标题。
我们一步一步看,首先设置最外层section元素的样式,给它设置一个淡淡的阴影效果。

  1. .cr-container{
  2. width: 600px;
  3. height: 400px;
  4. position: relative;
  5. margin: 0 auto;
  6. border: 20px solid #fff;
  7. box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
  8. }

因为后面我们要使用通用兄弟选择器“~”来选中对应的图片切片和标题,于是将所有的label元素放在代码的最上面。通过设置z-index属性确保label元素显示在图片和文字的上面,并且通过margin-top属性使它们距离整体上边框350px。

  1. .cr-container label{
  2. font-style: italic;
  3. width: 150px;
  4. height: 30px;
  5. cursor: pointer;
  6. color: #fff;
  7. line-height: 32px;
  8. font-size: 24px;
  9. float:left;
  10. position: relative;
  11. margin-top: 350px;
  12. z-index: 1000;
  13. }

接着,添加一个小圆圈来美化label元素,我们创建一个伪元素,并将其放在文字的中心位置。

  1. .cr-container label:before{
  2. content:'';
  3. width: 34px;
  4. height: 34px;
  5. background: rgba(130,195,217,0.9);
  6. position: absolute;
  7. left: 50%;
  8. margin-left: -17px;
  9. border-radius: 50%;
  10. box-shadow: 0px 0px 0px 4px rgba(255,255,255,0.3);
  11. z-index:-1;
  12. }

为了在面板与面板之间创建一个分隔线,我们使用label元素的另一个伪元素,并通过渐变背景将它从图片的上边缘延伸到下边缘。

  1. .cr-container label:after{
  2. width: 1px;
  3. height: 400px;
  4. content: '';
  5. background: linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
  6. position: absolute;
  7. bottom: -20px;
  8. right: 0px;
  9. }

最后一个面板的右侧不应该有分隔线,所以我们将它的宽度设置为0。

  1. .cr-container label.cr-label-img-4:after{
  2. width: 0px;
  3. }

现在,label标签的样式就完成了,我们可以将所有的单选按钮隐藏掉。

  1. .cr-container input{
  2. display: none;
  3. }

当我们点击一个label元素时,它对应的单选按钮就是被选中。反过来,就可以使用通用兄弟选择器来选择选中按钮对应的label元素,并改变它的文字颜色。

  1. .cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1,
  2. .cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2,
  3. .cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3,
  4. .cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4{
  5. color: #68abc2;
  6. }

我们还可以改变小圆圈的颜色并添加一个阴影效果。

  1. .cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1:before,
  2. .cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2:before,
  3. .cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3:before,
  4. .cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4:before{
  5. background: #fff;
  6. box-shadow: 0px 0px 0px 4px rgba(104,171,194,0.6);
  7. }

图片的容器将占据所有的宽度,并且绝对定位。稍后使用这个容器将背景图片设置为当前选定的图片。我们还需要一张默认可见的图片,因此,再添加一些背景属性:

  1. .cr-bgimg{
  2. width: 600px;
  3. height: 400px;
  4. position: absolute;
  5. left: 0px;
  6. top: 0px;
  7. z-index: 1;
  8. background-repeat: no-repeat;
  9. background-position: 0 0;
  10. }

因为我们有四个面板,每个面板的宽度为150像素(600除以4)。面板设置为左浮动后会并排显示,同时设置它们溢出隐藏,因为我们不希望在滑动时看到切片出来:

  1. .cr-bgimg div{
  2. width: 150px;
  3. height: 100%;
  4. position: relative;
  5. float: left;
  6. overflow: hidden;
  7. background-repeat: no-repeat;
  8. }

每个切片也被设置为绝对定位,并且通过left:-150px将它们显示在面板的外面并隐藏起来:

  1. .cr-bgimg div span{
  2. position: absolute;
  3. width: 100%;
  4. height: 100%;
  5. top: 0px;
  6. left: -150px;
  7. z-index: 2;
  8. text-indent: -9000px;
  9. }

再来,让我们来处理容器和每个切片的背景图片:

  1. .cr-container input.cr-selector-img-1:checked ~ .cr-bgimg,
  2. .cr-bgimg div span:nth-child(1){
  3. background-image: url(../images/1.jpg);
  4. }
  5. .cr-container input.cr-selector-img-2:checked ~ .cr-bgimg,
  6. .cr-bgimg div span:nth-child(2){
  7. background-image: url(../images/2.jpg);
  8. }
  9. .cr-container input.cr-selector-img-3:checked ~ .cr-bgimg,
  10. .cr-bgimg div span:nth-child(3){
  11. background-image: url(../images/3.jpg);
  12. }
  13. .cr-container input.cr-selector-img-4:checked ~ .cr-bgimg,
  14. .cr-bgimg div span:nth-child(4){
  15. background-image: url(../images/4.jpg);
  16. }

我们还需要根据不同的面板为切片的背景图片提供不同的定位:

  1. .cr-bgimg div:nth-child(1) span{
  2. background-position: 0px 0px;
  3. }
  4. .cr-bgimg div:nth-child(2) span{
  5. background-position: -150px 0px;
  6. }
  7. .cr-bgimg div:nth-child(3) span{
  8. background-position: -300px 0px;
  9. }
  10. .cr-bgimg div:nth-child(4) span{
  11. background-position: -450px 0px;
  12. }

当我们单击一个label标签时,我们将所有的切片滑到右边:

  1. .cr-container input:checked ~ .cr-bgimg div span{
  2. animation: slideOut 0.6s ease-in-out;
  3. }
  4. @keyframes slideOut{
  5. 0%{
  6. left: 0px;
  7. }
  8. 100%{
  9. left: 150px;
  10. }
  11. }

但是除了我们选中的这个label标签,它对应的图片切片是从-150px滑到0px:

  1. .cr-container input.cr-selector-img-1:checked ~ .cr-bgimg div span:nth-child(1),
  2. .cr-container input.cr-selector-img-2:checked ~ .cr-bgimg div span:nth-child(2),
  3. .cr-container input.cr-selector-img-3:checked ~ .cr-bgimg div span:nth-child(3),
  4. .cr-container input.cr-selector-img-4:checked ~ .cr-bgimg div span:nth-child(4)
  5. {
  6. transition: left 0.5s ease-in-out;
  7. animation: none;
  8. left: 0px;
  9. z-index: 10;
  10. }

最后,设置h3标签中主副标题的样式,当我们点击了某个label标签后,它对应的标题的透明度会从0过渡到1:

  1. .cr-titles h3{
  2. position: absolute;
  3. width: 100%;
  4. text-align: center;
  5. top: 50%;
  6. z-index: 10000;
  7. opacity: 0;
  8. color: #fff;
  9. text-shadow: 1px 1px 1px rgba(0,0,0,0.1);
  10. transition: opacity 0.8s ease-in-out;
  11. }
  12. .cr-titles h3 span:nth-child(1){
  13. font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
  14. font-size: 70px;
  15. display: block;
  16. letter-spacing: 7px;
  17. }
  18. .cr-titles h3 span:nth-child(2){
  19. letter-spacing: 0px;
  20. display: block;
  21. background: rgba(104,171,194,0.9);
  22. font-size: 14px;
  23. padding: 10px;
  24. font-style: italic;
  25. font-family: Cambria, Palatino, "Palatino Linotype", "Palatino LT STD", Georgia, serif;
  26. }
  27. .cr-container input.cr-selector-img-1:checked ~ .cr-titles h3:nth-child(1),
  28. .cr-container input.cr-selector-img-2:checked ~ .cr-titles h3:nth-child(2),
  29. .cr-container input.cr-selector-img-3:checked ~ .cr-titles h3:nth-child(3),
  30. .cr-container input.cr-selector-img-4:checked ~ .cr-titles h3:nth-child(4){
  31. opacity: 1;
  32. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,423人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HTML+CSS+JQ实现图片效果

    焦点图广泛运用于主页的醒目位置放置广告,简单好用,加上效果也很好看。 实现的效果: 5张图片每两秒轮播一次,右下角的数字随图片而变换,鼠标放在数字上图片停止,移走