首页 > 开发 > CSS > 正文

纯CSS3实现图片无间断轮播效果

2024-07-11 08:25:46
字体:
来源:转载
供稿:网友

前言:图片轮播效果现在在各大网站都是非常普遍的,以前我们都是通过postion的left or right来控制dom的移动,这里我要说的是利用css3来制作轮播!相比以前通过postion来移动dom来说,css3的实现方式更为优雅!

我相信使用过css3的童鞋们应该都知道我们是用css哪个大哥属性了吧!嗯,对的,就是translate3d我在前几天还特地写过一篇文章来介绍它,不熟悉的童鞋可以点击关键字链接过去看看!知道的童鞋们,那我们继续往下看!

效果图如下所示:

首先,我们先说下思路

非无限循环: 直接设置每次移动的位移数值,然后根据时间段执行,比如索引从0-10,直接判断是否到0了,最后是否到10了,然后到了0或者10的时候直接将索引设置我相反的索引数就可以了。

无限循环: 无限循环的就需要考虑前后的衔接了,比如第一张图跟最后一张图,在执行的最后一张图的时候应该会出现第一张图,那么相反之下出现第一张图然后往右翻的时候就应该出现最后一张图。那么这是这么做到的呢?其实很简单,那就是clone克隆最后一个dom到第一个dom的前面,然后clone第一个dom到最后一个dom的后面,可能我这么说,大家听起来有点晕。那么我们直接看看下面的代码就知道原理了!

HTML:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>   <html>  
  2. <head lang="en">       <meta charset="UTF-8">  
  3.     <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">       <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0,minimal-ui">  
  4.     <meta content="yes" name="apple-mobile-web-app-capable" />       <meta content="black" name="apple-mobile-web-app-status-bar-style" />  
  5.     <meta content="telephone=no" name="format-detection" />       <title>轮播DEMO | 科博网-钟科的个人博客</title>  
  6.     <style>           *{ padding:0; margin:0; border:0; }   
  7.         .wrap{ position: relative; height: 500px; width:360px; overflow:hidden; border:1px solid #ccc; margin:10px auto;  }            .wrap-list{ width:9999px; transform:translate3d(-360px,0,0); transition-delay: .3s; transition:ease; }   
  8.         .wrap-list img{ float:left; width:360px; height: 500px; }            .btns{ text-align:center; }   
  9.         .btns button{ width:100px; height: 30px; }        </style>  
  10. </head>   <body>  
  11. <div class="wrap">       <div class="wrap-list">  
  12.         <!--这里需要clone-->           <img src="http://img.mrco.cn/PKUgbaXooTzIz4TZQl7heAGF.jpg">  
  13.         <img src="http://img.mrco.cn/VQeqlUbGMLcGqpy-QnDjzyZn.jpg">           <img src="http://img.mrco.cn/E32rcqdZn0uMt9JbXr0w0K95.jpg">  
  14.         <img src="http://img.mrco.cn/uWHhrhupbMphjzsYtS7IRSD_.jpg">           <img src="http://img.mrco.cn/k2wZVNRo0YNU7i-wuC_-84Du.jpg">  
  15.         <img src="http://img.mrco.cn/PKUgbaXooTzIz4TZQl7heAGF.jpg">           <!--这里需要clone-->  
  16.         <img src="http://img.mrco.cn/VQeqlUbGMLcGqpy-QnDjzyZn.jpg">       </div>  
  17. </div>   <div class="btns">  
  18.     <button id="btnLeft">左</button>       <button id="btnRight">右</button>  
  19. </div>   <script src="http://s.mrco.cn/scripts/libs/zepto/zepto.min.js?0.0.1"></script>  
  20. <script src="http://s.mrco.cn/scripts/libs/zepto/selector.js?0.0.1"></script>   <script src="http://s.mrco.cn/scripts//libs/zepto/touch.js?0.0.1"></script>  
  21. </body>   </html>  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表