[前端] js实现上下滑动轮播

2885 0
黑夜隐士 2022-10-21 15:39:09 | 显示全部楼层 |阅读模式
本文实例为大家分享了js实现上下滑动轮播的具体代码,供大家参考,具体内容如下
一、效果图



二、设计思路

第一步:遍历所有的元素使得鼠标点击右侧小图时,图片变亮并且根据偏移值加上红框。点击右边的小图左边出现对用的图片。
第二步:利用循环计时器,克隆ul里面的第一个元素使得连续循环滑动。
第三步:鼠标进入时循环滑动停止,离开时继续。
第四步:设置上下按钮,当第一张图片的offsetTop值为0时,下面按钮出现,当到达底部最后一个元素时,上面按钮出现,底部按钮消失,当在整个元素中间时,上下按钮都出现,每点击一次按钮移动一个格子,左边图片也对应改变。
三、核心代码
  1. //找到right-btn 元素添加事件
  2. var righttBtnList;
  3. var Line;
  4. var transy = 0;
  5. var liHeight = 430;
  6. var ulItem;
  7. var count = 0;
  8. var timer;
  9. var speed = 2000;
  10. var Item;
  11. var ItemMenu;
  12. var offsetTop = 0;
  13. var itemtabinfo, topBtn, bottomBtn;
  14.   window.onload = function () {
  15.     righttBtnList = document.getElementsByClassName("right-btn");
  16.     Line = document.getElementsByClassName("line")[0];
  17.     ulItem = document.getElementsByClassName("item-child-ul")[0];
  18.     Item = document.getElementsByClassName("item-list")[0];
  19.     ItemMenu = document.getElementsByClassName("item-menu")[0];
  20.     itemtabinfo = document.getElementsByClassName("item-tab-info")[0];
  21.     topBtn = document.getElementsByClassName("top-btn")[0];
  22.     bottomBtn = document.getElementsByClassName("bottom-btn")[0];
  23.     //默认复制第一张添加到ulItem之中
  24.     ulItem.appendChild(ulItem.children[0].cloneNode(true));
  25.     //设置itemtabinfo 默认移动值
  26.     itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
  27.     //直接默认启动计时器
  28.     Animate();
  29.     //遍历所有的li添加事件
  30.     for (var i = 0; i < righttBtnList.length; i++) {
  31.       righttBtnList[i].index = i;
  32.       righttBtnList[i].onclick = function () {
  33.         //点击当前移除top-white
  34.         if (checkClass(this, 'top-white')) {
  35.           this.classList.remove("top-white");
  36.           //其余的添加
  37.           addWhite(this.index);
  38.         }
  39.         //获取偏移值
  40.         Line.style.top = ((this.index * 110 + 10) + offsetTop) + "px";
  41.         //输出当前点击的索引
  42.         ulItem.style.transform = "translateY(" + (-this.index * liHeight) + "px)";
  43.         //用户点击的索引 对应count值
  44.         count = this.index;
  45.       }
  46.     }
  47.     Item.onmouseenter=function(){
  48.       clearTimeout(timer);
  49.     }
  50.     Item.onmouseleave=function(){
  51.       Animate();
  52.     }
  53.     topBtn.onclick = function () {
  54.       offsetTop += 110;
  55.       //获取原来的top
  56.       var oldTop = parseFloat(Line.style.top);
  57.       Line.style.top = (oldTop + 110) + "px";
  58.       itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
  59.       checkBtnShow();
  60.     }
  61.     bottomBtn.onclick = function () {
  62.       offsetTop -= 110;
  63.       //获取原来的top
  64.       var oldTop = parseFloat(Line.style.top);
  65.       //只能取到行内样式
  66.       Line.style.top = (oldTop - 110) + "px";
  67.       itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
  68.       checkBtnShow();
  69.     }
  70.     ItemMenu.onmouseenter = function () {
  71.       checkBtnShow();
  72.     }
  73.     function checkBtnShow() {
  74.       if (offsetTop == 0) {
  75.         //下面按钮出现
  76.         bottomBtn.classList.add("showBottom");
  77.         topBtn.classList.remove("showTop");
  78.       }
  79.       else if (offsetTop == -220) {
  80.         //上面按钮出现
  81.         topBtn.classList.add("showTop");
  82.         bottomBtn.classList.remove("showBottom");
  83.       } else {
  84.         //两个按钮同时出现
  85.         bottomBtn.classList.add("showBottom");
  86.         topBtn.classList.add("showTop");
  87.       }
  88.     }
  89.     ItemMenu.onmouseleave = function () {
  90.       bottomBtn.classList.remove("showBottom");
  91.       topBtn.classList.remove("showTop");
  92.     }
  93.     //检测是否具有top-white
  94.     function checkClass(obj,className){
  95.       return obj.classList.contains(className);
  96.     }
  97.     //其余的li添加
  98.     function addWhite(index){
  99.       for(var i=0;i<righttBtnList.length;i++){
  100.         if(i!=index){
  101.           righttBtnList[i].classList.add("top-white");
  102.         }
  103.       }
  104.     }
  105.     //启动计时器动画
  106.     function Animate(){
  107.       //写执行代码
  108.       timer=setInterval(function(){
  109.         if (timer)
  110.           clearInterval(timer);
  111.         if(!ulItem.classList.contains("transY")){
  112.           ulItem.classList.add("transY");
  113.         }
  114.         count++;
  115.         ulItem.style.transform="translateY("+(-count*liHeight)+"px)";
  116.         //找出当前每一张图动画完成时间
  117.         setTimeout(function(){
  118.           if(count>=ulItem.children.length-1){
  119.             count=0;
  120.             //移除过渡类
  121.             ulItem.classList.remove("transY");
  122.             ulItem.style.transform="translateY(0px)";
  123.           }
  124.           //让右边的元素动画对应
  125.           //rigthBtnlist[count].click();
  126.         },500)
  127.       },speed)
  128.     }
  129.   }
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行