两种js实现轮播图的方式

本文主要为大家详细介绍了js实现轮播图的两种方式,一是构造函数、另一种是面向对象方式方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。

1、构造函数

nbsp;html>          *{ margin:0; padding:0;}      #wrap{    width:500px;    height:360px;    margin:100px auto;    position:relative;   }   #pic{    width:500px;    height:360px;    position:relative;   }   #pic img{    width: 100%;    height: 100%;    position:absolute;    top:0;    left:0;    display:none;   }   #tab{    width:105px;    height:10px;    position:absolute;    bottom:10px;    left:50%;    margin-left:-50px;   }   #tab ul li{    width:10px;    height:10px;    margin:0 5px;    background:#bbb;    border-radius:100%;    cursor:pointer;    list-style:none;    float:left;   }   #tab ul li.on{ background:#f60;}   #btn p{    width:40px;    height:40px;    position:absolute;    top:50%;    margin-top:-20px;    color:#fff;    background:#999;    background:rgba(0,0,0,.5);    font-size:20px;    font-weight:bold;    font-family:'Microsoft yahei';    line-height:40px;    text-align:center;    cursor:pointer;   }   #btn p#left{ left:0;}   #btn p#right{ right:0;}      

   

    两种js实现轮播图的方式    两种js实现轮播图的方式    两种js实现轮播图的方式    两种js实现轮播图的方式   

   

    

登录后复制登录后复制                              

    

    

>

        var oWrap=document.getElementById('wrap') var picImg=document.getElementById('pic').getElementsByTagName('img'); var tabLi=document.getElementById('tab').getElementsByTagName('li'); var btnp=document.getElementById('btn').getElementsByTagName('p'); var index=0; var timer=null;//设置一个timer变量,让他的值为空 //初始化 picImg[0].style.display='block'; tabLi[0].className='on'; for(var i=0;i<tabLi.length;i++){ tabLi[i].index=i; tabLi[i].onclick=function(){ //不然要for循环清空 /* for(var i=0;i<tabLi.length;i++){ picImg[i].style.display='none'; tabLi[i].className=''; }*/ picImg[index].style.display='none'; //每个li都有index自定义属性 tabLi[index].className=''; index=this.index; picImg[index].style.display='block'; tabLi[index].className='on'; } }; for(var i=0;itabLi.length){index=0} //index=index%arrUrl.length; 自己取模自己等于0 alert(3%3) == 0 index%=tabLi.length;//相当于当大于tabLi.length就等于0 }else{ index–; if(index<0)index=tabLi.length-1; } picImg[index].style.display='block'; tabLi[index].className='on'; } }; auto(); oWrap.onmouseover=function(){ clearInterval(timer) } oWrap.onmouseleave=function(){ auto(); } function auto(){ timer=setInterval(function(){ //一般都是向左轮播,index++ picImg[index].style.display='none'; tabLi[index].className=''; index++; index%=tabLi.length; picImg[index].style.display='block'; tabLi[index].className='on'; },2000) };  

2、面向对象

nbsp;html>          *{ margin:0; padding:0;}      #wrap{    width:500px;    height:360px;    margin:100px auto;    position:relative;   }   #pic{    width:500px;    height:360px;    position:relative;   }   #pic img{    width: 100%;    height: 100%;    position:absolute;    top:0;    left:0;    display:none;   }   #tab{    width:105px;    height:10px;    position:absolute;    bottom:10px;    left:50%;    margin-left:-50px;   }   #tab ul li{    width:10px;    height:10px;    margin:0 5px;    background:#bbb;    border-radius:100%;    cursor:pointer;    list-style:none;    float:left;   }   #tab ul li.on{ background:#f60;}   #btn p{    width:40px;    height:40px;    position:absolute;    top:50%;    margin-top:-20px;    color:#fff;    background:#999;    background:rgba(0,0,0,.5);    font-size:20px;    font-weight:bold;    font-family:'Microsoft yahei';    line-height:40px;    text-align:center;    cursor:pointer;   }   #btn p#left{ left:0;}   #btn p#right{ right:0;}      

   

    两种js实现轮播图的方式    两种js实现轮播图的方式    两种js实现轮播图的方式    两种js实现轮播图的方式   

   

    

登录后复制登录后复制                              

    

    

>

        var oWrap=document.getElementById('wrap') var picImg=document.getElementById('pic').getElementsByTagName('img'); var tabLi=document.getElementById('tab').getElementsByTagName('li'); var btnp=document.getElementById('btn').getElementsByTagName('p'); function Banner(oWrap,picImg,tabLi,btnp){ this.wrap=oWrap this.list=picImg this.tab=tabLi this.btn=btnp this.index=0; //这些都必须是私有的,不然两个banner会一样 this.timer=null; this.length=this.tab.length; // this.init();//下面创建好,要在这里执行 } //初始化分类 Banner.prototype.init=function(){ //先把下面的分类 var This=this; //var 一个This变量把this存起来 this.list[0].style.display='block'; this.tab[0].className='on'; for(var i=0;i<this.length;i++){ this.tab[i].index=i; this.tab[i].onclick=function(){ //this.list[index].style.display='none'; 这里的this指向tab的this This.list[This.index].style.display='none'; This.tab[This.index].className=''; //index=this.index; This.index=this.index; This.list[This.index].style.display='block'; //This.tab[This.index].className='on'; this.className='on'; } }; for(var i=0;i<this.btn.length;i++){ this.btn[i].index=i; this.btn[i].onselectstart=function(){ return false; } this.btn[i].onclick=function(){ This.list[This.index].style.display='none'; This.tab[This.index].className=''; if(this.index){ This.index++; This.index%=This.length; }else{ This.index–; if(index<0)This.index=This.length-1; } This.list[This.index].style.display='block'; This.tab[This.index].className='on'; } } this.auto(); this.clear(); }; Banner.prototype.auto=function(){ var This=this; This.timer=setInterval(function(){ //一般都是向左轮播,index++ This.list[This.index].style.display='none'; This.tab[This.index].className=''; This.index++; This.index%=This.length; This.list[This.index].style.display='block'; This.tab[This.index].className='on'; },2000) }; Banner.prototype.clear=function(){ var This=this; this.wrap.onmouseover=function(){ clearInterval(This.timer) } this.wrap.onmouseleave=function(){ This.auto(); } }; var banner1=new Banner(oWrap,picImg,tabLi,btnp); banner1.init(); /* * init() * function init(){ for(var i=0;i<tabLi.length;i++){ tabLi[i].index=i; tabLi[i].onclick=function(){ picImg[index].style.display='none'; tabLi[index].className=''; index=this.index; picImg[index].style.display='block'; tabLi[index].className='on'; } }; } for(var i=0;i<btnp.length;i++){ btnp[i].index=i; btnp[i].onselectstart=function(){ return false; } btnp[i].onclick=function(){ picImg[index].style.display='none'; tabLi[index].className=''; if(this.index){ index++; index%=tabLi.length; }else{ index–; if(index<0)index=tabLi.length-1; } picImg[index].style.display='block'; tabLi[index].className='on'; } }; auto(); oWrap.onmouseover=function(){ clearInterval(timer) } oWrap.onmouseleave=function(){ auto(); } function auto(){ timer=setInterval(function(){ //一般都是向左轮播,index++ picImg[index].style.display='none'; tabLi[index].className=''; index++; index%=tabLi.length; picImg[index].style.display='block'; tabLi[index].className='on'; },2000) }; */  

相关推荐:

封装运动框架实战之滑动的焦点轮播图讲解

js实现移动端手指滑动轮播图效果

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

以上就是两种js实现轮播图的方式的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2788572.html

(0)
上一篇 2025年3月8日 18:28:26
下一篇 2025年3月8日 18:28:38

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • Easy UI点击文字实现展开关闭功能

    本文主要介绍了easy ui动态树点击文字实现展开关闭功能,需要的朋友可以参考下,希望能帮助到大家。 只需要在JSP处,点击树的函数中,添加一段代码即可: 整体如下:  $(“#tt”).tree({     onClick: functi…

    编程技术 2025年3月8日
    000
  • JS实现div点击跳转到另一页面实例代码

    本文主要介绍了js动态添加的p点击跳转到另一页面实现代码,需要的朋友可以参考下,希望能帮助到大家。  p调用函数跳转: var obj = document.getElementById(‘id’);obj.onclick=function…

    编程技术 2025年3月8日
    200
  • vuejs使用$emit和$on进行组件间的传值

    $emit和$on可以实现组件之间的传值,我们知道父组件传值给子组件使用props,但是不允许子组件传值给父组件,这时候使用这个就可以实现了。本文主要介绍了vuejs使用$emit和$on进行组件之间的传值的示例,具有一定的参考价值,有兴趣…

    2025年3月8日
    200
  • JS数据访问对象模式详解

    本文主要为大家带来一篇js设计模式之数据访问对象模式的实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。 问题 大部分网站会将一些数据(如:用户Token)存储在前端,用来实现页面间的…

    编程技术 2025年3月8日
    200
  • D3.js创建物流地图实例分享

    本文主要介绍了使用d3.js创建物流地图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。 示例图 制作思路 需要绘制一张中国地图,做为背景。 需要主要城市的经纬坐标,以绘制路张起点和…

    2025年3月8日
    200
  • 微信小程序翻牌小游戏的实现

    本文主要为大家详细介绍了微信小程序版翻牌小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。 一、新建一个quick start项目看看结构 在微信开发工具点击添加项目,选择 无appid,勾上”在当前目…

    编程技术 2025年3月8日
    200
  • javascript定时器实现进度条功能

    本文主要介绍javascript基于定时器实现进度条功能,简单分析了javascript定时器的功能、使用方法并给出了基于定时器实现的进度条功能实例,需要的朋友可以参考下,希望能帮助到大家。 Javascript 中的定时器 window度…

    2025年3月8日
    200
  • layer前端组件实现图片显示功能方法

    本文主要介绍了layui中layer前端组件实现图片显示功能的方法,结合实例形式分析了layui中layer组件调用图片显示功能的操作方法与相关注意事项,并提供了layer与layui源码下载,需要的朋友可以参考下,希望能帮助到大家。 实现…

    编程技术 2025年3月8日
    200
  • JS上传大文件显示进度条实现方法

    本文主要为大家详细介绍js原生上传大文件显示进度条,php上传文件关键代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。 在php.ini修改需要的大小: upload_max_filesize = 8M    po…

    2025年3月8日
    200
  • JavaScript随机数生成器实现方法

    本文主要为大家详细介绍了javascript实现随机数生成器,生成不重复的随机数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。 1.实验前准备: Math函数的理解 数组方法的理解 2.实验操作: 立即学习“Java…

    2025年3月8日
    200

发表回复

登录后才能评论