原生js轮播(仿慕课网)

 更新时间:2017年02月15日 10:33:50   作者:完美续航  
本文主要分享了原生js实现仿慕课网的轮播效果。具有很好的参考价值,下面跟着小编一起来看下吧

效果如下:

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>javascript</title>
 <style>
 *{margin:0;padding:0;border:0;}
 a{text-decoration:none;color:#fff;font-size:40px;line-height:200px;display:none;text-align:center;}
 #container{width:300px;height:200px;margin:50px auto;position:relative;overflow:hidden;}
 #list{width:2100px;height:200px;position:absolute;top:0;}
 #list span{width:300px;height:200px;display:inline-block;text-align:center;font-size:22px;float:left;color:#fff;}
 .one{background:red;}
 .two{background:orange;}
 .three{background:blue;}
 .four{background:green;}
 .five{background:black;}
 #buttons{width:200px;height:30px;position:absolute;bottom:0px;left:100px;z-index:9;}
 #buttons span{display:inline-block;cursor:pointer;width:12px;height:12px;border-radius:6px;background: #2a2a2a}
 #prev{width:40px;height:200px;position:absolute;left:0px;}
 #next{width:40px;height:200px;position:absolute;right:0px;}
 #container .on{background:#fff;}
 </style>
</head>
<body>
 <div id="container">
 <div id="list" style="left:-300px">
 <span class="five">我是黑色第五张</span>
 <span class="one">我是红色第一张</span>
 <span class="two">我是黄色第二张</span>
 <span class="three">我是蓝色第三张</span>
 <span class="four">我是绿色第四张</span>
 <span class="five">我是黑色第五张</span>
 <span class="one">我是红色第一张</span>
 </div>
 <div id="buttons">
 <span class="on" index="1"></span>
 <span index="2"></span>
 <span index="3"></span>
 <span index="4"></span>
 <span index="5"></span>
 </div>
 <a id="prev" href="javascript:;" rel="external nofollow" rel="external nofollow" >&lt;</a>
 <a id="next" href="javascript:;" rel="external nofollow" rel="external nofollow" >&gt;</a>
 </div>
 <script>
  var container = document.getElementById('container'),
   list = document.getElementById('list'),
   buttons = document.getElementById('buttons').getElementsByTagName('span'),
   prev = document.getElementById('prev'),
   next = document.getElementById('next'),
   index = 1,
   len = 5,
   interval = 3000,
   animated = false,
   timer;
  function animate(offset){
  if(offset == 0) return;
  animated = true;
  var time = 150,
  inter = 5,
  speed = offset/(time/inter),
  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';
  setTimeout(go,inter);
 }else{
  list.style.left = left + 'px';
  if(left > -100){
  list.style.left = -300*len + 'px';
  }
  if(left < (-300*len)){
  list.style.left = '-300px'
  }
  animated = false;
 }
 }
 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 == 5){
  index = 1;
  }else{
  index++;
  }
  animate(-300);
  showButton();
  }
  prev.onclick = function(){
  if(animated) {
  return;
  }
  if(index == 1){
  index = 5;
  }else{
  index--;
  }
  animate(300);
  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 = -300 * (myIndex - index);
     animate(offset);
     index = myIndex;
     showButton();
    }
   }
  container.onmouseover = function(){
  prev.style.display = next.style.display = 'block';
  stop();
  }
  container.onmouseout = function(){
  prev.style.display = next.style.display = 'none';
  play();
 }
  play();
 </script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • JavaScript中setInterval()和setTimeout()的用法及区别

    JavaScript中setInterval()和setTimeout()的用法及区别

    这篇文章主要给大家介绍了关于JavaScript中setInterval()和setTimeout()用法及区别的相关资料,Javascript的setTimeOut和setInterval函数应用非常广泛,它们都用来处理延时和定时任务,需要的朋友可以参考下
    2023-11-11
  • 微信小程序实现animation动画

    微信小程序实现animation动画

    这篇文章主要为大家详细介绍了微信小程序实现animation动画的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • js操作输入框提示信息且响应鼠标事件

    js操作输入框提示信息且响应鼠标事件

    注册网站的输入框就有默认提示值,当获取鼠标焦点的时候,默认值被删除,当用户没输入东西焦点离开的时候,又恢复默认提示值
    2014-03-03
  • JavaScript将坐标字符串转为数组的项目实践

    JavaScript将坐标字符串转为数组的项目实践

    本文主要介绍了JavaScript将坐标字符串转为数组的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01
  • javascript css在IE和Firefox中区别分析

    javascript css在IE和Firefox中区别分析

    我们讨论的主题CSS网页布局,最令大家头疼的问题就是浏览器兼容性,虽然52CSS.com介绍过很多这方向的知识,但依然让很多开发人员晕头转向,今天的这篇文章,将列出css和javascript在IE和Firefox中二十三个不同点,希望对大家的学习有所帮助。
    2009-02-02
  • 用js将long型数据转换成date型或datetime型的实例

    用js将long型数据转换成date型或datetime型的实例

    下面小编就为大家带来一篇用js将long型数据转换成date型或datetime型的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • JS 中的类Public,Private 和 Protected详解

    JS 中的类Public,Private 和 Protected详解

    这篇文章主要介绍了JS中的类Public,Private和Protected详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • javascript实现获取一个日期段内每天不同的价格(计算入住总价格)

    javascript实现获取一个日期段内每天不同的价格(计算入住总价格)

    这篇文章主要介绍了javascript实现获取一个日期段内每天不同的价格(计算入住总价格)的相关资料,需要的朋友可以参考下
    2018-02-02
  • localStorage实现便签小程序

    localStorage实现便签小程序

    这篇文章主要为大家详细介绍了localStorage实现便签小程序的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • JS获取鼠标坐标的实例方法

    JS获取鼠标坐标的实例方法

    这篇文章介绍了JS获取鼠标坐标的实例方法,有需要的朋友可以参考一下
    2013-07-07

最新评论