源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

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

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2017-1-12 08:34:11 | 显示全部楼层 |阅读模式
本文主要分享了原生js实现焦点轮播图效果的示例代码,并解析了实例中的注意点。具有一定的参考价值,下面跟着小编一起来看下吧

原生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"><</a>
  <a href="javascript:;" id="next" class="arrow">></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>

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

回复

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-8-13 21:27:04 | 显示全部楼层
vcxvcxv
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2023-1-28 09:11:13 | 显示全部楼层
谢谢您的分享!
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

207

积分

中级会员

Rank: 3Rank: 3

积分
207
发表于 2023-2-9 02:54:46 | 显示全部楼层
强烈支持楼主ing……
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-6-24 07:33:56 | 显示全部楼层
的vgdsvsdvdsvdsvds
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-6-30 15:09:02 | 显示全部楼层
哦哦哦哦哦哦哦哦哦
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

319

积分

中级会员

Rank: 3Rank: 3

积分
319
发表于 2023-9-17 05:20:28 | 显示全部楼层
抽根烟,下来看看再说
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-10-17 10:57:45 | 显示全部楼层
终于找到了,我擦
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

120

积分

注册会员

Rank: 2

积分
120
发表于 2023-10-27 22:59:13 | 显示全部楼层
还有人在不。。。。。。。。。。啊
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

163

积分

注册会员

Rank: 2

积分
163
发表于 2024-3-2 07:33:05 | 显示全部楼层
很不错的样子
TS人妖演出表演服务q3268336102电话13168842816
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-7 03:10 , Processed in 0.157022 second(s), 41 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表