图片/图形

CSS3炫酷堆叠图片展示动画特效

阿里云


这是一款使用纯 CSS3 制作的炫酷堆叠图片展示动画特效插件。该堆叠图片展示特效开始时所用图片都堆叠在一起,然后会以各种动画效果将最上面一张图片切换掉,有旋转、滑落效果、放大效果、淡入淡出效果、弹射效果等。该堆叠图片展示效果是将一组图片堆叠在一起,然后以各种 CSS 动画效果来切换最上面的一张图片。该特效将使用纯 CSS3 制作,它可以在所有现代浏览器和移动设备上运行。该堆叠图片展示特效将被做成自动播放模式,就像播放幻灯片一样。

HTML 结构

html 结构采用一个 HTML5 的标准结构来制作。

也想出现在这里?联系我们
创客主机
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8" />
  5.   <title>Animated CSS3 Photo Stack | Tutorialzine Demo</title>
  6.   <!-- CSS Includes -->
  7.   <link href="assets/css/style.css" rel="stylesheet" />
  8.   <link href="assets/css/animate.css" rel="stylesheet" />
  9. </head>
  10. <body>
  11.   <ul id="photos">
  12.     <li><a href="#"
  13.     style="background-image:url(...)">Landscape 5</a></li>
  14.     <!-- More photos here -->
  15.   </ul>
  16.   <a href="#" class="arrow previous"></a>
  17.   <a href="#" class="arrow next"></a>
  18.   <!-- Libraries -->
  19.   <script src="http://libs.useso.com/js/jquery/1.9.1/jquery.min.js"></script>
  20.   <script src="assets/js/script.js"></script>
  21. </body>
  22. </html>

#photos 的无序列表包含我们将要动画的图片。对于每一张图片,我们使用一个 li 元素来制作,在里面还包含一个 a 标签。图像被设置为链接的背景图片,正如你看到的 CSS 代码,我们使用 background-size 属性来使背景图片的宽度和高度和链接的大小一样大。当你添加更多的图片的时候,你要注意,因为它们的位置是绝对定位的,所以它们时反向显示的(后添加的先显示)。我们在这个堆叠图片展示动画特效中使用了 animate.css-一个强大的 CSS3 动画库。我们还使用了一点 jQuery 代码来控制两个前后导航按钮。css3 堆叠图片展示动画特效

  1. JAVASCRIPT

为了触发 animate.css 给我们的动画效果,我们需要给动画元素添加相应的 CLASS。我们在一张图片展示完毕,必须将它移动到堆叠图片栈的底部,以便能显示下一张图片。下面是我们需要做的事情:

首先,我们要监听两个导航按钮的 click 事件

如果“下一张图片”按钮被点击,我们将随机选择一种 animate.css 效果的 class,将它添加到堆叠图片栈顶部的图片上。(实际上是给 l 列表项的最后一个元素添加)

当图片展示动画结束后,我们将把当前动画 li 元素用 jQuery 的 prependTo 方法移动到其它所有 li 元素的前面。并移除它的 animate.css 效果的 class

对于“前一张图片”按钮,我们做相同的事情。唯一不同的是,在触发动画前,我们将最后一张图片放到图片栈的顶部。

JS 代码

  1. $(function() {
  2.   var exits = ['fadeOut', 'fadeOutDown', 'fadeOutUpBig', 'bounceOut', 'bounceOutDown',
  3.     'hinge', 'bounceOutUp', 'bounceOutLeft', 'rotateOut', 'rotateOutUpLeft',
  4.     'lightSpeedOut', 'rollOut'];
  5.   var entrances = ['fadeIn', 'fadeInDown', 'fadeInRight', 'bounceIn', 'bounceInRight',
  6.       'rotateIn', 'rotateInDownLeft', 'lightSpeedIn', 'rollIn', 'bounceInDown']; 
  7.   var photos = $('#photos'),
  8.     ignoreClicks = false;
  9.   $('.arrow').click(function(e, simulated){
  10.     if(ignoreClicks){
  11.       // If clicks on the arrows should be ignored,
  12.       // stop the event from triggering the rest
  13.       // of the handlers
  14.       e.stopImmediatePropagation();
  15.       return false;
  16.     }
  17.     // Otherwise allow this click to proceed,
  18.     // but raise the ignoreClicks flag
  19.     ignoreClicks = true;
  20.     if(!simulated){
  21.       // Once the user clicks on the arrows,
  22.       // stop the automatic slideshow
  23.       clearInterval(slideshow);
  24.     }
  25.   });
  26.   // Listen for clicks on the next arrow
  27.   $('.arrow.next').click(function(e){
  28.     e.preventDefault();
  29.     // The topmost element
  30.     var elem = $('#photos li:last');
  31.     // Apply a random exit animation
  32.     elem.addClass('animated')
  33.       .addClass( exits[Math.floor(exits.length*Math.random())] );
  34.     setTimeout(function(){
  35.       // Reset the classes
  36.       elem.attr('class','').prependTo(photos);
  37.       // The animation is complate!
  38.       // Allow clicks again:
  39.       ignoreClicks = false;
  40.     },1000);
  41.   });
  42.   // Listen for clicks on the previous arrow
  43.   $('.arrow.previous').click(function(e){
  44.     e.preventDefault();
  45.     // The bottom-most element
  46.     var elem = $('#photos li:first');
  47.     // Move the photo to the top, and
  48.     // apply a random entrance animation
  49.     elem.appendTo(photos)
  50.       .addClass('animated')
  51.       .addClass( entrances[Math.floor(entrances.length*Math.random())] );
  52.     setTimeout(function(){
  53.       // Remove the classess
  54.       elem.attr('class','');
  55.       // The animation is complate!
  56.       // Allow clicks again:
  57.       ignoreClicks = false;
  58.     },1000);
  59.   });
  60.   // Start an automatic slideshow
  61.   var slideshow = setInterval(function(){
  62.     // Simulate a click every 1.5 seconds
  63.     $('.arrow.next').trigger('click',[true]);
  64.   }, 1500);
  65. });

CSS 样式

下面我们需要添加 CSS 样式。这里不列出所有的 CSS 代码,只列出关于堆叠图片部分的代码:

  1. #photos{
  2.   margin:0 auto;
  3.   padding-top:120px;
  4.   width:450px;
  5.   position:relative;
  6. }
  7.  
  8. #photos li{
  9.   position:absolute;
  10.   width:450px;
  11.   height:450px;
  12.   overflow:hidden;
  13.   background-color:#fff;
  14.   box-shadow: 1px 1px 1px #ccc;
  15.   z-index:10;
  16.  
  17.   -webkit-animation-duration: 1s;
  18.   -moz-animation-duration: 1s;
  19.   animation-duration: 1s;
  20. }
  21.  
  22. #photos li a{
  23.   position:absolute;
  24.   top:6px;
  25.   left:6px;
  26.   right:6px;
  27.   bottom:6px;
  28.   background-size: cover;
  29.   text-indent:-9999px;
  30.   overflow:hidden;
  31. }

通过设置 animation-duration 属性来设置图片动画的持续时间,在 DEMO 中,我们设置为 1 秒。你还可以通过 animation-delay 来设置图片动画前的延时时间和通过设置 animation-iteration-count 来设置动画的重复次数。

CSS3 炫酷堆叠图片展示动画特效

已有 309 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!