js实现上下滑动轮播

本文实例为大家分享了js实现上下滑动轮播的具体代码,供大家参考,具体内容如下

一、效果图

二、设计思路

第一步:遍历所有的元素使得鼠标点击右侧小图时,图片变亮并且根据偏移值加上红框。点击右边的小图左边出现对用的图片。

第二步:利用循环计时器,克隆ul里面的第一个元素使得连续循环滑动。

第三步:鼠标进入时循环滑动停止,离开时继续。

第四步:设置上下按钮,当第一张图片的offsetTop值为0时,下面按钮出现,当到达底部最后一个元素时,上面按钮出现,底部按钮消失,当在整个元素中间时,上下按钮都出现,每点击一次按钮移动一个格子,左边图片也对应改变。

三、核心代码

//找到right-btn 元素添加事件
var righttBtnList;
var Line;
var transy = 0;
var liHeight = 430;
var ulItem;
var count = 0;
var timer;
var speed = 2000;
var Item;
var ItemMenu;
var offsetTop = 0;
var itemtabinfo, topBtn, bottomBtn;
    window.onload = function () {
        righttBtnList = document.getElementsByClassName("right-btn");
        Line = document.getElementsByClassName("line")[0];
        ulItem = document.getElementsByClassName("item-child-ul")[0];
        Item = document.getElementsByClassName("item-list")[0];
        ItemMenu = document.getElementsByClassName("item-menu")[0];
        itemtabinfo = document.getElementsByClassName("item-tab-info")[0];
        topBtn = document.getElementsByClassName("top-btn")[0];
        bottomBtn = document.getElementsByClassName("bottom-btn")[0];
        //默认复制第一张添加到ulItem之中
        ulItem.appendChild(ulItem.children[0].cloneNode(true));
        //设置itemtabinfo 默认移动值
        itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
        //直接默认启动计时器
        Animate();
        //遍历所有的li添加事件
        for (var i = 0; i < righttBtnList.length; i++) {
            righttBtnList[i].index = i;
            righttBtnList[i].onclick = function () {
                //点击当前移除top-white
                if (checkClass(this, 'top-white')) {
                    this.classList.remove("top-white");
                    //其余的添加
                    addWhite(this.index);
                }
                //获取偏移值
                Line.style.top = ((this.index * 110 + 10) + offsetTop) + "px";
                //输出当前点击的索引
                ulItem.style.transform = "translateY(" + (-this.index * liHeight) + "px)";
                //用户点击的索引  对应count值
                count = this.index;
            }
        }

        Item.onmouseenter=function(){
            clearTimeout(timer);
        }
        Item.onmouseleave=function(){
            Animate();
        }
        topBtn.onclick = function () {
            offsetTop += 110;
            //获取原来的top
            var oldTop = parseFloat(Line.style.top);
            Line.style.top = (oldTop + 110) + "px";
            itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
            checkBtnShow();
        }
        bottomBtn.onclick = function () {
            offsetTop -= 110;
            //获取原来的top
            var oldTop = parseFloat(Line.style.top);
            //只能取到行内样式
            Line.style.top = (oldTop - 110) + "px";
            itemtabinfo.style.transform = "translateY(" + offsetTop + "px)";
            checkBtnShow();
        }

        ItemMenu.onmouseenter = function () {
            checkBtnShow();
        }
        function checkBtnShow() {
            if (offsetTop == 0) {
                //下面按钮出现
                bottomBtn.classList.add("showBottom");
                topBtn.classList.remove("showTop");

            }
            else if (offsetTop == -220) {
                //上面按钮出现
                topBtn.classList.add("showTop");
                bottomBtn.classList.remove("showBottom");
            } else {
                //两个按钮同时出现
                bottomBtn.classList.add("showBottom");
                topBtn.classList.add("showTop");
            }
        }

        ItemMenu.onmouseleave = function () {
            bottomBtn.classList.remove("showBottom");
            topBtn.classList.remove("showTop");
        }
        //检测是否具有top-white
        function checkClass(obj,className){
            return obj.classList.contains(className);
        }
        //其余的li添加
        function addWhite(index){
            for(var i=0;i<righttBtnList.length;i++){
                if(i!=index){
                    righttBtnList[i].classList.add("top-white");
                }
            }
        }
        //启动计时器动画
        function Animate(){
            //写执行代码
            timer=setInterval(function(){
                if (timer)
                    clearInterval(timer);
                if(!ulItem.classList.contains("transY")){
                    ulItem.classList.add("transY");
                }
                count++;
                ulItem.style.transform="translateY("+(-count*liHeight)+"px)";
                //找出当前每一张图动画完成时间
                setTimeout(function(){
                    if(count>=ulItem.children.length-1){
                        count=0;
                        //移除过渡类
                        ulItem.classList.remove("transY");
                        ulItem.style.transform="translateY(0px)";
                    }
                    //让右边的元素动画对应
                    //rigthBtnlist[count].click();
                },500)
            },speed)
        }
    } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • js实现固定宽高滑动轮播图效果

    话不多说,请看具体示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> <st

  • 原生js实现移动开发轮播图、相册滑动特效

    使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" href="css/photoSlider.min.css" /> <script src="js/photoSlider.min.js" type="text/javascript" charset="utf-8"></script

  • JS实现touch 点击滑动轮播实例代码

    废话不多说了,直接给大家贴js代码了,具体代码如下所示: <script src="../js/jquery-1.8.3.min.js"></script> <script src="../js/jQuery.mobile-1.3.2.min.js"></script> ----------------------需要应用jquery .mobile自行百度 .num-zcon{ overflow: hidden; w

  • js实现从左向右滑动式轮播图效果

    轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实现第一种效果的方法. 原理 相同大小的图片并成一列,但只显示其中一张图片,其余的隐藏,通过修改left值来改变显示的图片. 点击查看效果 html部分 nav为总容器,第一个ul列表#index为小圆点列表,鼠标覆盖哪个小圆点就显现第几张图片,on是一个给小圆点添加背景颜色属性的类:第二个ul列表#

  • JS实现自动轮播图效果(自适应屏幕宽度+手机触屏滑动)

    1.本文使用js+jQuery实现轮播图,需要引用jquery包,另种实现分别是animate实现自适应的轮播,以及transform平滑轮播(在注释代码中). 2.代码中的图片大家自己更换就可以了,样式和逻辑均写在js里. 3.html标签代码,js代码 <div class="slider"> //轮播箭头 <p class="lastpic"><img src="../images/prev.png">&

  • javascript经典特效分享 手风琴、轮播图、图片滑动

    最近都没有更,就来几个效果充实一下. 都没有进行美化这步. 纯css的手风琴: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>手风琴css</title> <style> .showBox{ width: 660px; overflow: hidden; } ul{ list-style:

  • js实现支持手机滑动切换的轮播图片效果实例

    本文实例讲述了js实现支持手机滑动切换的轮播图片效果的方法.分享给大家供大家参考.具体如下: 运行效果如下: 完整实例代码点击此处本站下载. 使用方法案例: <script type="text/javascript" src="../src/zepto.js"></script> <script type="text/javascript" src="../src/carousel-image.js&qu

  • 原生JS无缝滑动轮播图

    本文实例为大家分享了原生JS无缝滑动轮播图的具体实现代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> ul,div,img{ margin: 0; padding

  • js实现滑动轮播效果

    本文实例为大家分享了js实现滑动轮播效果的具体代码,供大家参考,具体内容如下 1.构建html样式,代码如下 <div class="banner"> <ul> <li> <a href="#" > <img src="images/1.jpeg"> </a> </li> <li> <a href="#" > <i

  • js原生实现移动端手指滑动轮播图效果的示例

    如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0

随机推荐