最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

jquery版轮播图效果和extend扩展实例分享

来源:动视网 责编:小OO 时间:2020-11-27 20:08:02
文档

jquery版轮播图效果和extend扩展实例分享

具体代码如下:<;,<。DOCTYPE html>;<;html lang=";en";>;<;head>;<;meta charset=";UTF-8";>;<;title>;Document<;/title>;<;style>;*{ margin:0;padding:0;font-size:14px;-webkit-user-select:none;} ul,之前获取到的dom集合,之后再页面中HTML结构改变了,集合中的内容不会跟着自动发生变化(JS获取的元素集合有DOM映射的机制) function myBanner(selector,ajaxURL,300。
推荐度:
导读具体代码如下:<;,<。DOCTYPE html>;<;html lang=";en";>;<;head>;<;meta charset=";UTF-8";>;<;title>;Document<;/title>;<;style>;*{ margin:0;padding:0;font-size:14px;-webkit-user-select:none;} ul,之前获取到的dom集合,之后再页面中HTML结构改变了,集合中的内容不会跟着自动发生变化(JS获取的元素集合有DOM映射的机制) function myBanner(selector,ajaxURL,300。


本文主要为大家详细介绍了jquery版轮播图效果,以及extend扩展的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。

具体代码如下

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style>
 *{
 margin:0;
 padding:0;
 font-size:14px;
 -webkit-user-select:none;
 }
 ul,li{
 list-style:none;
 }
 img{
 display:block;
 border:none;
 }
 a{
 text-decoration: none;
 }
 .banner{
 position:relative;
 margin:10px auto;
 width:1000px;
 height:300px;
 overflow:hidden;
 }
 .bannerInner{
 width:100%;
 height:100%;
 background:url("../img/default.gif") no-repeat center center;
 }
 .bannerInner p{
 position:absolute;
 top:0;
 left:0;
 z-index:0;
 width:100%;
 height:100%;
 opacity: 0;
 filter:alpha(opacity=0);
 }
 .bannerInner p img{
 display:none;
 width:100%;
 height:100%;
 }
 .banner .bannerTip{
 position:absolute;
 right:20px;
 bottom:20px;
 z-index:10;
 overflow:hidden;
 }
 .banner .bannerTip li{
 float:left;
 margin-left:10px;
 width:18px;
 height:18px;
 background:lightblue;
 border-radius:50%;
 cursor:pointer;
 }
 .banner .bannerTip li.bg{
 background:orange;
 }
 .banner a{
 display:none;
 position:absolute;
 top:50%;
 margin-top:-22.5px;
 z-index:10;
 width:30px;
 height:45px;
 opacity: 0.5;
 filter:alpha(opacity=50);
 background-image:url('../img/pre.png');

 }
 .banner a.bannerLeft{
 left:20px;
 background-position:0 0;
 }
 .banner a.bannerRight{
 right:20px;
 background-position:-50px 0;
 }
 .banner a:hover{
 opacity: 1;
 filter:alpha(opacity=100);
 }
 </style>
</head>
<body>
 <p class='banner' id='bannerFir'>
 <p class='bannerInner'>
 <p><img src="" alt="" trueImg='img/banner1.jpg'></p>
 <p><img src="" alt="" trueImg='img/banner2.jpg'></p>
 <p><img src="" alt="" trueImg='img/banner3.jpg'></p>
 <p><img src="" alt="" trueImg='img/banner4.jpg'></p>
 </p>
 <ul class='bannerTip'>
 <li class='bg'></li>
 <li></li>
 <li></li>
 <li></li>
 </ul>
 <a href="javascript:;" rel="external nofollow" rel="external nofollow" class='bannerLeft'></a>
 <a href="javascript:;" rel="external nofollow" rel="external nofollow" class='bannerRight'></a>
 </p>

 <script>
 jQuery.fn.extend({
 banner:myBanner
 })
 //通过jQuery选择器或者筛选的方法获取到的jQuery集合是不存在dom映射机制的,之前获取到的dom集合,之后再页面中HTML结构改变了,集合中的内容不会跟着自动发生变化(JS获取的元素集合有DOM映射的机制)
 function myBanner(selector,ajaxURL,interval){
 var $banner = $("#"+selector);
 var $bannerInner = $banner.children(".bannerInner"),$pList = null,$imgList = null;
 var $bannerTip = $banner.children(".bannerTip"),$oLis = null
 var $bannerLeft = $banner.children(".bannerLeft"),$bannerRight = $banner.children(".bannerRight")

 //1、Ajax读取数据
 var jsonData = null;
 $.ajax({
 url:ajaxURL+"?_="+Math.random(),
 type:'get',
 dataType::"json",
 async:false,//当前的请求是同步的
 success:function(data){
 jsonData = data;

 }
 })
 //2、实现数据的绑定
 function bindData(){
 var str = "",str2 = "";
 if(jsonData){
 //原生的jsonData使用$.each()
 $.each(jsonData,function(index,item){
 str+='<p><img src="" alt="" trueImg="'+item["img"]+'"></p>';
 index===0?str2+='<li class="bg"></li>':str2+='<li></li>'
 })

 $bannerInner.html(str);
 $bannerTip.html(str2);
 $pList = $bannerInner.children("p")
 $imgList = $bannerInner.find('img')
 $oLis = $bannerTip.children("li")
 }
 }
 //3、实现图片的延迟加载
 window.setTimeout(lazyImg,500);
 function lazyImg(){
 //jquery元素集合 直接写$imgList.each()
 $imgList.each(function(index,item){
 var _this = this;
 var oImg = new Image;
 oImg.src = $(this).attr("trueImg");//$(this)等价于$(item)
 oImg.onload = function(){
 $(_this).prop('src',this.src).css("display","block")//内置属性使用prop
 }
 })
 $pList.eq(0).css("zIndex",1).animate({opacity:1},300);
 }
 //封装一个轮播图切换的效果
 function changeBanner(){
 var $curp = $pList.eq(step);
 $curp.css("zIndex",1).siblings().css("zIndex",0);
 $curp.animate({opacity:1},300,function(){
 $(this).siblings().css("opacity",0)
 })
 $oLis.eq(step).addClass("bg").siblings().removeClass('bg')
 }
 //4、实现自动轮播
 interval = interval || 3000;
 var step = 0,autoTimer = null;
 autoTimer = window.setInterval(autoMove,interval)
 function autoMove(){
 if(step === jsonData.length-1){
 step = -1;
 }
 step++;
 changeBanner();
 }
 //5、控制左右按钮的显示隐藏和自动轮播的开始和暂停
 $banner.on('mouseover',function(){
 window.clearInterval(autoTimer);
 $bannerLeft.css("display","block")
 $bannerRight.css("display","block")
 }).on('mouseout',function(){
 autoTimer = window.setInterval(autoMove,interval);
 $bannerLeft.css("display","none")
 $bannerRight.css("display","none")

 })
 //6、实现焦点切换
 $oLis.on('click',function(){
 step = $(this).index();
 changeBanner();
 })

 //7、实现左右切换
 $bannerRight.on('click',autoMove);
 $bannerLeft.on('click',function(){
 if(step===0){
 step = jsonData.length;
 }
 step--;
 changeBanner();
 });

 }


 //外部使用
 $().banner("bannerFir","json/banner.txt",1000)
 </script>
</body>
</html>

文档

jquery版轮播图效果和extend扩展实例分享

具体代码如下:<;,<。DOCTYPE html>;<;html lang=";en";>;<;head>;<;meta charset=";UTF-8";>;<;title>;Document<;/title>;<;style>;*{ margin:0;padding:0;font-size:14px;-webkit-user-select:none;} ul,之前获取到的dom集合,之后再页面中HTML结构改变了,集合中的内容不会跟着自动发生变化(JS获取的元素集合有DOM映射的机制) function myBanner(selector,ajaxURL,300。
推荐度:
标签: 分享 效果 案例
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top