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 相关文章推荐
深入认识javascript中的eval函数
Nov 02 Javascript
ASP.NET jQuery 实例11 通过使用jQuery validation插件简单实现用户登录页面验证功能
Feb 03 Javascript
简单的JavaScript互斥锁分享
Feb 02 Javascript
javascript实现禁止复制网页内容
Dec 16 Javascript
实例详解jQuery的无new构建
Aug 02 Javascript
常用的javascript设计模式
Jan 11 Javascript
详解vue slot插槽的使用方法
Jun 13 Javascript
详解node.js 下载图片的 2 种方式
Mar 02 Javascript
Vue中qs插件的使用详解
Feb 07 Javascript
vue实现匀速轮播效果
Jun 29 Javascript
jQuery 动态粒子效果示例代码
Jul 07 jQuery
JavaScript前后端JSON使用方法教程
Nov 23 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也能干大事 随机函数
2015/04/14 PHP
详解php curl带有csrf-token验证模拟提交方法
2018/04/18 PHP
thinkPHP5.1框架使用SemanticUI实现分页功能示例
2019/08/03 PHP
关于laravel模板中生成URL的几种模式总结
2019/10/18 PHP
详细分析PHP 命名空间(namespace)
2020/06/30 PHP
ASP SQL防注入的方法
2008/12/25 Javascript
JS小功能(button选择颜色)简单实例
2013/11/29 Javascript
href下载文件根据id取url并下载
2014/05/28 Javascript
JavaScript中的alert()函数使用技巧详解
2014/12/29 Javascript
AngularJS基础 ng-show 指令简单示例
2016/08/03 Javascript
jquery中$.fn和图片滚动效果实现的必备知识总结
2017/04/21 jQuery
Javascript创建类和对象详解
2017/05/31 Javascript
Nodejs实现多房间简易聊天室功能
2017/06/20 NodeJs
Vue2.0基于vue-cli+webpack Vuex的用法(实例讲解)
2017/09/15 Javascript
浅谈webpack打包之后的文件过大的解决方法
2018/03/07 Javascript
jQuery实现数字自动增加或者减少的动画效果示例
2018/12/11 jQuery
Vue源码探究之虚拟节点的实现
2019/04/17 Javascript
vue组件库的在线主题编辑器的实现思路
2020/04/03 Javascript
解决微信授权成功后点击按返回键出现空白页和报错的问题
2020/06/08 Javascript
vue根据条件不同显示不同按钮的操作
2020/08/04 Javascript
[58:12]Ti4第二日主赛事败者组 LGD vs iG 3
2014/07/21 DOTA
[06:53]2018DOTA2国际邀请赛寻真——勇于创新的Vici Gaming
2018/08/14 DOTA
Python利用Nagios增加微信报警通知的功能
2016/02/18 Python
Python OpenCV实现视频分帧
2019/06/01 Python
python threading和multiprocessing模块基本用法实例分析
2019/07/25 Python
Pytorch 数据加载与数据预处理方式
2019/12/31 Python
Python如何发送与接收大型数组
2020/08/07 Python
HTML5实现页面切换激活的PageVisibility API使用初探
2016/05/13 HTML / CSS
出纳员的岗位职责
2014/02/22 职场文书
家长寄语大全
2014/04/02 职场文书
教师求职自荐书
2014/06/14 职场文书
群教个人对照检查材料
2014/08/20 职场文书
纪念9.18事变演讲稿
2014/09/14 职场文书
卡特教练观后感
2015/06/08 职场文书
宣传稿格式范文
2015/07/23 职场文书
golang三种设计模式之简单工厂、方法工厂和抽象工厂
2022/04/10 Golang