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 相关文章推荐
基于Jquery+Ajax+Json的高效分页实现代码
Oct 29 Javascript
JS中eval函数的使用示例
Jul 21 Javascript
jQuery Trim去除字符串首尾空字符的实现方法说明
Feb 11 Javascript
js实现仿百度风云榜可重复多次调用的TAB切换选项卡效果
Aug 31 Javascript
jquery ztree实现模糊搜索功能
Feb 25 Javascript
Javascript中for循环语句的几种写法总结对比
Jan 23 Javascript
Node.js pipe实现源码解析
Aug 12 Javascript
AngularJS实现的select二级联动下拉菜单功能示例
Oct 25 Javascript
javaScript 连接打印机,打印小票的实例
Dec 29 Javascript
基于Vue+element-ui 的Table二次封装的实现
Jul 20 Javascript
Vue的生命周期操作示例
Sep 17 Javascript
vue中路由跳转不计入history的操作
Sep 21 Javascript
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
php 静态变量的初始化
2009/11/15 PHP
使用迭代器 遍历文件信息的详解
2013/06/08 PHP
php.ini save_handler 修改不生效的解决办法
2014/07/22 PHP
php实现的错误处理封装类实例
2017/06/20 PHP
js 方法实现返回多个数据的代码
2009/04/30 Javascript
utf-8编码引起js输出中文乱码的解决办法
2010/06/23 Javascript
来自国外的页面JavaScript文件优化
2010/12/08 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
javascript实现保留两位小数的多种方法
2015/12/18 Javascript
js随机生成26个大小写字母
2016/02/12 Javascript
JavaScript 消息框效果【实现代码】
2016/04/27 Javascript
详解Angular开发中的登陆与身份验证
2016/07/27 Javascript
Angular Renderer (渲染器)的具体使用
2018/05/03 Javascript
详解vue-loader在项目中是如何配置的
2018/06/04 Javascript
angular 表单验证器验证的同时限制输入的实现
2019/04/11 Javascript
p5.js码绘“跳动的小正方形”的实现代码
2019/10/22 Javascript
JS操作JSON常用方法(10w阅读)
2020/12/06 Javascript
python中使用百度音乐搜索的api下载指定歌曲的lrc歌词
2014/07/18 Python
Python中使用copy模块实现列表(list)拷贝
2015/04/14 Python
九步学会Python装饰器
2015/05/09 Python
Python批量提取PDF文件中文本的脚本
2018/03/14 Python
python学生信息管理系统(完整版)
2020/04/05 Python
python批量下载网站马拉松照片的完整步骤
2018/12/05 Python
将pandas.dataframe的数据写入到文件中的方法
2018/12/07 Python
树莓派极简安装OpenCv的方法步骤
2019/10/10 Python
基于Python中Remove函数的用法讨论
2020/12/11 Python
Elemis美国官网:英国的第一豪华护肤品牌
2018/03/15 全球购物
Bodum官网:咖啡和茶壶、玻璃器皿、厨房电器等
2018/08/01 全球购物
自我推荐书
2013/12/04 职场文书
高中生物教学反思
2014/02/05 职场文书
关于爱国的标语
2014/06/24 职场文书
离婚协议书该怎么写
2014/10/04 职场文书
2015年反洗钱工作总结
2015/04/25 职场文书
房产电话营销开场白
2015/05/29 职场文书
财务管理制度范本
2015/08/04 职场文书
Python图像处理库PIL详细使用说明
2022/04/06 Python