jQuery实现的简单图片轮播效果完整示例


Posted in jQuery onFebruary 08, 2018

本文实例讲述了jQuery实现的简单图片轮播效果。分享给大家供大家参考,具体如下:

先来看看运行效果:

jQuery实现的简单图片轮播效果完整示例

具体代码如下:

<!DOCTYPE html>
<html>
<head>
<title>jquery实现图片轮播效果</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
#lunbo{width: 600px;height: 300px;margin: 0 auto;overflow: hidden;}
#pics{width: 600px;height: 300px;cursor: pointer;position: relative;}
ul li{width: 600px;height: 300px;list-style: none;position: absolute;top: 0;left: 0;display: none;}
img{width: 600px;height: 300px;}
</style>
</head>
<body>
<div id="lunbo">
  <ul id="pics">
    <li style="display: inline;"><img src="http://pic.qiantucdn.com/58pic/14/60/54/32n58PICxE6_1024.jpg"></li>
    <li><img src="http://pic.qiantucdn.com/58pic/19/09/94/5678b76f75315_1024.jpg"></li>
    <li><img src="http://pic.qiantucdn.com/58pic/19/39/22/01D58PICFx7_1024.jpg"></li>
    <li><img src="http://pic.qiantucdn.com/58pic/19/11/08/5679490f4892d_1024.jpg"></li>
    <li><img src="http://pic.qiantucdn.com/58pic/18/44/26/5620690acb188_1024.jpg"></li>
  </ul>
</div>
<script type="text/javascript">
$(document).ready(function(){
  var oLi = $("li");
  var liWidth = oLi.eq(0).width();
  var liHeight = oLi.eq(0).height();
   //每隔3秒进行轮播
  var timer = setInterval(change,3000);
    //鼠标放置在图片上时停止轮播,移开时继续轮播
    $("div").mouseover ( function(){
    clearInterval(timer);
  })
  $("div").mouseout (function(){
    timer = setInterval(change,3000);
  })
  //轮播函数
  var prevIndex = 0,nextIndex = 1;
  function change(){
    if (prevIndex == oLi.length-1 ) {//若当前图片是最后一张图片,下一张出现首张图片
      nextIndex = 0;
    }
    var n = Math.floor(Math.random()*3);
    if (n == 0) {
      fade(prevIndex,nextIndex);
    }
    else if (n== 2) {
      slide(prevIndex,nextIndex);
    }
    else{
      grap(prevIndex,nextIndex);
    }
    prevIndex = nextIndex;
    nextIndex ++;
  }
  //淡入淡出效果,
   function fade(prevIndex,nextIndex){
    //传入当前显示图片以及下一张图片的索引
    oLi.eq(prevIndex).fadeOut(1000);
    oLi.eq(nextIndex).fadeIn(1000);
   }
   //向左右滑动效果
   function slide(prevIndex,nextIndex){
    var rand = Math.floor(Math.random()*2);
    oLi.eq(nextIndex).show();
    oLi.eq(nextIndex).css("z-index","-1");
    if (rand) {
      //向左滑动
      oLi.eq(prevIndex).animate({left: -liWidth},1000,fun);
    }
    else{
      oLi.eq(prevIndex).animate({left: liWidth},1000,fun);
    }
    function fun(){
      oLi.eq(prevIndex).css({"left":"0","display":"none"});
      oLi.eq(nextIndex).css("z-index","1");
    }
   }
   //收缩效果
   function grap(prevIndex,nextIndex){
    var rand = Math.floor(Math.random()*4);
    oLi.eq(nextIndex).show();
    oLi.eq(nextIndex).css("z-index","-1");
    switch (rand){
      case 0://向左上角滑动
        oLi.eq(prevIndex).animate({left: -liWidth,top: -liHeight},1000,function(){
          oLi.eq(prevIndex).css({"left":"0","top": "0","display":"none"});
          oLi.eq(nextIndex).css("z-index","1");
        });break;
      case 1://向右上角滑动
        oLi.eq(prevIndex).animate({left: liWidth,top: -liHeight},1000,function(){
          oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"});
          oLi.eq(nextIndex).css("z-index","1");
        });break;
      case 2://向右下角滑动
        oLi.eq(prevIndex).animate({left: liWidth,top: liHeight},1000,function(){
          oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"});
          oLi.eq(nextIndex).css("z-index","1");
        });break;
      case 3://向左下角滑动
        oLi.eq(prevIndex).animate({left: -liWidth,top: liHeight},1000,function(){
          oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"});
          oLi.eq(nextIndex).css("z-index","1");
        });break;
      default:break;
    }
   }
  });
</script>
</body>
</html>

希望本文所述对大家jQuery程序设计有所帮助。

jQuery 相关文章推荐
jquery实现tab选项卡切换效果(悬停、下方横线动画位移)
May 05 jQuery
jQuery查找dom的几种方法效率详解
May 17 jQuery
最常用的jQuery表单验证(简单)
May 23 jQuery
Jquery EasyUI $.Parser
Jun 02 jQuery
jQuery常用选择器详解
Jul 17 jQuery
jquery实现下拉菜单的手风琴效果
Jul 23 jQuery
基于jquery实现多级菜单效果
Jul 25 jQuery
使用jQuery实现页面定时弹出广告效果
Aug 24 jQuery
jQuery使用each遍历循环的方法
Sep 19 jQuery
IE8中jQuery.load()加载页面不显示的原因
Nov 15 jQuery
Jquery滑动门/tab切换实现方法完整示例
Jun 05 jQuery
JS实现选项卡插件的两种写法(jQuery和class)
Dec 30 jQuery
jQuery 防止相同的事件快速重复触发方法
Feb 08 #jQuery
jQuery实现动态显示select下拉列表数据的方法
Feb 05 #jQuery
jQuery Dom元素操作技巧
Feb 04 #jQuery
jQuery niceScroll滚动条错位问题的解决方法
Feb 03 #jQuery
jQuery实现的下雪动画效果示例【附源码下载】
Feb 02 #jQuery
jquery.picsign图片标注组件实例详解
Feb 02 #jQuery
jquery应用实例分享_实现手风琴特效
Feb 01 #jQuery
You might like
eWebEditor v3.8 商业完整版 (PHP)
2006/12/06 PHP
php通过session防url攻击方法
2014/12/10 PHP
PHP中trait使用方法详细介绍
2017/05/21 PHP
自适应图片大小的弹出窗口
2006/07/27 Javascript
理解Javascript_08_函数对象
2010/10/15 Javascript
jQuery中jqGrid分页实现代码
2011/11/04 Javascript
js中同步与异步处理的方法和区别总结
2013/12/25 Javascript
JQuery中ajax方法访问web服务实例
2015/07/18 Javascript
js利用正则表达式检验输入内容是否为网址
2016/07/05 Javascript
完全深入学习Bootstrap表单
2016/11/28 Javascript
移动设备手势事件库Touch.js使用详解
2017/08/18 Javascript
vue下拉列表功能实例代码
2018/04/08 Javascript
使用layer弹窗和layui表单实现新增功能
2018/08/09 Javascript
解决使用layui对select append元素无效或者未及时更新的问题
2019/09/18 Javascript
JS创建自定义对象的六种方法总结
2020/12/15 Javascript
Python中异常重试的解决方案详解
2017/05/05 Python
Python实现Logger打印功能的方法详解
2017/09/01 Python
Python实现返回数组中第i小元素的方法示例
2017/12/04 Python
Python实现使用卷积提取图片轮廓功能示例
2018/05/12 Python
利用pandas进行大文件计数处理的方法
2018/07/25 Python
详解关于Django中ORM数据库迁移的配置
2018/10/08 Python
对python中的try、except、finally 执行顺序详解
2019/02/18 Python
Python去除字符串前后空格的几种方法
2019/03/04 Python
详解Python 切片语法
2019/06/10 Python
Flask框架中request、请求钩子、上下文用法分析
2019/07/23 Python
Django中ajax发送post请求 报403错误CSRF验证失败解决方案
2019/08/13 Python
python读文件的步骤
2019/10/08 Python
Python中的__init__作用是什么
2020/06/09 Python
西班牙在线光学:Visual-Click
2020/06/22 全球购物
党的群众路线教育实践活动党员个人剖析材料
2014/10/08 职场文书
教师党员批评与自我批评发言稿
2014/10/15 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
创业计划书之溜冰场
2019/10/25 职场文书
HTML5+CSS+JavaScript实现捉虫小游戏设计和实现
2021/10/16 HTML / CSS
mysql中整数数据类型tinyint详解
2021/12/06 MySQL
MySQL中rank() over、dense_rank() over、row_number() over用法介绍
2022/03/23 MySQL