JS+css3实现幻灯片轮播图


Posted in Javascript onAugust 14, 2020

本文实例为大家分享了JS+css3实现幻灯片轮播图的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style>
   *{
    margin: 0;
    padding: 0;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
   }
   .clearfix:after{
    clear: both;
   }
   .clearfix:after,.clearfix:before{
    content: "";
    display: table;
   }
   .slide_view{
    width: 600px;
    height: 200px;
    overflow: hidden;
    margin: 40px auto;
    position: relative;
   }
   ul{
    width: 600px;
    height: 100%;
   }
   li{
    position: absolute;
    width: 600px;
    height:100%;
    opacity: 0;
   }
   li.active{
    opacity: 1;
   }
   
   .hor-slide-ani .next-out
   {
    animation: hor-slide-next-out .8s forwards cubic-bezier(0.7, 0, 0.3, 1);
   }
   .hor-slide-ani .next-in{
    animation: hor-slide-next-in .8s forwards cubic-bezier(0.7, 0, 0.3, 1);
   }
   
   .hor-slide-ani .prev-out
   {
    animation: hor-slide-prev-out .8s forwards cubic-bezier(0.7, 0, 0.3, 1);
   }
   .hor-slide-ani .prev-in{
    animation: hor-slide-prev-in .8s forwards cubic-bezier(0.7, 0, 0.3, 1);
   }
   @keyframes hor-slide-next-out{
    from{
     opacity: 1;
    }
    to{
     opacity: 1;
     transform: translateX(100%);
    }
   }
   
   @keyframes hor-slide-next-in{
    from{
     opacity: 1;
     transform: translateX(-100%);
    }
    to{
     opacity: 1;
     transform: translateX(0);
    }
   }
   @keyframes hor-slide-prev-out{
    from{
     opacity: 1;
    }
    to{
     opacity: 1;
     transform: translateX(-100%);
    }
   }
   
   @keyframes hor-slide-prev-in{
    from{
     opacity: 1;
     transform: translateX(100%);
    }
    to{
     opacity: 1;
     transform: translateX(0);
    }
   }
   .prev{
    position: absolute;
    left: 10px;
    top: 40%;
    display: block;
    padding: 10px;
    text-align: center;
    width: 20px;
    height: 20px;
    border-radius: 100%;
    background: rgba(0,0,0,.4);
    color: white;
    font-size: 22px;
    line-height: 22px;
   }
   .next{
    position: absolute;
    right: 10px;
    top: 40%;
    display: block;
    padding: 10px;
    text-align: center;
    width: 20px;
    height: 20px;
    border-radius: 100%;
    background: rgba(0,0,0,.4);
    color: white;
    font-size: 22px;
    line-height: 22px;
   }
  </style>
 </head>
 <body>
  <div class="slide_view">
   <ul class="slides clearfix hor-slide-ani" style="position: relative;">
    <li class="active" style="background: salmon;">1</li>
    <li style="background: darkcyan;">2</li>
    <li style="background: seagreen;">3</li>
    <li style="background: sandybrown;">4</li>
   </ul>
   <div class="control">
    <span class="prev">←</span>
    <span class="next">→</span>
   </div>
  </div>
</body>
<script type="text/javascript" src="js/jquery-2.1.4.min.js" ></script>
  <script>
   var aniName = (function(el) {
    var animations = {
    animation: 'animationend',
    OAnimation: 'oAnimationEnd',
    MozAnimation: 'mozAnimationEnd',
    WebkitAnimation: 'webkitAnimationEnd',
    };
  
    for (var t in animations) {
    if (el.style[t] !== undefined) {
     return animations[t];
    }
    }
    return false;
   })(document.createElement('div'));
   
   var aniEndCallback=function($ele,endCall){
    if(aniName && typeof endCall == 'function'){
     var called=false;
     //在每次transitionEnd的事件后执行该函数
     var callback = function(){ 
      if (!called){
       called=true;
       endCall($ele);
      } 
     };
     $ele[0].addEventListener(aniName,function(){
      callback();
      //通过setTimeout来补救windowphone中不触发事件的问题
      setTimeout(callback,200);
     },false);
    }else{
     endCall($ele);
    }   
   };
   
   
   
   $(function(){
    var aniStatus = false;
    $('.next').click(function(){
     if(aniStatus){return};
     aniStatus = true;
     var $slides = $('.slides').children()
     , slideCount = $slides.length
     , $active = $('.active')
     , curActiveIndex = $('.active').index()
     , nextActiveIndex = curActiveIndex -1;
     if(curActiveIndex == 0){
      nextActiveIndex = slideCount-1;
     }
     $slides.eq(curActiveIndex).addClass('next-out');
     $slides.eq(nextActiveIndex).addClass('next-in');
     
     aniEndCallback($active,function($ele){
      aniStatus = false;
      $active.removeClass('next-out active');
      $slides.eq(nextActiveIndex).removeClass('next-in').addClass('active');
     });
    });
    
    $('.prev').click(function(){
     if(aniStatus){return;}//不在动画状态,才能执行
     aniStatus= true;
     var $slides = $('.slides').children()
      , slideCount = $slides.length
      , $active = $('.active')
      , curActiveIndex = $('.active').index()
      , nextActiveIndex = curActiveIndex + 1;
      if(curActiveIndex == slideCount-1){
       nextActiveIndex = 0;
      }
      $slides.eq(curActiveIndex).addClass('prev-out');
      $slides.eq(nextActiveIndex).addClass('prev-in');
      
     aniEndCallback($active,function($ele){
      aniStatus = false;
      $active.removeClass('prev-out active');
      $slides.eq(nextActiveIndex).removeClass('prev-in').addClass('active');
     });
    });
    
    setInterval(function(){
     $('.prev').trigger('click')
    },4000);
   });

精彩专题分享:jQuery图片轮播 JavaScript图片轮播 Bootstrap图片轮播

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript多种数据类型表格排序代码分析
Sep 11 Javascript
JavaScript获取并更改input标签name属性的方法
Jul 02 Javascript
异步JS框架的作用以及实现方法
Oct 29 Javascript
javascript运动效果实例总结(放大缩小、滑动淡入、滚动)
Jan 08 Javascript
基于javascript实现全国省市二级联动下拉选择菜单
Jan 28 Javascript
jQuery包裹节点用法完整示例
Sep 13 Javascript
JS操作xml对象转换为Json对象示例
Mar 25 Javascript
使用JavaScript进行表单校验功能
Aug 01 Javascript
Angular4开发解决跨域问题详解
Aug 28 Javascript
vue页面离开后执行函数的实例
Mar 13 Javascript
Angular封装搜索框组件操作示例
Apr 25 Javascript
JavaScript实现轮播图效果代码实例
Sep 28 Javascript
浅谈vue获得后台数据无法显示到table上面的坑
Aug 13 #Javascript
vue 解决data中定义图片相对路径页面不显示的问题
Aug 13 #Javascript
解决vue net :ERR_CONNECTION_REFUSED报错问题
Aug 13 #Javascript
vue用elementui写form表单时,在label里添加空格操作
Aug 13 #Javascript
jQuery中event.target和this的区别详解
Aug 13 #jQuery
在Vue 中获取下拉框的文本及选项值操作
Aug 13 #Javascript
vue自动添加浏览器兼容前后缀操作
Aug 13 #Javascript
You might like
PHP 缓存实现代码及详细注释
2010/05/16 PHP
PHP+MySQL投票系统的设计和实现分享
2012/09/23 PHP
CodeIgniter启用缓存和清除缓存的方法
2014/06/12 PHP
php强制更新图片缓存的方法
2015/02/11 PHP
php实现中文字符截取防乱码方法汇总
2015/04/29 PHP
10条php编程小技巧
2015/07/07 PHP
PHP 的比较运算与逻辑运算详解
2016/05/12 PHP
基于jQuery的倒计时插件代码
2011/05/07 Javascript
使用js显示当前时间示例
2014/03/02 Javascript
javascript判断移动端访问设备并解析对应CSS的方法
2015/02/05 Javascript
javascript实现控制div颜色
2015/07/07 Javascript
javascript事件绑定学习要点
2016/03/09 Javascript
WebView启动支付宝客户端支付失败的问题小结
2017/01/11 Javascript
Angular2入门教程之模块和组件详解
2017/05/28 Javascript
使用ajax的post同步执行(实现方法)
2017/12/21 Javascript
原生JS实现轮播图效果
2018/10/12 Javascript
[01:36:19]Secret vs NB 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
python中的内置函数getattr()介绍及示例
2014/07/20 Python
跟老齐学Python之不要红头文件(2)
2014/09/28 Python
python实现支持目录FTP上传下载文件的方法
2015/06/03 Python
Python实现批量修改文件名实例
2015/07/08 Python
详解Python中表达式i += x与i = i + x是否等价
2017/02/08 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
2019/05/06 Python
jupyter notebook tensorflow打印device信息实例
2020/04/20 Python
详解python中GPU版本的opencv常用方法介绍
2020/07/24 Python
Html5踩坑记之mandMobile使用小记
2020/04/02 HTML / CSS
会计与审计专业自荐信范文
2014/03/15 职场文书
《狼和小羊》教学反思
2014/04/20 职场文书
读书小明星事迹材料
2014/05/03 职场文书
药店促销活动总结
2014/07/10 职场文书
领导班子党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
怀孕辞职信怎么写
2015/02/28 职场文书
观后感的写法
2015/06/19 职场文书
2016入党积极分子心得体会
2016/01/06 职场文书
2019送给家人们的中秋节祝福语
2019/08/15 职场文书
JavaScript架构localStorage特殊场景下二次封装操作
2022/06/21 Javascript