幻灯片/轮播

jQuery响应式图片无缝轮播插件

阿里云


这是一款简单实用的 jQuery 响应式图片无缝轮播插件。该插件会将要轮播的图片制作为背景图片,它支持 ie8+浏览器,具有无限、无缝轮播等特点。

HTML 结构:

该轮播图插件的基本 HTML 结构如下:

也想出现在这里?联系我们
创客主机
  1. <div class="banner">
  2.     <ul>
  3.     </ul>
  4.     <ol>
  5.     </ol>
  6.     <i class="left"></i><i class="right"></i>
  7. </div>

CSS 样式:

使用下面的 CSS 代码来制作轮播图的全屏响应式效果。

  1. .banner {
  2.   width: 100%;
  3.   position: relative;
  4.   overflow: hidden;
  5. }
  6.  
  7. .banner ul { position: absolute; }
  8.  
  9. .banner ul li {
  10.   width: 100%;
  11.   height: 560px;
  12.   float: left;
  13.   background: no-repeat center center;
  14. }

圆点导航和箭头导航按钮的样式如下:

  1. .banner ol {
  2.   height: 20px;
  3.   background: rgba(0,0,0,0.5);
  4.   position: absolute;
  5.   left: 50%;
  6.   bottom: 30px;
  7.   padding: 0 10px;
  8.   border-radius: 10px;
  9. }
  10.  
  11. .banner ol li {
  12.   width: 10px;
  13.   height: 10px;
  14.   float: left;
  15.   margin: 5px 5px;
  16.   background: rgba(255,255,255,0.5);
  17.   border-radius: 50%;
  18.   cursor: pointer;
  19. }
  20.  
  21. .banner ol .current { background: rgba(255,255,255,1); }
  22.  
  23. .banner i {
  24.   width: 58px;
  25.   height: 120px;
  26.   position: absolute;
  27.   top: 50%;
  28.   margin-top: -60px;
  29.   cursor: pointer;
  30.   border-radius: 5px;
  31.   display: none;
  32. }
  33.  
  34. .banner .left {
  35.   left: 60px;
  36.   background: url('left_right.png') no-repeat 0 0px;
  37. }
  38.  
  39. .banner .right {
  40.   right: 60px;
  41.   background: url('left_right.png') no-repeat 0px -120px;
  42. }
  43.  
  44. .banner .left:hover, .banner .right:hover { background-color: rgba(0, 0, 0, 0.31); }

JavaScript:

在页面中引入 jquery,实现该轮播图效果的 jquery 代码如下:

  1. $(function(){ //页面加载完毕才执行
  2.  
  3.     //=========设置参数==========
  4.     //图片统一高度:
  5.     var images_height = '560px';
  6.     //图片路径/链接(数组形式):
  7.     var images_url = [
  8.         'img/1.jpg',
  9.         'img/2.jpg',
  10.         'img/3.jpg'
  11.     ];
  12.     var images_count = images_url.length;
  13.     //console.log(images_count);
  14.  
  15.     //创建节点
  16.     //图片列表节点
  17.     for(var j=0;j<images_count+1;j++){
  18.         $('.banner ul').append('<li></li>')
  19.     }
  20.     //轮播圆点按钮节点
  21.     for(var j=0;j<images_count;j++){
  22.         if(j==0){
  23.             $('.banner ol').append('<li class="current"></li>')
  24.         }else{
  25.             $('.banner ol').append('<li></li>')
  26.         }
  27.     }
  28.  
  29.     //载入图片
  30.     $('.banner ul li').css('background-image','url('+images_url[0]+')');
  31.     $.each(images_url,function(key,value){
  32.         $('.banner ul li').eq(key).css('background-image','url('+value+')');
  33.     });
  34.  
  35.     $('.banner').css('height',images_height);
  36.  
  37.     $('.banner ul').css('width',(images_count+1)*100+'%');
  38.  
  39.     $('.banner ol').css('width',images_count*20+'px');
  40.     $('.banner ol').css('margin-left',-images_count*20*0.5-10+'px');
  41.  
  42.     //=========================
  43.  
  44.     var num = 0;
  45.     //获取窗口宽度
  46.     var window_width = $(window).width();
  47.     $(window).resize(function(){
  48.         window_width = $(window).width();
  49.         $('.banner ul li').css({width:window_width});
  50.         clearInterval(timer);
  51.         nextPlay();
  52.         timer = setInterval(nextPlay,2000);
  53.     });
  54.     //console.log(window_width);
  55.     $('.banner ul li').width(window_width);
  56.     //轮播圆点
  57.     $('.banner ol li').mouseover(function(){//用hover的话会有两个事件(鼠标进入和离开)
  58.         $(this).addClass('current').siblings().removeClass('current');
  59.         //第一张图: 0 * window_width
  60.         //第二张图: 1 * window_width
  61.         //第三张图: 2 * window_width
  62.         //获取当前编号
  63.         var i = $(this).index();
  64.         //console.log(i);
  65.         $('.banner ul').stop().animate({left:-i*window_width},500);
  66.         num = i;
  67.     });
  68.     //自动播放
  69.     var timer = null;
  70.     function prevPlay(){
  71.         num--;
  72.         if(num<0){
  73.             //悄悄把图片跳到最后一张图(复制页,与第一张图相同),然后做出图片播放动画,left参数是定位而不是移动的长度
  74.             $('.banner ul').css({left:-window_width*images_count}).stop().animate({left:-window_width*(images_count-1)},500);
  75.             num=images_count-1;
  76.         }else{
  77.             //console.log(num);
  78.             $('.banner ul').stop().animate({left:-num*window_width},500);
  79.         }
  80.         if(num==images_count-1){
  81.             $('.banner ol li').eq(images_count-1).addClass('current').siblings().removeClass('current');
  82.         }else{
  83.             $('.banner ol li').eq(num).addClass('current').siblings().removeClass('current');
  84.  
  85.         }
  86.     }
  87.     function nextPlay(){
  88.         num++;
  89.         if(num>images_count){
  90.             //播放到最后一张(复制页)后,悄悄地把图片跳到第一张,因为和第一张相同,所以难以发觉,
  91.             $('.banner ul').css({left:0}).stop().animate({left:-window_width},500);
  92.             //css({left:0})是直接悄悄改变位置,animate({left:-window_width},500)是做出移动动画
  93.             //随后要把指针指向第二张图片,表示已经播放至第二张了。
  94.             num=1;
  95.         }else{
  96.             //在最后面加入一张和第一张相同的图片,如果播放到最后一张,继续往下播,悄悄回到第一张(肉眼看不见),从第一张播放到第二张
  97.             //console.log(num);
  98.             $('.banner ul').stop().animate({left:-num*window_width},500);
  99.         }
  100.         if(num==images_count){
  101.             $('.banner ol li').eq(0).addClass('current').siblings().removeClass('current');
  102.         }else{
  103.             $('.banner ol li').eq(num).addClass('current').siblings().removeClass('current');
  104.  
  105.         }
  106.     }
  107.     timer = setInterval(nextPlay,2000);
  108.     //鼠标经过banner,停止定时器,离开则继续播放
  109.     $('.banner').mouseenter(function(){
  110.         clearInterval(timer);
  111.         //左右箭头显示(淡入)
  112.         $('.banner i').fadeIn();
  113.     }).mouseleave(function(){
  114.         timer = setInterval(nextPlay,2000);
  115.         //左右箭头隐藏(淡出)
  116.         $('.banner i').fadeOut();
  117.     });
  118.     //播放下一张
  119.     $('.banner .right').click(function(){
  120.         nextPlay();
  121.     });
  122.     //返回上一张
  123.     $('.banner .left').click(function(){
  124.         prevPlay();
  125.     });
  126. });

Github 地址:https://github.com/maizhenying09/jquery-seamless-picture-carousel

jQuery 响应式图片无缝轮播插件

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

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

发表回复

热销模板

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

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