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


Posted in Javascript onNovember 22, 2018

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

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

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

效果图:

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

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

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 相关文章推荐
js 程序执行与顺序实现详解
May 13 Javascript
jquery实现select选中行、列合计示例
Apr 25 Javascript
浅谈JavaScript函数的四种存在形态
Jun 08 Javascript
jquery表单插件Autotab使用方法详解
Jun 24 Javascript
判断输入的字符串是否是日期格式的简单方法
Jul 11 Javascript
JavaScript生成.xls文件的代码
Dec 22 Javascript
用纯Node.JS弹出Windows系统消息提示框实例(MessageBox)
May 17 Javascript
vue左右侧联动滚动的实现代码
Jun 06 Javascript
详解Vue实战指南之依赖注入(provide/inject)
Nov 13 Javascript
浅谈Vue.js组件(二)
Apr 09 Javascript
node.js基础知识汇总
Aug 25 Javascript
可拖拽组件slider.js使用方法详解
Dec 04 Javascript
js实现按钮开关单机下拉菜单效果
Nov 22 #Javascript
vue+node实现图片上传及预览的示例方法
Nov 22 #Javascript
微信上传视频文件提示(推荐)
Nov 22 #Javascript
vue-cli3.0如何使用CDN区分开发、生产、预发布环境
Nov 22 #Javascript
详解三种方式解决vue中v-html元素中标签样式
Nov 22 #Javascript
详解Vue组件之作用域插槽
Nov 22 #Javascript
详解vue中localStorage的使用方法
Nov 22 #Javascript
You might like
PHP 获取目录下的图片并随机显示的代码
2009/12/28 PHP
phpmailer发送gmail邮件实例详解
2013/06/24 PHP
实现PHP中session存储及删除变量
2018/10/15 PHP
PHP过滤器 filter_has_var() 函数用法实例分析
2020/04/23 PHP
仿校内登陆框,精美,给那些很厉害但是没有设计天才的程序员
2008/11/24 Javascript
jquery UI 1.72 之datepicker
2009/12/29 Javascript
asp.net网站开发中用jquery实现滚动浏览器滚动条加载数据(类似于腾讯微博)
2012/03/14 Javascript
JQuery页面的表格数据的增加与分页的实现
2013/12/10 Javascript
css3元素简单的闪烁效果实现(html5 jquery)
2013/12/28 Javascript
Javascript实现简单二级下拉菜单实例
2014/06/15 Javascript
JavaScript使用循环和分割来替换和删除元素实例
2014/10/13 Javascript
使用Meteor配合Node.js编写实时聊天应用的范例
2015/06/23 Javascript
JavaScript深度复制(deep clone)的实现方法
2016/02/19 Javascript
js提交form表单,并传递参数的实现方法
2016/05/25 Javascript
js轮播图的插件化封装详解
2017/07/17 Javascript
vue 动态改变静态图片以及请求网络图片的实现方法
2018/02/07 Javascript
Vue中axios的封装(报错、鉴权、跳转、拦截、提示)
2019/08/20 Javascript
vue滚动插件better-scroll使用详解
2019/10/18 Javascript
koa-passport实现本地验证的方法示例
2020/02/20 Javascript
python flask中静态文件的管理方法
2018/03/20 Python
numpy返回array中元素的index方法
2018/06/27 Python
使用Django开发简单接口实现文章增删改查
2019/05/09 Python
利用anaconda作为python的依赖库管理方法
2019/08/13 Python
wxPython实现分隔窗口
2019/11/19 Python
Python实现GIF图倒放
2020/07/16 Python
如何利用Python matplotlib绘制雷达图
2020/12/21 Python
小学体育教学反思
2014/01/31 职场文书
2014学年自我鉴定
2014/02/23 职场文书
经典洗发水广告词
2014/03/13 职场文书
《鸟岛》教学反思
2014/04/26 职场文书
综合素质自我评价评语
2015/03/06 职场文书
幼儿园推普周活动总结
2015/05/07 职场文书
高中升旗仪式主持词
2015/07/03 职场文书
Mysql MVCC机制原理详解
2021/04/20 MySQL
Java并发编程必备之Future机制
2021/06/30 Java/Android
JavaScript严格模式不支持八进制的问题讲解
2021/11/07 Javascript