jQuery实现经典的网页3D轮播图封装功能【附源码下载】


Posted in jQuery onFebruary 15, 2019

本文实例讲述了jQuery实现的网页3D轮播图封装功能。分享给大家供大家参考,具体如下:

网页伪3D轮播图,其实就是轮播图旋转木马效果。其实在jquery插件库也有很多旋转木马的插件,但是博主封装的这个新的插件比起以上的都适应性更好。其适应性好表现在:调用灵活性高用法更简单,css样式都封装好了基本不用写,在body里面写ul>li>img标签即可,可设置参数多,甚至不同图片的大小都可以自适应轮播,各个浏览器兼容性好(包括IE,虽然我没测过IE8以下浏览器,不过IE8以上都没问题),好了,以下看代码和用法。

head引入两个文件,第一个是jquery的插件(这是1.11.0版本,当然其他版本也可以哦,不过低版本的相对IE兼容性更好),第二个是我封装好的javascript脚本

<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script src="js/Figure_3D.js" type="text/javascript" charset="utf-8"></script>

body里面插入轮播图,ul的id要为pic_play,当然我这里是7张图,你也可以增加减少图片,但是这里有一个重要问题是,图片数量要为单数,不能为偶数,否则会出问题,这是一个小小的bug。当然你也可以在li里面写<a><img src/></a>也是可以的。

<ul id="pic_play">
  <li><img src="img/dc1.jpg"/></li>
  <li><img src="img/dc2.jpg"/></li>
  <li><img src="img/dc3.jpg"/></li>
  <li><img src="img/dc4.jpg"/></li>
  <li><img src="img/dc5.jpg"/></li>
  <li><img src="img/dc6.jpg"/></li>
  <li><img src="img/dc7.jpg"/></li>
</ul>

css基本不用写,不过要习惯性给padding和margin归0

*{
  margin: 0;
  padding: 0;
  list-style: none;
}

重要封装脚本Figure_3D.js

//    ulwidth:父节点ul总宽度(number),
//    height:ul和图片的初始化最大高度(中间)(number),
//    liwidth:图片的初始化最大宽度(中间)(number),
//    minopacity:图片最小透明度(0~1),
//    minscale:图片最小缩放系数(0~1),
//    direction:默认轮播方向("left","right"),
//    speed:动画速度(number),
//    delayed:每张图片延时停留时间(number)(注:delayed>=speed),
//    mousestop:鼠标经过是否停止(false,true),
function init(ulwidth,height,liwidth,minopacity,minscale,direction,speed,delayed,mousestop){
  $("#pic_play").css({"position":"relative","width":ulwidth,"height":height});
  $("#pic_play>li").css({"position":"absolute","width":liwidth,"height":height});
  if($("#pic_play>li>a").size()>0){
    $("#pic_play>li>a").css("display","block");
  }
  if($("#pic_play>li>a>img").size()>0){
    $("#pic_play>li>a>img").css({"display":"block","width":"100%","height":"100%"});
  }else if($("#pic_play>li>img").size()>0){
    $("#pic_play>li>img").css({"display":"block","width":"100%","height":"100%"});
  }else return;
  var len=$("#pic_play>li").size();
  jsonstyle=[];
  var display=-1;
  var minopacity=minopacity;
  var opacitystep=(1-minopacity)/(Math.floor(len/2)-1);
  var minscale=minscale;
  var scalestep=(1-minscale)/(Math.floor(len/2));
  var Dvalue=Math.round((($("#pic_play").innerWidth()-$("#pic_play>li").eq(0).outerWidth())/2)/Math.floor(len/2));
  $("#pic_play>li").each(function(i){
    if(i<Math.floor(len/2)){
      var realoff=i*Dvalue;
      display++;
      var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth());
      var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight());
      var realtop=Math.round(($("#pic_play>li").eq(Math.floor(len/2)).outerHeight()-realheight)/2);
      jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":minopacity,"realWidth":realwidth,"realHeight":realheight,"realTop":realtop};
      $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":minopacity,"width":realwidth,"height":realheight,"top":realtop});
      minopacity+=opacitystep;
      if(minopacity>=1){
        minopacity=1.0;
      }
      minscale+=scalestep;
    }else if(i==Math.floor(len/2)){
      display++
      var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth());
      var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight());
  var realoff=Math.round(($("#pic_play").innerWidth()-$("#pic_play>li").eq(i).outerWidth())/2);
      jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":1,"realWidth":realwidth,"realHeight":realheight,"realTop":0};
      $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":1,"width":realwidth,"height":realheight,"top":0});
    }else{
      display--;
      minscale-=scalestep;
      var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth());
      var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight());
      var realoff=Math.round($("#pic_play").innerWidth()-(realwidth+(len-1-i)*Dvalue));
      var realtop=Math.round(($("#pic_play>li").eq(Math.floor(len/2)).outerHeight()-realheight)/2);
      jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":minopacity,"realWidth":realwidth,"realHeight":realheight,"realTop":realtop};
      $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":minopacity,"width":realwidth,"height":realheight,"top":realtop});
      minopacity-=opacitystep;
    }
  });
  animationPlay(direction,speed,delayed);
  if(mousestop==true){
    animationStop(direction,speed,delayed);
  }
}
//offset:左右按钮分别距离ul左右边距(number),
//top:左右按钮距离ul上边距(number),
//direction:默认轮播方向("left","right"),一般和init里的一样,如果你不想点击后改变运动方向,
//speed:动画速度(number),一般和init里的一样,如果你不想点击后改变动画速度,
//delayed:每张图片延时停留时间(number)(注:delayed>=speed),一般和init里的一样,如果你不想点击后改变延时停留时间,
function btn(offset,top,direction,speed,deleyed){
  var leftbtn=$("<span></span>");
  leftbtn.css({"width":32,"height":32,"display":"inline-block","position":"absolute","left":offset,"top":top,"background":"url(img/slider-arrow.png) no-repeat -100px 0","cursor":"pointer","z-index":100});
  var rightbtn=$("<span></span>");
  rightbtn.css({"width":32,"height":32,"display":"inline-block","position":"absolute","right":offset,"top":top,"background":"url(img/slider-arrow.png) no-repeat 0 0","cursor":"pointer","z-index":100});
  $("#pic_play").append(leftbtn);
  $("#pic_play").append(rightbtn);
  leftbtn.hover(function(){
    $(this).css("background-position","-160px 0");
  },function(){
    $(this).css("background-position","-100px 0");
  });
  rightbtn.hover(function(){
    $(this).css("background-position","-60px 0");
  },function(){
    $(this).css("background-position","0 0");
  });
  leftbtn.click(function(){
    clearInterval(timeplay);
    $("#pic_play>li").stop(true);
    var li=$("#pic_play>li").first();
    $("#pic_play").append(li);
    $("#pic_play>li").each(function(list){
      $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed);
    });
    animationPlay(direction,speed,deleyed);
  });
  rightbtn.click(function(){
    clearInterval(timeplay);
    $("#pic_play>li").stop(true);
    var li=$("#pic_play>li").last();
    $("#pic_play").prepend(li);
    $("#pic_play>li").each(function(list){
      $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed);
    });
    animationPlay(direction,speed,deleyed);
  });
}
//JSON动画
function animationPlay(direction,speed,deleyed){
  timeplay=setInterval(function(){
    if(direction.toLowerCase()=="left"){
      var li=$("#pic_play>li").first();
      $("#pic_play").append(li);
    }else if(direction.toLowerCase()=="right"){
      var li=$("#pic_play>li").last();
      $("#pic_play").prepend(li);
    }else return;
    $("#pic_play>li").each(function(list){
      $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed);
    });
  },deleyed);
}
function animationStop(direction,speed,delayed){
  $("#pic_play").mouseenter(function(){
    clearInterval(timeplay);
  });
  $("#pic_play").mouseleave(function(){
    animationPlay(direction,speed,delayed)
  });
}

脚本的调用方法,调用一个init函数初始化,定义的参数意义是:自定父节点ul总宽度(number),自定ul和图片的初始化最大高度(中间那图)(number),图片的初始化最大宽度(中间那图)(number),图片最小透明度(0~1),图片最小缩放系数(0~1),默认轮播方向("left","right"),动画速度ms(number),轮播图片延时停留时间ms(number)(注:这个参数必须大于动画速度),鼠标经过是否停止(false,true)。

$(function(){
  init(1300,600,600,0.7,0.5,"right",500,3000,false);
});

为了运行效果更为显眼,给ul加个边框居中吧。

#pic_play{
  position: relative;
  border: 1px solid black;
  margin: 30px auto;
}

看以下运行效果:这里其实每张图的大小都不一样啊,都可以自适应轮播了

jQuery实现经典的网页3D轮播图封装功能【附源码下载】

大家会发现没有左右切换的按钮?没关系,我们可以调用btn()函数。btn里面的参数意义分别是:左右按钮分别距离ul左右边距(number),左右按钮距离ul上边距(number),默认轮播方向("left","right")一般和init里的一样如果你不想点击后改变运动方向,动画速度ms(number)一般和init里的一样如果你不想点击后改变动画速度,每张图片延时停留时间ms(number)(注:这个参数必须大于动画速度)一般和init里的一样如果你不想点击后改轮播停留时间。

$(function(){
  init(1000,460,460,0.7,0.5,"right",500,3000,false);
  btn(40,230,"right",500,3000);
});

下面来看效果:

jQuery实现经典的网页3D轮播图封装功能【附源码下载】

如果想用自己的按钮款式,可以直接在我的btn封装函数里面换啊。

附:这里给出了一个完整测试实例(图片替换成了养眼的美女图片),可点击此处本站下载

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

jQuery 相关文章推荐
jQuery插件FusionCharts绘制的2D帕累托图效果示例【附demo源码】
Mar 28 jQuery
jquery实现图片上传前本地预览
Apr 28 jQuery
jQuery树插件zTree使用方法详解
May 02 jQuery
JQuery 获取Dom元素的实例讲解
Jul 08 jQuery
jQuery 实时保存页面动态添加的数据的示例
Aug 14 jQuery
基于JQuery的Ajax方法使用详解
Aug 16 jQuery
bootstrap可编辑下拉框jquery.editable-select
Oct 12 jQuery
jQuery实现的上传图片本地预览效果简单示例
Mar 29 jQuery
jQuery超简单遮罩层实现方法示例
Sep 06 jQuery
jQuery实现鼠标移入显示蒙版效果
Jan 11 jQuery
jQuery实现简单日历效果
Jul 05 jQuery
Jquery使用each函数实现遍历及数组处理
Jul 14 jQuery
详解JavaScript原生封装ajax请求和Jquery中的ajax请求
Feb 14 #jQuery
Jquery实现无缝向上循环滚动列表的特效
Feb 13 #jQuery
jquery无缝图片轮播组件封装
Nov 25 #jQuery
详解如何使用webpack打包多页jquery项目
Feb 01 #jQuery
JQuery中queue方法用法示例
Jan 31 #jQuery
AJAX在JQuery中的应用详解
Jan 30 #jQuery
jQuery访问json文件中数据的方法示例
Jan 28 #jQuery
You might like
PHP+JS无限级可伸缩菜单详解(简单易懂)
2007/01/02 PHP
PHP编程过程中需要了解的this,self,parent的区别
2009/12/30 PHP
PHP实现的连贯操作、链式操作实例
2014/07/08 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
2017/09/22 PHP
学习YUI.Ext 第六天--关于树TreePanel(Part 2异步获取节点)
2007/03/10 Javascript
JQuery从头学起第三讲
2010/07/06 Javascript
单击复制文字兼容各浏览器的完美解决方案
2013/07/04 Javascript
javascript表格的渲染组件
2015/07/03 Javascript
详解照片瀑布流效果(js,jquery分别实现与知识点总结)
2017/01/01 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
js中apply与call简单用法详解
2017/11/06 Javascript
使用3D引擎threeJS实现星空粒子移动效果
2020/09/13 Javascript
vue.extend实现alert模态框弹窗组件
2018/04/28 Javascript
Vue组件教程之Toast(Vue.extend 方式)详解
2019/01/27 Javascript
构建大型 Vue.js 项目的10条建议(小结)
2019/11/14 Javascript
利用Vue的v-for和v-bind实现列表颜色切换
2020/07/17 Javascript
jQuery实现日历效果
2020/09/11 jQuery
[47:04]EG vs RNG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
Python通过解析网页实现看报程序的方法
2014/08/04 Python
python中 logging的使用详解
2017/10/25 Python
解决pandas .to_excel不覆盖已有sheet的问题
2018/12/10 Python
pyshp创建shp点文件的方法
2018/12/31 Python
python Gunicorn服务器使用方法详解
2019/07/22 Python
修改 CentOS 6.x 上默认Python的方法
2019/09/06 Python
Python中使用gflags实例及原理解析
2019/12/13 Python
Desigual美国官方网站:西班牙服装品牌
2019/03/29 全球购物
linux面试题参考答案(9)
2016/01/29 面试题
揠苗助长教学反思
2014/02/04 职场文书
个人授权委托书
2014/09/15 职场文书
国家领导干部党的群众路线教育实践活动批评与自我批评材料
2014/09/23 职场文书
介绍长城的导游词
2015/01/30 职场文书
大学生暑期实践报告
2015/07/13 职场文书
婚宴父亲致辞
2015/07/27 职场文书
用Python爬虫破解滑动验证码的案例解析
2021/05/06 Python
Element-ui Layout布局(Row和Col组件)的实现
2021/12/06 Vue.js
尝试使用Python爬取城市租房信息
2022/04/12 Python