原生js实现焦点轮播图效果

发布时间 - 2026-01-10 22:28:01    点击率:

原生js焦点轮播图主要注意这几点:

1、前后按钮实现切换,同时注意辅助图

2、中间的button随着前后按钮对应切换,同时按button也能跳转到相应的index

3、间隔调用与无限轮播。

4、注意在动画时要停止按钮,或者说上一个动画完毕下一个动画才能执行

5、另外在切换图片的时候,底部的Button动画效果,是从底部开始往上升的,要用到transform:scale()和transform-origin:0 100%两个转换属性,代码如下

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8"/>
 <meta name="viewpoint" content="width=device-width,initial-scale=1,user-scalable="no">
 <title>20170101</title>
 <style type="text/css">
 a{text-decoration:none;color:#3DBBF5;}
 .wrapper{width:750px;height:350px;background:#001032;margin:20px auto;text-align:center;box-shadow:0 0 12px 2px hsla(0,20%,30%,0.5);padding:10px 15px;position:relative;}
 .effect{position:relative;cursor:pointer;}
 .effect:hover{color:#02a0e9;}
 .effect:before{width:100%;display:inline-block !important;position:absolute;height:1px;background:#02a0e9;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(0,1);content:'';bottom:-5px;left:0;}
 .effect:hover:before{transform:scale(1);-webkit-transform:scale(1);}
 #lunBo{margin-top:20px;overflow:hidden;height:300px;width:750px;position:relative;}
 #list{position:absolute;z-index:22;height:300px;width:5250px;}
 #list img{float:left;}
 #buttons { position: absolute; height: 20px; width: 150px; z-index: 99; bottom: 20px; left: 40%;}
    span { cursor: pointer; float: left; width: 10px; height: 5px; background: #333; margin-right: 10px;}
     .on { background: yellow;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(1,4);-ms-transform:scale(1,4);-moz-transform:scale(1,4);-webkit-transform:scale(1,4);transform-origin:0% 0%;-webkit-transform-origin:0% 100%;-moz-transform-origin:0% 100%;}
  .arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 100px; line-height:100px;position: absolute; z-index: 92; top: 30%; background-color: RGBA(0,0,0,.3); color: #fff;}
    .arrow:hover { background-color: RGBA(0,0,0,.7);}
    #lunBo:hover .arrow { display: block;}
    #prev { left: 0px;}
    #next { right: 0px;}
 </style>
 </head>
 <body>
 <div class="wrapper">
  <a class="effect" href="#">2016完了,2017来了</a>
  <div id="lunBo">
  <div id="list" style="left:-750px;">
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856m1bhxxx1d8jfnblb.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856z48mfrrr8u064rf6.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856e95yze236lvq7y2a.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.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:;" id="prev" class="arrow">&lt;</a>
  <a href="javascript:;" id="next" class="arrow">&gt;</a>
  </div>
 </div>
 <script>
 window.onload = function(){
  var lunBo = document.getElementById('lunBo');
  var list = document.getElementById('list');
  var buttons = document.getElementById('buttons').getElementsByTagName('span');
  //console.log(buttons);
  var prev = document.getElementById('prev');
      var next = document.getElementById('next');
  var index = 1;
  var animated = false;
  var interval = 3000;
  var timer;
  //显示按钮的索引
  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 += 1;
        }
        animate(-750);
        showButton();
      };
      prev.onclick = function () {
        if (animated) {
          return;
        }
        if (index == 1) {
          index = 5;
        }
        else {
          index -= 1;
        }
        animate(750);
        showButton();
      };
  //parseInt()转换为纯数值
  function animate(offset){
  animated = true;
  var newLeft = parseInt(list.style.left) + offset; //目标值
  var time = 300; //位移总时间为300
  var interval = 10; //
  var speed = offset/(Math.floor(time/interval)); //每次位移量
  function go(){
  if( (speed < 0 && parseInt(list.style.left) > newLeft) || ( speed > 0 && parseInt(list.style.left) < newLeft) ){
   list.style.left = parseInt(list.style.left) + speed + 'px';
    setTimeout(go,interval);
  }else{
   animated = false;
   list.style.left = newLeft+ 'px';  //现在的位移
   if( newLeft > -750){           //假的辅助图
   list.style.left = -3750 + 'px';
   }
   if( newLeft < -3750){
   list.style.left = -750 + 'px';
   }
  }
  };
  go();  
  };
  //小按钮
  for(var i=0;i < buttons.length;i++){
  buttons[i].onclick = function(){

  if(this.className == 'on'){
   return;
  };
   var myIndex = parseInt(this.getAttribute('index'));
   var offset = -750 * (myIndex - index);

   animate(offset);
          index = myIndex;
          showButton();
  }
  }
  lunBo.onmouseout = play;
  lunBo.onmouseover = stop;
  play();
 }
 </script>
 </body>
</html>

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


# 原生js  # 焦点  # 轮播  # 原生js实现轮播图的示例代码  # 原生js实现无限循环轮播图效果  # 原生js实现移动开发轮播图、相册滑动特效  # 原生js实现无缝轮播图效果  # 原生js实现旋转木马轮播图效果  # 原生JS实现层叠轮播图  # 原生JS轮播图插件  # 原生JS京东轮播图代码  # 原生js实现网易轮播图效果  # 无限循环轮播图之运动框架(原生JS实现)  # 原生JS实现的轮播图功能详解  # 来了  # 也能  # 是从  # 要用  # 几点  # 时间为  # 或者说  # 时要  # 转换为  # 跳转到  # 往上升  # effect  # hsla  # relative  # padding  # position  # auto  # margin  # background  # box 


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


相关推荐: Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】  Python数据仓库与ETL构建实战_Airflow调度流程详解  Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】  JavaScript数据类型有哪些_如何准确判断一个变量的类型  canvas 画布在主流浏览器中的尺寸限制详细介绍  Laravel如何生成和使用数据填充?(Seeder和Factory示例)  Laravel如何集成Inertia.js与Vue/React?(安装配置)  如何在搬瓦工VPS快速搭建网站?  如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】  如何在建站之星绑定自定义域名?  bootstrap日历插件datetimepicker使用方法  做企业网站制作流程,企业网站制作基本流程有哪些?  如何在腾讯云服务器快速搭建个人网站?  浅析上传头像示例及其注意事项  Laravel如何处理和验证JSON类型的数据库字段  如何在阿里云完成域名注册与建站?  Win11怎么修改DNS服务器 Win11设置DNS加速网络【指南】  Android使用GridView实现日历的简单功能  Laravel PHP版本要求一览_Laravel各版本环境要求对照  ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】  如何在IIS中新建站点并配置端口与IP地址?  1688铺货到淘宝怎么操作 1688一键铺货到自己店铺详细步骤  SQL查询语句优化的实用方法总结  PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑  如何在云虚拟主机上快速搭建个人网站?  Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】  制作旅游网站html,怎样注册旅游网站?  使用豆包 AI 辅助进行简单网页 HTML 结构设计  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  如何在橙子建站上传落地页?操作指南详解  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  lovemo网页版地址 lovemo官网手机登录  消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工  Laravel怎么上传文件_Laravel图片上传及存储配置  Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  如何快速辨别茅台真假?关键步骤解析  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  javascript读取文本节点方法小结  Android自定义listview布局实现上拉加载下拉刷新功能  Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门  Win11关机界面怎么改_Win11自定义关机画面设置【工具】  宙斯浏览器怎么屏蔽图片浏览 节省手机流量使用设置方法  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  Laravel Octane如何提升性能_使用Laravel Octane加速你的应用  Android实现代码画虚线边框背景效果  Laravel如何发送邮件和通知_Laravel邮件与通知系统发送步骤  ,怎么在广州志愿者网站注册?