原生JS实现手动轮播图效果实例代码

 更新时间:2018年11月22日 14:29:05   作者:静遇。  
手动轮播图,为轮播图中的一种,轮播图主要有无缝轮播,手动轮播,延迟轮播,切换轮播等等,轮播图主要用于展现图片,新出商品,词条,又能美观网页。給网页中增加动态效果。接下来通过本文给大家分享原生JS实现手动轮播图的实例代码,一起看看吧

手动轮播图,为轮播图中的一种,轮播图主要有无缝轮播,手动轮播,延迟轮播,切换轮播等等。。。

轮播图主要用于展现图片,新出商品,词条,又能美观网页。給网页中增加动态效果。

手动轮播,是小编认为最简单的一种轮播方式,既能左右翻页,还能通过悬浮按钮,快速预览图片,所以今天就给大家写一个原生js手动轮播图。

一,利用JavaScript制作手动轮播图,首先排版。

引入默认样式表(可以手写);

<link rel="stylesheet" href="css/Default style sheet.css" rel="external nofollow" rel="external nofollow" />//本博客有css默认样式表可以下载。

div排版布局:

<body>
 <div id="box">
 <div id="lunbo"><img src="img/1.jpg" id="img"/></div>
 <div id="left"><</div>
 <div id="right">></div>
 <div id="radiu">
 <ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
 </ul>
 </div>
 </div>
 </body>

二,定义div的CSS样式,给div设置宽高,定位。

<style>
 body{
 background: darkturquoise;
 }
 #box{
 height:320px;
 width:480px;
 margin: 3px auto;
 border: 2px solid red;
 }
 #lunbo{
 height: 292px;
 width:453px;
 border: 1px solid yellow;
 margin: 0px auto;
 position: relative;
 }
 #left{
 height: 60px;
 width: 60px;
 font-size: 60px;
 text-align: center;
 line-height: 60px;
 position: absolute;
 top:150px;
 left: 440px;
 color: black;
 }
 #right{
 height: 60px;
 width: 60px;
 font-size: 60px;
 text-align: center;
 line-height: 60px;
 position: absolute;
 top:150px;
 left: 880px;
 color: black;
 }
 #radiu{
 height: 30px;
 width: 240px;
 text-align: center;
 
 margin: 0px auto;
 }
 #radiu li{
 float: left;
 background: white;
 height: 30px;
 width: 30px;
 line-height: 30px;
 border-radius:50% ;
 margin-right:6px;
 }
 .active{
 background: orange;
 color: ;
 }
 </style>
原生js代码:
<script>
 window.onload=function(){
 var ridiu=document.getElementById("radiu")
 var right=document.getElementById("right");
 var left=document.getElementById("left")
 var img=document.getElementById("img");
 var oli=ridiu.getElementsByTagName("li")
 var arry=['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg']
  var a=null;
 
  right.onclick=function(){
   a++
   if(a>arry.length-1){
   a=0;
   }
  
   img.src=arry[a]
  }
  left.onclick=function(){
   a--
   if(a<0){
   a=arry.length-1;
   }
  
   img.src=arry[a]
  
  }
  
  for (var i=0;i<oli.length;i++) {
   
   oli[i].onclick=function(){
   a++
   if(a==arry.length){
   a=0;
   }
    img.src=arry[a];
   }
   
  }
 
 }
 </script>

HTML全部效果代码:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>手动轮播图</title>
 <link rel="stylesheet" href="css/Default style sheet.css" rel="external nofollow" rel="external nofollow" />
 <style>
 body{
 background: darkturquoise;
 }
 #box{
 height:320px;
 width:480px;
 margin: 3px auto;
 border: 2px solid red;
 }
 #lunbo{
 height: 292px;
 width:453px;
 border: 1px solid yellow;
 margin: 0px auto;
 position: relative;
 }
 #left{
 height: 60px;
 width: 60px;
 font-size: 60px;
 text-align: center;
 line-height: 60px;
 position: absolute;
 top:150px;
 left: 440px;
 color: black;
 }
 #right{
 height: 60px;
 width: 60px;
 font-size: 60px;
 text-align: center;
 line-height: 60px;
 position: absolute;
 top:150px;
 left: 880px;
 color: black;
 }
 #radiu{
 height: 30px;
 width: 240px;
 text-align: center;
 
 margin: 0px auto;
 }
 #radiu li{
 float: left;
 background: white;
 height: 30px;
 width: 30px;
 line-height: 30px;
 border-radius:50% ;
 margin-right:6px;
 }
 .active{
 background: orange;
 color: ;
 }
 </style>
 <script>
 window.onload=function(){
 var ridiu=document.getElementById("radiu")
 var right=document.getElementById("right");
 var left=document.getElementById("left")
 var img=document.getElementById("img");
 var oli=ridiu.getElementsByTagName("li")
 var arry=['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg']
  var a=null;
 
  right.onclick=function(){
   a++
   if(a>arry.length-1){
   a=0;
   }
  
   img.src=arry[a]
  }
  left.onclick=function(){
   a--
   if(a<0){
   a=arry.length-1;
   }
  
   img.src=arry[a]
  
  }
  
  for (var i=0;i<oli.length;i++) {
   
   oli[i].onclick=function(){
   a++
   if(a==arry.length){
   a=0;
   }
    img.src=arry[a];
   }
   
  }
 
 }
 </script>
 </head>
 <body>
 <div id="box">
 <div id="lunbo"><img src="img/1.jpg" id="img"/></div>
 <div id="left"><</div>
 <div id="right">></div>
 <div id="radiu">
 <ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
 </ul>
 </div>
 </div>
 </body>
</html>

效果图:

body{
background: darkturquoise;
}
#box{
height:320px;
width:480px;
margin: 3px auto;
border: 2px solid red;
}
#lunbo{
height: 292px;
width:453px;
border: 1px solid yellow;
margin: 0px auto;
position: relative;
}
#left{
height: 60px;
width: 60px;
font-size: 60px;
text-align: center;
line-height: 60px;
position: absolute;
top:150px;
left: 440px;
color: black;
}
#right{
height: 60px;
width: 60px;
font-size: 60px;
text-align: center;
line-height: 60px;
position: absolute;
top:150px;
left: 880px;
color: black;
}
#radiu{
height: 30px;
width: 240px;
text-align: center;
margin: 0px auto;
}
#radiu li{
float: left;
background: white;
height: 30px;
width: 30px;
line-height: 30px;
border-radius:50% ;
margin-right:6px;
}
.active{
background: orange;
color: ;
}

相关文章

  • javascript实现炫酷的拖动分页

    javascript实现炫酷的拖动分页

    非常酷的javascript拖动分页功能,无缝循环分页,拖动鼠标即可完成分页,鼠标向左拖动回到前一页,向右拖动则翻开第二页,还带有动画特效,着实很不错,界面黑色,非主流风格,相信很多人会喜欢的。
    2015-05-05
  • file模式访问网页时iframe高度自适应解决方案

    file模式访问网页时iframe高度自适应解决方案

    最近做到iframe的高度自适应这个问题;发现自己做的网页是通过file方式访问的,将网页代码放到apache下通过http协议访问,在iframe加载的时候调用如下js方法:果然网页高度能够自适应(对于其他方案应该也有效果,我没有注意去尝试)感兴趣的朋友可以了解下
    2013-01-01
  • JS中的oninput和onchange事件的区别及如何正确使用

    JS中的oninput和onchange事件的区别及如何正确使用

    在JavaScript中,oninput和onchange事件是用于处理用户输入的常见事件,本文将介绍oninput和onchange事件的区别,以及如何在实际开发中正确使用它们,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • 获取当前月(季度/年)的最后一天(set相关操作及应用)

    获取当前月(季度/年)的最后一天(set相关操作及应用)

    本文主要介绍了setset相关操作及应用,通过获取当前月(季度/年)的最后一天具体事例来解析说明,具有一定的参考价值,下面跟着小编一起来看下吧
    2016-12-12
  • ie下js不执行的几种可能

    ie下js不执行的几种可能

    本文主要介绍了ie下js不执行的几种可能,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 详解JavaScript中new操作符的解析和实现

    详解JavaScript中new操作符的解析和实现

    这篇文章主要介绍了JavaScript中new操作符的解析和实现,帮助大家更好的理解和学习JavaScript,感兴趣的朋友可以了解下
    2020-09-09
  • 一文总结JS中逻辑运算符的特点

    一文总结JS中逻辑运算符的特点

    在JavaScript的众多运算符里,提供了三个逻辑运算符&&、||和!,下面这篇文章主要给大家介绍了关于JS中逻辑运算符的特点,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-03-03
  • javascript实现限制上传文件大小

    javascript实现限制上传文件大小

    这篇文章主要介绍了javascript实现限制上传文件大小的方法和示例,需要的朋友可以参考下
    2015-02-02
  • JavaScript实现页面定时刷新(定时器,meta)

    JavaScript实现页面定时刷新(定时器,meta)

    很多朋友看到定时,很容易想到用js定时器,还有盆友用meta来设置,下面小编给大家介绍js实现页面定时刷新的方法,一起看看吧
    2016-10-10
  • javascript中Object使用详解

    javascript中Object使用详解

    这篇文章主要介绍了javascript中Object使用详解,非常全面详尽,有需要的小伙伴参考下
    2015-01-01

最新评论