JS+HTML+CSS实现轮播效果


Posted in Javascript onNovember 28, 2017

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

1.lunbo.html代码:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <title>大轮播</title> 
  <link rel="stylesheet" type="text/css" href="CSS/lunbo.css"/> 
  <script src="JS/lunbo.js" type="text/javascript"></script> 
</head> 
 
<body> 
<div id="container"> 
  <div id="list" style="left: -1350px;"> 
    <img src="image/banner_3.jpg"/> 
    <img src="image/banner_1.jpg"/> 
    <img src="image/banner_2.jpg"/> 
    <img src="image/banner_3.jpg"> 
    <img src="image/banner_1.jpg"/></div> 
  <div id="buttons"> 
    <span index="1"></span> 
    <span index="2"></span> 
    <span index="3"></span> 
  </div> 
  <a href="javascript:;" id="prev" class="arrow" style="font-size:100px; text-align:center;"><</a> 
  <a href="javascript:;" id="next" class="arrow" style="font-size:100px; text-align:center;">></a></div> 
</body> 
 
</html>

2.CSS/lunbo.css代码:

body { 
  margin: 0px; 
  padding: 0px; 
  width: 1350px; 
  height: 618px; 
} 
 
a { 
  text-decoration: none; 
} 
 
#container { 
  width: 1350px; 
  height: 618px; 
  overflow: hidden; 
  position: relative; 
  border-top: 1px solid #ac6a0a; 
} 
 
#list { 
  width: 6750px; 
  height: 618px; 
  position: absolute; 
  z-index: 1; 
} 
 
#list img { 
  float: left; 
  width: 1350px; 
  height: 618px; 
} 
 
#buttons { 
  position: absolute; 
  height: 20px; 
  width: 60px; 
  z-index: 2; 
  bottom: 20px; 
  left: 50%; 
} 
 
#buttons span { 
  cursor: pointer; 
  float: left; 
  border: 1px solid #ad6a0a; 
  width: 10px; 
  height: 10px; 
  -webkit-border-radius: 50%; 
  -moz-border-radius: 50%; 
  border-radius: 50%; 
  margin-right: 5px; 
} 
 
#buttons .on { 
  background: orangered; 
} 
 
.arrow { 
  cursor: pointer; 
  display: none; 
  line-height: 100px; 
  text-align: center; 
  width: 40px; 
  height: 40px; 
  position: absolute; 
  z-index: 2; 
  top: 180px; 
  background-color: RGBA(0, 0, 0, 0); 
  color: #fff; 
} 
 
.arrow:hover { 
  background-color: RGBA(0, 0, 0, 0); 
} 
 
#container:hover .arrow { 
  display: block; 
} 
 
#prev { 
  left: 10px; 
  color: #ffffff; 
} 
 
#next { 
  right: 10px; 
  color: #ffffff; 
}

3.JS/lunbo.js代码:

window.onload = function () { 
  var container = document.getElementById('container'); 
  var list = document.getElementById('list'); 
  var buttons = document.getElementById('buttons').getElementsByTagName('span'); 
  var prev = document.getElementById('prev'); 
  var next = document.getElementById('next'); 
  var index = 1; 
  var len = 3; 
  var animated = false; 
  var interval = 3000; 
  var timer; 
  var size = 1350; 
 
  function animate(offset) { 
    if (offset == 0) { 
      return; 
    } 
    animated = true; 
    var time = 300; 
    var inteval = 10; 
    var speed = offset / (time / inteval); 
    console.log("speed:" + speed); 
    var left = parseInt(list.style.left) + offset; 
 
    var go = function () { 
      if ((speed > 0 && parseInt(list.style.left) < left) || (speed < 0 && parseInt(list.style.left) > left)) { 
        list.style.left = parseInt(list.style.left) + speed + 'px'; 
        console.log(list.style.left); 
        setTimeout(go, inteval); 
      } else { 
        list.style.left = left + 'px'; 
        if (left > -200) { 
          list.style.left = -size * len + 'px'; 
        } 
        if (left < ( -size * len)) { 
          list.style.left = '-' + size + 'px'; 
        } 
        animated = false; 
        console.log("left:" + list.style.left); 
      } 
    } 
    go(); 
  } 
 
  function showButton() { 
    for (var i = 0; i < buttons.length; i++) { 
      if (buttons[i].className == 'on') { 
        buttons[i].className = ''; 
        break; 
      } 
    } 
    buttons[index - 1].className = 'on'; 
  } 
 
  function play() { 
    timer = setTimeout(function () { 
        next.onclick(); 
        play(); 
      }, 
      interval); 
  } 
 
  function stop() { 
    clearTimeout(timer); 
  } 
 
  next.onclick = function () { 
    if (animated) { 
      return; 
    } 
    if (index == len) { 
      index = 1; 
    } else { 
      index += 1; 
    } 
    animate(-size); 
    showButton(); 
  } 
  prev.onclick = function () { 
    if (animated) { 
      return; 
    } 
    if (index == 1) { 
      index = len; 
    } else { 
      index -= 1; 
    } 
    animate(size); 
    showButton(); 
  } 
  for (var i = 0; i < buttons.length; i++) { 
    buttons[i].onclick = function () { 
      if (animated) { 
        return; 
      } 
      if (this.className == 'on') { 
        return; 
      } 
      var myIndex = parseInt(this.getAttribute('index')); 
      var offset = -size * (myIndex - index); 
 
      animate(offset); 
      index = myIndex; 
      showButton(); 
    } 
  } 
  container.onmouseover = stop; 
  container.onmouseout = play; 
  play(); 
};

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

Javascript 相关文章推荐
给Function做的OOP扩展
May 07 Javascript
深入理解javaScript中的事件驱动
May 21 Javascript
JS操作iframe里的dom(实例讲解)
Jan 29 Javascript
jQuery中scrollLeft()方法用法实例
Jan 16 Javascript
Highcharts使用简例及异步动态读取数据
Dec 30 Javascript
基于JS代码实现图片在页面中旋转效果
Jun 16 Javascript
详解AngularJS 路由 resolve用法
Apr 24 Javascript
JavaScript对象_动力节点Java学院整理
Jun 23 Javascript
JavaScript实现三级联动菜单效果
Aug 16 Javascript
jQuery ajax调用webservice注意事项
Oct 08 jQuery
新手如何快速理解js异步编程
Jun 24 Javascript
vue 递归组件的简单使用示例
Jan 14 Vue.js
vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
Nov 28 #Javascript
解决在vue+webpack开发中出现两个或多个菜单公用一个组件问题
Nov 28 #Javascript
vue-music关于Player播放器组件详解
Nov 28 #Javascript
JavaScript数据结构之双向链表和双向循环链表的实现
Nov 28 #Javascript
JS实现的找零张数最小问题示例
Nov 28 #Javascript
JavaScript数据结构之单链表和循环链表
Nov 28 #Javascript
微信小程序tabBar模板用法实例分析【附demo源码下载】
Nov 28 #Javascript
You might like
人大复印资料处理程序_输入篇
2006/10/09 PHP
解析PHP函数array_flip()在重复数组元素删除中的作用
2013/06/27 PHP
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
2013/06/29 PHP
解读PHP中的垃圾回收机制
2015/08/10 PHP
PHP简单实现文本计数器的方法
2016/04/28 PHP
PHP入门教程之数组用法汇总(创建,删除,遍历,排序等)
2016/09/11 PHP
一次因composer错误使用引发的问题与解决
2019/03/06 PHP
json数据的列循环示例
2013/09/06 Javascript
javascript获取dom的下一个节点方法
2014/09/05 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
Angularjs 实现分页功能及示例代码
2016/09/14 Javascript
js遍历json对象所有key及根据动态key获取值的方法(必看)
2017/03/09 Javascript
Node.js API详解之 util模块用法实例分析
2020/05/09 Javascript
[06:16]第十四期-国士无双绝地翻盘之撼地神牛
2014/06/24 DOTA
[42:56]VGJ.S vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
利用Django内置的认证视图实现用户密码重置功能详解
2017/11/24 Python
python遍历一个目录,输出所有的文件名的实例
2018/04/23 Python
python 把文件中的每一行以数组的元素放入数组中的方法
2018/04/29 Python
python批量从es取数据的方法(文档数超过10000)
2018/12/27 Python
python调用摄像头拍摄数据集
2019/06/01 Python
Python应用实现处理excel数据过程解析
2020/06/19 Python
解决PyCharm不在run输出运行结果而不是再Console里输出的问题
2020/09/21 Python
2020年10款优秀的Python第三方库,看看有你中意的吗?
2021/01/12 Python
Python datetime模块的使用示例
2021/02/02 Python
前端canvas水印快速制作(附完整代码)
2019/09/19 HTML / CSS
清明节扫墓活动方案
2014/03/02 职场文书
医院合作协议书
2014/08/19 职场文书
合作协议书格式
2014/08/19 职场文书
小学教育见习报告
2014/10/31 职场文书
2015年建筑工作总结报告
2015/05/04 职场文书
初中语文教学随笔
2015/08/15 职场文书
python requests模块的使用示例
2021/04/07 Python
python办公自动化之excel的操作
2021/05/23 Python
Python 如何解决稀疏矩阵运算
2021/05/26 Python
解决SpringCloud Feign传对象参数调用失败的问题
2021/06/23 Java/Android
MySQL中order by的执行过程
2022/06/05 MySQL