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 动态调整图片尺寸实现代码
Dec 28 Javascript
jQuery 数据缓存data(name, value)详解及实现
Jan 04 Javascript
一些常用的JavaScript函数(json)附详细说明
May 25 Javascript
ie下动态加态js文件的方法
Sep 13 Javascript
node.js中的http.request.end方法使用说明
Dec 10 Javascript
使用javascript插入样式
Mar 14 Javascript
Vue2实现组件props双向绑定
Dec 02 Javascript
vue2.0构建单页应用最佳实战
Apr 01 Javascript
sublime text配置node.js调试(图文教程)
Nov 23 Javascript
JS 正则表达式验证密码、邮箱格式的实例代码
Oct 28 Javascript
微信小程序点击view动态添加样式过程解析
Jan 21 Javascript
JavaScript原型继承和原型链原理详解
Feb 04 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远程下载类分享
2016/04/13 PHP
PHP中include/require/include_once/require_once使用心得
2016/08/28 PHP
微信支付的开发流程详解
2016/09/13 PHP
PHP实现的MD5结合RSA签名算法实例
2017/10/07 PHP
laravel 解决Validator使用中出现的问题
2019/10/25 PHP
jQuery 中使用JSON的实现代码
2011/12/01 Javascript
jquery多选项卡效果实例代码(附效果图)
2013/03/23 Javascript
JQuery记住用户名密码实现下次自动登录功能
2015/04/27 Javascript
JQUERY简单按钮轮换选中效果实现方法
2015/05/07 Javascript
AngularJS通过$location获取及改变当前页面的URL
2016/09/23 Javascript
详解webpack打包vue时提取css
2017/05/26 Javascript
微信小程序 es6-promise.js封装请求与处理异步进程
2017/06/12 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
学习python (1)
2006/10/31 Python
python实现登陆知乎获得个人收藏并保存为word文件
2015/03/16 Python
在Python中操作字符串之replace()方法的使用
2015/05/19 Python
3个用于数据科学的顶级Python库
2018/09/29 Python
Python jieba库用法及实例解析
2019/11/04 Python
python cv2在验证码识别中应用实例解析
2019/12/25 Python
jupyter notebook 添加kernel permission denied的操作
2020/04/21 Python
python实现密码验证合格程序的思路详解
2020/06/01 Python
使用HTML5加载音频和视频的实现代码
2020/11/30 HTML / CSS
德国大型箱包和皮具商店:Koffer
2019/10/01 全球购物
开会迟到检讨书
2014/02/03 职场文书
学校消防演习方案
2014/02/19 职场文书
高中英语演讲稿范文
2014/04/24 职场文书
党政领导班子四风问题对照检查材料思想汇报
2014/10/02 职场文书
煤矿安全保证书
2015/02/27 职场文书
主持稿开场白
2015/06/01 职场文书
党员转正党支部意见
2015/06/02 职场文书
红歌会主持词
2015/07/02 职场文书
先进个人主要事迹怎么写
2015/11/04 职场文书
《雷雨》教学反思
2016/02/20 职场文书
Python re.sub 反向引用的实现
2021/07/07 Python
关于python pygame游戏进行声音添加的技巧
2021/10/24 Python
MySQL创建管理子分区
2022/04/13 MySQL