原生js实现简单轮播图


Posted in Javascript onOctober 26, 2020

本文实例为大家分享了js实现简单轮播图的具体代码,供大家参考,具体内容如下

一、写入网页基本结构

body:

网页的最外部设置一个id为wrap的容器,取代body,这样方便我们做一些初始化

css:

初始化:

包括外边距margin、内边距padding、链接a、图片img、输入框input、列表ul、li、网页html、body一系列初始化

css设置:

根据图片数与图片宽度设置轮播图宽度

二、设置js逻辑

需要完成的功能有:

1、鼠标移入轮播图逐渐出现左右浮动块
2、点击浮动块切换图片
3、点击小圆点切换图片
4、切换图片同时切换小圆点
5、自动播放
6、鼠标移入轮播图自动播放停止、移出恢复自动播放

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style type="text/css">
 *{
  margin: 0;
  padding: 0;
 }
 a{
  text-decoration: none;
 }
 ul,li{
  list-style: none;
 }
 img{
  display: block;
 }
 input{
  outline: none;
 }
 html,body{
  height: 100%;
  overflow: hidden;
 }
 .content{
  position: absolute;
  top: 0;
  left: 0;
 }
 .banner{
  width: 600px;
  height: 400px;
  position: relative;
  margin: 50px auto;
  overflow: hidden;
 }
 .banner .bannerList{
  position: absolute;
  left:-600px;
  top: 0;
  width: 4800px;
  height: 100%;
 }
 .banner .bannerList li{
  float: left;
  width: 600px;
  height: 400px;
  
 }
 .banner .bannerList li img{
  height: 100%;
  width: 100%;
 }
 .banner .left,.banner .right{
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 50px;
  border: solid 2px gray;
  font-size:30px;
  text-align: center;
  line-height: 50px;
  color: rgb(255, 255, 255);
  opacity: 0;
  transition: 1000ms;
 }
 .banner .left{
  left: 0px;
 }
 .banner .right{
  right: 0px;
 }
 .banner .point{
  position: absolute;
  bottom: 30px;
  left:50%;
  transform: translateX(-50%);
 }
 .banner .point li{
  float: left;
  width: 15px;
  height: 15px;
 border-radius: 50%;
 background-color: gray;
  margin: 5px;
 }
 </style>
 <script type="text/javascript">
 window.onload=function(){
  var bannerLeft=document.querySelector('.banner .left');
  var bannerRight=document.querySelector('.banner .right');
  var banner=document.querySelector('.banner')
  var bannerList=document.querySelector('.banner .bannerList')
  var liList=document.querySelectorAll('.banner .bannerList li')
  var pointList=document.querySelectorAll('.banner .point li')
  var start=-600;


   timer2=setInterval(function(){    //设置定时器,自动播放
   var a=30;
   var index=bannerList.offsetLeft/-600+1;
   if(bannerList.offsetLeft==-4200){
     bannerList.style.left=-600+'px'   //无缝操作
     index=2;
    }
    if(bannerList.offsetLeft==-3600){
     index=1;
    }
   for(var i=0;i<pointList.length;i++){
     pointList[i].style.backgroundColor='grey'
    }
    pointList[index-1].style.backgroundColor='red';
   timer3=setInterval(function(){
    a=a-1
    bannerList.style.left=bannerList.offsetLeft-20+'px';
    
    if(a==0){
     clearInterval(timer3)
    }
   },
    30)
    if(bannerList.offsetLeft==-4200){
     bannerList.style.left=-600+'px';
    } 
  },4000)


  // 创建一个移入出现函数
  banner.addEventListener('mouseover',occur)
  function occur(){
  bannerLeft.style.opacity=1;   //移入逐渐出现
  bannerRight.style.opacity=1;  
  clearInterval(timer2);    //移入取消自动播放
 }
  banner.addEventListener('mouseout',function(){
   bannerLeft.style.opacity=0;
   bannerRight.style.opacity=0;  //移出消失
   timer2=setInterval(function(){   //移出恢复自动播放
   var a=30;
   timer3=setInterval(function(){
    a=a-1
    bannerList.style.left=bannerList.offsetLeft-20+'px';
    
    if(a==0){
     clearInterval(timer3)
    }
   },
    30)
    if(bannerList.offsetLeft==-4200){
     bannerList.style.left=-600+'px';
    }
  },4000)

  })
  //设置左右浮动块点击变色以及滚动
  function colorChange(){
   this.style.color='black';
   if(this.className=='right'){  //判断是哪边被点击
     var index=bannerList.offsetLeft/-600+1;
     var a=30;
     timer=setInterval(function(){  //点击浮动块切换图片
     a=a-1;
     bannerList.style.left=bannerList.offsetLeft-20+'px';
     if(a!=0){
      bannerRight.removeEventListener('mousedown',colorChange)
      bannerLeft.removeEventListener('mousedown',colorChange)
      
     }
     if(a==0){
      clearInterval(timer);
      bannerRight.addEventListener('mousedown',colorChange)
      bannerLeft.addEventListener('mousedown',colorChange)
     }
    },30)
    if(bannerList.offsetLeft==-4200){
     bannerList.style.left=-600+'px'
     index=2;
    }
    if(bannerList.offsetLeft==-3600){
     index=1;
    }
    for(var i=0;i<pointList.length;i++){
     pointList[i].style.backgroundColor='grey'
    }
    pointList[index-1].style.backgroundColor='red';
   }
   else{
     var a=30;
     var index=bannerList.offsetLeft/-600-1;
     timer=setInterval(function(){
     a=a-1;
     bannerList.style.left=bannerList.offsetLeft+20+'px';
     if(a!=0){
      bannerLeft.removeEventListener('mousedown',colorChange)
      bannerRight.removeEventListener('mousedown',colorChange)
     }
     if(a==0){
      clearInterval(timer);
      bannerLeft.addEventListener('mousedown',colorChange)
      bannerRight.addEventListener('mousedown',colorChange)
     }
    },30)
    if(bannerList.offsetLeft==0){
     bannerList.style.left=-3600+'px'
     index=5;
    }
    if(bannerList.offsetLeft==-600){
     index=6;
    }
    for(var i=0;i<pointList.length;i++){
     pointList[i].style.backgroundColor='grey'
    }
    pointList[index-1].style.backgroundColor='red';
   }
  }
  function colorReturn(){
   this.style.color='white'
  }
  bannerLeft.addEventListener('mousedown',colorChange)
  bannerRight.addEventListener('mousedown',colorChange)
  bannerLeft.addEventListener('mouseup',colorReturn)
  bannerRight.addEventListener('mouseup',colorReturn)
  for(var i=0;i<pointList.length;i++){
   pointList[i].onmousedown=function(){
    for(var i=0;i<pointList.length;i++){
     pointList[i].style.backgroundColor='gray'
    }
    this.style.backgroundColor='red';
    for(var b=0;b<pointList.length;b++){
     if(pointList[b].style.backgroundColor==this.style.backgroundColor){
      var a=30;
      var step=-(b+1)*600-bannerList.offsetLeft;
      timer1=setInterval(function(){
      a=a-1;
      bannerList.style.left=bannerList.offsetLeft+step/30+'px';
      if(a!=0){
      bannerLeft.removeEventListener('mousedown',colorChange)
      bannerRight.removeEventListener('mousedown',colorChange)

     }
      if(a==0){
       clearInterval(timer1)
       bannerLeft.addEventListener('mousedown',colorChange)
       bannerRight.addEventListener('mousedown',colorChange)
      }
      },20)
     }
    }
   }
  }
  
 }
 </script>
</head>
<body>
 <div id="wrap">
  <!-- 写出轮播图框架 -->
  <div class="banner">
   <ul class="bannerList">
    <li>
     <img src="./img/james6.jpeg">
    </li>
    <li>
     <img src="./img/james1.jpg">
    </li>
    <li>
     <img src="./img/james2.jpg">
    </li>
    <li>
     <img src="./img/james3.jpg">
    </li>
    <li>
     <img src="./img/james4.jpg">
    </li>
    <li>
     <img src="./img/james5.jpeg">
    </li>
    <li>
     <img src="./img/james6.jpeg">
    </li>
    <li>
     <img src="./img/james1.jpg">
    </li>
   </ul>
    <!-- 左右两个箭头 -->
    <span class="left"> < </span>
   <span class="right"> > </span>
    <!-- 添加小圆点 -->
    <ul class="point">
     <li style="background-color: red;"></li>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
    </ul>
  </div>
 </div>
 
</body>
</html>

其中,将图片替换为其他图片可以实现不同的轮播图,值得一提的是,六张图片的轮播图需要放八张图,第八张与第二张一致,第一张与第七张一致,真正呈现出来的仅仅是第二张到第七张

精彩专题分享:jQuery图片轮播 JavaScript图片轮播 Bootstrap图片轮播

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
对联广告js flash激活
Oct 19 Javascript
javascript showModalDialog模态对话框使用说明
Dec 31 Javascript
jquery 插件学习(一)
Aug 06 Javascript
Javacript实现颜色梯度变化和渐变的效果代码
May 31 Javascript
浅谈JavaScript中运算符的优先级
Jul 07 Javascript
js实现String.Fomat的实例代码
Sep 02 Javascript
IOS中safari下的select下拉菜单文字过长不换行的解决方法
Sep 26 Javascript
jquery组件WebUploader文件上传用法详解
Oct 23 Javascript
js实现手机发送验证码功能
Mar 13 Javascript
前端MVVM框架解析之双向绑定
Jan 24 Javascript
对angularJs中ng-style动态改变样式的实例讲解
Sep 30 Javascript
gojs实现蚂蚁线动画效果
Feb 18 Javascript
vue项目开启Gzip压缩和性能优化操作
Oct 26 #Javascript
Vue检测屏幕变化来改变不同的charts样式实例
Oct 26 #Javascript
解决ant design vue中树形控件defaultExpandAll设置无效的问题
Oct 26 #Javascript
vue下载二进制流图片操作
Oct 26 #Javascript
JavaScript如何操作css
Oct 24 #Javascript
javascript实现多边形碰撞检测
Oct 24 #Javascript
解决vue scoped html样式无效的问题
Oct 24 #Javascript
You might like
如何通过Linux命令行使用和运行PHP脚本
2015/07/29 PHP
PHP中ajax无刷新上传图片与图片下载功能
2017/02/21 PHP
Yii2框架数据验证操作实例详解
2018/05/02 PHP
php fread函数使用方法总结
2019/05/28 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
swoole锁的机制代码实例讲解
2021/03/04 PHP
JAVASCRIPT 对象的创建与使用
2021/03/09 Javascript
PJBlog插件 防刷新的在线播放器
2006/10/25 Javascript
JQUBar 基于JQUERY的柱状图插件
2010/11/23 Javascript
js 调用百度地图api并在地图上进行打点添加标注
2014/05/13 Javascript
JQuery 实现在同一页面锚点链接之间的平滑滚动
2014/10/29 Javascript
教你如何使用node.js制作代理服务器
2014/11/26 Javascript
深入理解JavaScript系列(36):设计模式之中介者模式详解
2015/03/04 Javascript
原生js实现弹出层登录拖拽功能
2016/12/05 Javascript
js实现键盘自动打字效果
2016/12/23 Javascript
Angular4学习笔记之准备和环境搭建项目
2017/08/01 Javascript
使用Angular CLI生成 Angular 5项目教程详解
2018/03/18 Javascript
浅谈vue3中effect与computed的亲密关系
2019/10/10 Javascript
详解钉钉小程序组件之自定义模态框(弹窗封装实现)
2020/03/07 Javascript
Python编写一个闹钟功能
2017/07/11 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
PyQt5实现简易计算器
2020/05/30 Python
Python日期时间Time模块实例详解
2019/04/15 Python
python整合ffmpeg实现视频文件的批量转换
2019/05/31 Python
Python绘制堆叠柱状图的实例
2019/07/09 Python
django 消息框架 message使用详解
2019/07/22 Python
pd.DataFrame统计各列数值多少的实例
2019/12/05 Python
python 通过手机号识别出对应的微信性别(实例代码)
2019/12/22 Python
PyQt5多线程防卡死和多窗口用法的实现
2020/09/15 Python
安装python依赖包psycopg2来调用postgresql的操作
2021/01/01 Python
艺术家策划的室内设计:Curious Egg
2019/03/06 全球购物
Lentiamo丹麦:购买便宜的隐形眼镜
2021/01/13 全球购物
厂办主管岗位职责范本
2014/02/28 职场文书
机关搬迁方案
2014/05/18 职场文书
安阳殷墟导游词
2015/02/10 职场文书
JS实现扫雷项目总结
2021/05/19 Javascript