JavaScript知识点整理(十六)- PC端网页特效 - 页面轮播图

目录

一、案例练习 - 页面轮播图

1.1 首先构建轮播图基本框架

1.2 左右按钮的显示和隐藏

1.3 下方小圆点 - 动态生成和点击事件

1.4 点击按钮滚动图片

1.5 节流阀

1.6 整体代码

1.6.1 HTML

1.6.2 CSS

1.6.3 JS


一、案例练习 - 页面轮播图

功能需求:

  1. 鼠标经过轮播图模块,左右按钮显示,离开时隐藏左右按钮
  2. 点击右侧按钮,图片往左播放一张
  3. 图片播放的同时,下方的小圆点跟随变化
  4. 点击小圆点可以播放相应的图片
  5. 鼠标没有经过轮播图,轮播图会自动播放
  6. 鼠标经过轮播图模块,自动播放停止

1.1 首先构建轮播图基本框架

HTML - 构建轮播图基本框架以及引入对于的css和js文件




    
    
    
    Document
    
    


    
< >

CSS - 渲染页面框架

*{
    margin: 0;
    padding: 0;
}
a{
    text-decoration: none;
}
li {
    list-style: none;
}
.focus {
    position: relative;
    width: 721px;
    height: 455px;
    background-color: skyblue;
}
.focus ul{
    position: absolute;
    top: 0;
    left: 0;
    width: 600%;
}
.focus ul li {
    float: left;
}
.focus img{
    width: 721px;
    height: 455px;
}
.arrow-l,
.arrow-r {
    position: absolute;
    top: 50%;
    margin-top: -20px;
    width: 24px;
    height: 40px;
    background: rgba(0, 0, 0, .3);
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-family: 'icomoon';
    font-size: 18px;
    z-index: 2;
}
.arrow-r {
    right: 0;
}
.circle {
    position: absolute;
    bottom: 10px;
    left: 50%;
    margin-left: -16px;
}

.circle li {
    float: left;
    width: 8px;
    height: 8px;
    border: 2px solid rgba(255, 255, 255, 0.5);
    margin: 0 3px;
    border-radius: 50%;
    cursor: pointer;
}
.current {
    background-color: orange;
}

1.2 左右按钮的显示和隐藏

左右按钮的显示和隐藏 - 案例分析:

  1. 鼠标经过轮播图,左右按钮显示,离开则隐藏左右按钮
  2. 显示隐藏,则改变左右按钮的display属性
window.addEventListener('load',function(){
    // 鼠标事件 - 左右按钮显示和隐藏
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var foucs = document.querySelector('.focus');
    foucs.addEventListener('mouseenter',function(){
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
    });
    foucs.addEventListener('mouseleave',function(){
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
    });
});

1.3 下方小圆点 - 动态生成和点击事件

动态生成小圆点-案例分析:

  1. 核心思路:小圆点的个数要和图片的张数一致
  2. 需先得到ul里面的图片张数,即得到ul内的li个数
  3. 利用循环动态生成小圆点,放入ol内
  4. 创建节点createElement('li'),插入节点ol.appendChild(li)
  5. 第一个小圆点先添加个添加个current类,显示为背景色为白色
  6. 注意:需删除先前HTML内ol标签下的li

点击小圆点改变该小圆点背景色 - 案例分析:

  1. 点击当前的小圆点,就为该小圆点添加current类
  2. 移除其他小圆点的current类

点击小圆点滑动图片 - 案例分析:

  1. 点击小圆点滚动图片
  2. 引入animate动画函数的JS文件
  3. 使用该动画函数的前提,该元素必须有定位
  4. 注意移动的是ul,而不是li
  5. 滚动图片的核心算法:点击莫格小圆点,就让图片鼓捣弄,小圆点的索引好乘以图片的宽度作为ul的移动距离

JS部分

    // 动态生成下方小圆点
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('ol');
    var focusWidth = focus.offsetWidth
    for(var i = 0; i < ul.children.length; i++){
        var li = document.createElement('li');
        // 记录当前小圆点的索引号
        li.setAttribute('index', i);
        ol.appendChild(li);
        // 生成小圆点的同时绑定点击事件
        li.addEventListener('click',function(){
            for(var i = 0; i < ol.children.length; i++){
                ol.children[i].className = '';
            }
            this.className = 'current';
            // 获取点击小圆点的索引号
            var index = this.getAttribute('index');
            console.log(index);
            animate(ul, -index * focusWidth)
        });
    }
    // 默认第一个小圆点设置current类
    ol.children[0].className = 'current';

animate.js

function animate(obj, target, callback) {
    clearInterval(obj.timer)
    // 给不同元素指定了不同定时器
    obj.timer = setInterval(function(){
        // 步长值写道定时器里
        // var step = Math.ceil((target - obj.offsetLeft) / 10);
        var step = (target - obj.offsetLeft) / 10;
        // 判断步长值的正负
        step = step > 0 ? Math.ceil(step) :Math.floor(step);
        if(obj.offsetLeft == target){
            clearInterval(obj.timer);
            // 回调函数写道定时器结束里面
            // if(callback){
            //     acllback();
            // }
            callback && callback();
        } else {
            obj.style.left = obj.offsetLeft + step +'px';
        }
    },15);
}

1.4 点击按钮滚动图片

无缝滚动 - 案例分析:

  1. 点击按钮一次,就让图片滚动一张
  2. 声明一个全局变量num,点一次,自动增加1,让这个变量乘以图片的宽度,就是ul的滚动距离
  3. 图片无缝滚动原理
  4. 把ul第一个li赋值一份,放到ul最后面
  5. 当图片滚动到克隆的最后一张图片时,ul快速跳转到最左侧,left:0;
  6. 注意:需要在小圆点点击事件中添加num = index,防止出现点击小圆点后再点击按钮时出现图片和小圆点对应不上

克隆第一张图片 - 案例分析

  1. 克隆ul第一个li,cloneNode() 加ture 深克隆赋值里面的子节点,false浅克隆

小圆点根据按钮变化 - 案例分析:

  1. 声明一个变量circle,每次点击自动加1,注意:该变量需声明全局变量
  2. 注意:需要在小圆点点击事件中添加circle = index,防止出现点击小圆点后再点击按钮时出现图片和小圆点对应不上
 // 克隆第一张图片,放到ul最后面
    var firstImg = ul.children[0].cloneNode(true);
    ul.appendChild(firstImg);

    // 点击右侧按钮,滚动图片
    var num = 0;
    var circle = 0;
    arrow_r.addEventListener('click',function(){
        if (num == ul.children.length -1){
            ul.style.left = 0;
            num = 0;
        }
        // 小圆点跟随按钮点击变化
        num++;
        animate(ul, -num * focusWidth);
        circle++;
        if(circle == ol.children.length){
            circle = 0;
        }
        // 调用函数
        circleChange();
    });
    // 点击左侧按钮,滚动图片
    arrow_l.addEventListener('click',function(){
        if (num == 0){
            num = ul.children.length - 1;
            ul.style.left = -num * focusWidth + 'px';
        }
        // 小圆点跟随按钮点击变化
        num--;
        animate(ul, -num * focusWidth);
        circle--;
        if(circle < 0){
            circle = ol.children.length - 1;
        }
        // 调用函数
        circleChange();
    });

    // 小圆点变化
    function circleChange(){
        for(var i = 0; i < ol.children.length; i++){
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current';
    }

JavaScript知识点整理(十六)- PC端网页特效 - 页面轮播图_第1张图片

 自动播放功能 - 案例分析:

  1. 自动播放实际上就是类似于按固定事件点击了一次右侧按钮
  2. 使用手动调用右侧按钮点击事件arrow_r.click();
  3. 鼠标经过,停止定时器,在鼠标进入事件中添加代码
  4. 鼠标离开,开始定时器,在鼠标离开事件中添加代码
    // 自动播放
    var timer = setInterval(function(){
        arrow_r.click();
    },2000);
focus.addEventListener('mouseenter',function(){
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        // 停止定时器
        clearInterval(timer);
        timer = null;
    });
    focus.addEventListener('mouseleave',function(){
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        // 开始定时器
        timer = setInterval(function(){
            arrow_r.click();
        },2000);
    });

1.5 节流阀

防止轮播图按钮连续点击造成播放过快

节流阀目的:当上一个函数动画内容执行完毕,再去执行下一个函数动画,让事件无法连续触发

核心思路:利用回调函数,添加一个变量来控制,锁住函数和解锁函数

开始设置一个变量 var flag = true;

if(flag){flag = flase; do something} 关闭

利用回调函数,动画执行完毕,flag = true,打开

添加到左右点击案例内

// 点击右侧按钮,滚动图片
    var num = 0;
    var circle = 0;
    var flag = true;

    arrow_r.addEventListener('click',function(){
        if(flag){
            // 关闭节流阀
            flag = false;
            if (num == ul.children.length -1){
                ul.style.left = 0;
                num = 0;
            }
            // 小圆点跟随按钮点击变化
            num++;
            animate(ul, -num * focusWidth,function(){
                flag =true; //打开节流阀
            });
            circle++;
            if(circle == ol.children.length){
                circle = 0;
            }
            // 调用函数
            circleChange();
        }
    });
    // 点击左侧按钮,滚动图片
    arrow_l.addEventListener('click',function(){
        if(flag){
            flag = false;
            if (num == 0){
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';
            }
            // 小圆点跟随按钮点击变化
            num--;
            animate(ul, -num * focusWidth,function(){
                flag =true; //打开节流阀
            });
            circle--;
            if(circle < 0){
                circle = ol.children.length - 1;
            }
            // 调用函数
            circleChange();
        }
    });

1.6 整体代码

1.6.1 HTML




    
    
    
    Document
    
    
    


    
< >

1.6.2 CSS

*{
    margin: 0;
    padding: 0;
}
a{
    text-decoration: none;
}
li {
    list-style: none;
}
.focus {
    position: relative;
    width: 721px;
    height: 455px;
    overflow: hidden;
    background-color: skyblue;
}
.focus ul{
    position: absolute;
    top: 0;
    left: 0;
    width: 600%;
}
.focus ul li {
    float: left;
}
.focus img{
    width: 721px;
    height: 455px;
}
.arrow-l,
.arrow-r {
    display: none;
    position: absolute;
    top: 50%;
    margin-top: -20px;
    width: 24px;
    height: 40px;
    background: rgba(0, 0, 0, .3);
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-family: 'icomoon';
    font-size: 18px;
    z-index: 2;
}
.arrow-r {
    right: 0;
}
.circle {
    position: absolute;
    bottom: 10px;
    left: 50%;
    margin-left: -16px;
}

.circle li {
    float: left;
    width: 8px;
    height: 8px;
    border: 2px solid rgba(255, 255, 255, 0.5);
    margin: 0 3px;
    border-radius: 50%;
    cursor: pointer;
}
.current {
    background-color: orange
}

1.6.3 JS

window.addEventListener('load',function(){
    // 鼠标事件 - 左右按钮显示和隐藏
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    var focusWidth = focus.offsetWidth;
    focus.addEventListener('mouseenter',function(){
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        // 停止定时器
        clearInterval(timer);
        timer = null;
    });
    focus.addEventListener('mouseleave',function(){
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        // 开始定时器
        timer = setInterval(function(){
            arrow_r.click();
        },2000);
    });

    // 动态生成下方小圆点
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('ol');
    for(var i = 0; i < ul.children.length; i++){
        var li = document.createElement('li');
        // 记录当前小圆点的索引号
        li.setAttribute('index', i);
        ol.appendChild(li);
        // 生成小圆点的同时绑定点击事件
        li.addEventListener('click',function(){
            for(var i = 0; i < ol.children.length; i++){
                ol.children[i].className = '';
            }
            this.className = 'current';
            // 获取点击小圆点的索引号
            var index = this.getAttribute('index');
            num = index;
            circle = index;
            console.log(index);
            animate(ul, -index * focusWidth)
        });
    }
    // 默认第一个小圆点设置current类
    ol.children[0].className = 'current';

    // 克隆第一张图片,放到ul最后面
    var firstImg = ul.children[0].cloneNode(true);
    ul.appendChild(firstImg);

    // 点击右侧按钮,滚动图片
    var num = 0;
    var circle = 0;
    var flag = true;

    arrow_r.addEventListener('click',function(){
        if(flag){
            // 关闭节流阀
            flag = false;
            if (num == ul.children.length -1){
                ul.style.left = 0;
                num = 0;
            }
            // 小圆点跟随按钮点击变化
            num++;
            animate(ul, -num * focusWidth,function(){
                flag =true; //打开节流阀
            });
            circle++;
            if(circle == ol.children.length){
                circle = 0;
            }
            // 调用函数
            circleChange();
        }
    });
    // 点击左侧按钮,滚动图片
    arrow_l.addEventListener('click',function(){
        if(flag){
            flag = false;
            if (num == 0){
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';
            }
            // 小圆点跟随按钮点击变化
            num--;
            animate(ul, -num * focusWidth,function(){
                flag =true; //打开节流阀
            });
            circle--;
            if(circle < 0){
                circle = ol.children.length - 1;
            }
            // 调用函数
            circleChange();
        }
    });

    // 小圆点变化
    function circleChange(){
        for(var i = 0; i < ol.children.length; i++){
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current';
    }

    // 自动播放
    var timer = setInterval(function(){
        arrow_r.click();
    },2000);
});

你可能感兴趣的:(JavaScript笔记,javascript,前端,css3)