纯JS实现轮播图

发布时间 - 2026-01-10 23:13:35    点击率:

这几天一直在看js动画,今天又get到了一个轮播图,使用纯js实现的,但是没有美化哈,需要的小伙伴自己美化喔

如下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>图片轮播的效果</title>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
      text-decoration: none;
    }
    body {
      padding: 20px;
    }
    #container {
      position: relative;
      width: 600px;
      height: 400px;
      border: 3px solid #333;
      overflow: hidden;
    }
    #list {
      position: absolute;
      z-index: 1;
      width: 4200px;
      height: 400px;
    }
    #list img {
      float: left;
      width: 600px;
      height: 400px;
    }
    #buttons {
      position: absolute;
      left: 250px;
      bottom: 20px;
      z-index: 2;
      height: 10px;
      width: 100px;
    }
    #buttons span {
      float: left;
      margin-right: 5px;
      width: 10px;
      height: 10px;
      border: 1px solid #fff;
      border-radius: 50%;
      background: #333;
      cursor: pointer;
    }
    #buttons .on {
      background: orangered;
    }
    .arrow {
      position: absolute;
      top: 180px;
      z-index: 2;
      display: none;
      width: 40px;
      height: 40px;
      font-size: 36px;
      font-weight: bold;
      line-height: 39px;
      text-align: center;
      color: #fff;
      background-color: RGBA(0, 0, 0, .3);
      cursor: pointer;
    }
    .arrow:hover {
      background-color: RGBA(0, 0, 0, .7);
    }
    #container:hover .arrow {
      display: block;
    }
    #prev {
      left: 20px;
    }
    #next {
      right: 20px;
    }
  </style>
</head>
<body>
<div id="container">
  <div id="list" style="left: -600px;">
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s018.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s018.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s019.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s020.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s021.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s022.jpg" alt="无缝滚动" />
    <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/o_s022.jpg" alt="无缝滚动" />
  </div>
  <div id="buttons">
    <span index="1" class="on"></span>
    <span index="2"></span>
    <span index="3"></span>
    <span index="4"></span>
    <span index="5"></span>
  </div>
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="prev" class="arrow">&lt;</a>
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="next" class="arrow">&gt;</a>
</div>
<script type="text/javascript">
  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;
   function animate(offset){
     var newLeft = parseInt(list.style.left) + offset;
     list.style.left = newLeft + 'px';
     if(newLeft<-3000){
       list.style.left= -600 +'px';
     }
     if(newLeft>-600){
       list.style.left = -3000 + 'px';
     }
   }
   function buttonsshow(){
     for(var i =0; i<buttons.length;i++){
       if(buttons[i].className == 'on'){
         buttons[i].className='';
       }
     }
     buttons[index-1].className='on';
   }
   prev.onclick = function(){
     index-=1;
     if(index<1)
     {
       index=5;
     }
     buttonsshow();
     animate(600);
   };
   next.onclick = function(){
     index+=1;
     if(index>5){
       index=1;
     }
     buttonsshow();
     animate(-600);
   };
   //自动播放
   var timer;
    function play(){
      timer= setInterval(function(){
        next.onclick();
      },1000)
    }
    play();
    function stop(){
      clearInterval(timer);
    }
    container.onmouseover=stop;
    container.onmouseout=play;
for(var i=0; i<buttons.length; i++){
  ( function(i){
      buttons[i].onclick=function(){
        var clickindex= parseInt(this.getAttribute('index'));
        var offset = 600*(index-clickindex);
        animate(offset);
        index = clickindex;
        buttonsshow();
      }
  })(i);
}
  }
</script>
</body>
</html>

以上所述是小编给大家介绍的纯JS实现轮播图,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# js  # 轮播图  # 原生js实现无缝轮播图效果  # JS仿京东移动端手指拨动切换轮播图效果  # JavaScript 轮播图和自定义滚动条配合鼠标滚轮分享代码贴  # javascript轮播图算法  # 利用AngularJs实现京东首页轮播图效果  # js 基础篇必看(点击事件轮播图的简单实现)  # JS实现左右无缝轮播图代码  # zepto中使用swipe.js制作轮播图附swipeUp  # swipeDown不起效果问题  # js实现支持手机滑动切换的轮播图片效果实例  # 原生js实现移动开发轮播图、相册滑动特效  # js实现点击左右按钮轮播图片效果实例  # 小编  # 在此  # 给大家  # 这几天  # 所述  # 给我留言  # 感谢大家  # 自动播放  # 小伙伴  # 疑问请  # 有任何  # position  # container  # px  # height  # relative  # width  # type  # css  # title 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: javascript日期怎么处理_如何格式化输出  如何用花生壳三步快速搭建专属网站?  Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用  Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】  香港服务器部署网站为何提示未备案?  如何在橙子建站中快速调整背景颜色?  如何在Windows虚拟主机上快速搭建网站?  Android自定义listview布局实现上拉加载下拉刷新功能  如何在香港免费服务器上快速搭建网站?  瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口  如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环  北京企业网站设计制作公司,北京铁路集团官方网站?  佛山企业网站制作公司有哪些,沟通100网上服务官网?  网站建设要注意的标准 促进网站用户好感度!  Python文件流缓冲机制_IO性能解析【教程】  Laravel如何生成URL和重定向?(路由助手函数)  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  laravel服务容器和依赖注入怎么理解_laravel服务容器与依赖注入解析  Laravel如何使用模型观察者?(Observer代码示例)  Linux系统运维自动化项目教程_Ansible批量管理实战  图册素材网站设计制作软件,图册的导出方式有几种?  Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  大型企业网站制作流程,做网站需要注册公司吗?  常州企业网站制作公司,全国继续教育网怎么登录?  微信推文制作网站有哪些,怎么做微信推文,急?  googleplay官方入口在哪里_Google Play官方商店快速入口指南  Laravel如何创建自定义中间件?(Middleware代码示例)  如何基于云服务器快速搭建个人网站?  Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】  Laravel如何实现用户密码重置功能?(完整流程代码)  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  制作企业网站建设方案,怎样建设一个公司网站?  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全  Laravel怎么生成URL_Laravel路由命名与URL生成函数详解  HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】  如何在IIS管理器中快速创建并配置网站?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  Chrome浏览器标签页分组怎么用_谷歌浏览器整理标签页技巧【效率】  Python文件异常处理策略_健壮性说明【指导】  夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化  laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  Laravel中的Facade(门面)到底是什么原理  今日头条AI怎样推荐抢票工具_今日头条AI抢票工具推荐算法与筛选【技巧】  Laravel如何使用Eloquent ORM进行数据库操作?(CRUD示例)