原生JavaScript实现轮播图(PC端和移动端)

文章目录

    • PC端
      • 效果
      • Html部分
      • css部分
      • js部分
      • 文件源码
    • 移动端
      • 效果
      • 源码

PC端

效果

Html部分

<div class="container">
        
        <a href="javascript:;" class="previous"><a>
        <a href="javascript:;" class="next"> >a>
        
        
        <ul class="show">
            <li><img src="img/bg1.jpg" alt="">li>
            <li><img src="img/bg2.jpg" alt="">li>
            <li><img src="img/bg3.jpg" alt="">li>
        ul>

        
        <ul class="bottom"> ul>
    div>

css部分

		* {
            margin: 0;
            padding: 0;
        }


        .container {
            position: relative;
            width: 1000px;
            height: 677px;
            overflow: hidden;
        }

        .container .show {
            width: 300%;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;
        }

        .container .show li {
            list-style: none outside none;
            float: left;
        }

        .container a {
            position: absolute;
            text-decoration: none;
            display: none;
            width: 50px;
            font-size: 3em;
            color: #ccc;
            border-radius: 50%;
            line-height: 1;
            height: 50px;
            text-align: center;
            top: 350px;
        }

        .container a.next {
            right: 0;
        }

        .container .bottom {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 50px;
            background: rgba(0, 0, 0, .5);
        }

        .container .bottom li {
            list-style: none outside none;
            float: left;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: rgba(208, 208, 208, .8);
            margin-left: 10px;
            margin-top: 20px;
        }

        .container .bottom li.active {
            background: #545353;
        }

js部分

		//得到html诸元素
        let container = document.querySelector('.container');
        let left = document.querySelector('.previous');
        let right = document.querySelector('.next');
        let show = document.querySelector('.show');
        let bottom = document.querySelector('.bottom');
        
        //鼠标滑过时,出现左右箭头
        container.addEventListener('mouseenter', function () {
            left.style.display = "block";
            right.style.display = "block";
        });
        container.addEventListener('mouseleave', function () {
            left.style.display = "none";
            right.style.display = "none";
        });

        //根据图片数目生成底部小圆点
        for (let i = 0; i < show.childElementCount; i++) {
            let li = document.createElement('li');
            //每个小圆点点击时事件
            li.addEventListener('click', function () {
                //底部原点样式改变
                for (let j = 0; j < bottom.childElementCount; j++)
                    bottom.children[j].removeAttribute("class");
                this.setAttribute("class", "active");
                //滑动
                move(show, -(show.children[i].clientWidth * i));
            });
            bottom.appendChild(li);
        }

        let index = 0;
        function slider() {
            bottom.children[index].click();
            index = (index + 1) % bottom.children.length;
        }
        
        //左右按钮点击事件
        left.addEventListener('click', function () {
            clearInterval(t);
            index = index == 0 ? bottom.children.length - 1 : index - 1;
            bottom.children[index].click();
            t = setInterval(slider, 3000);
        });
        right.addEventListener('click', function () {
            clearInterval(t);
            index = index == bottom.children.length - 1 ? 0 : index + 1;
            bottom.children[index].click();
            t = setInterval(slider, 3000);
        });

        //定时器,间隔一定时间自动播放
        slider();
        let t = setInterval(slider, 3000);

        //将obj水平移动到target位置
        function move(obj, target, callback) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                let step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    if (callback)
                        callback();
                }
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15);
        }

文件源码


<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }


        .container {
            position: relative;
            width: 1000px;
            height: 677px;
            overflow: hidden;
        }

        .container .show {
            padding: 0;
            width: 300%;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;
        }

        .container .show li {
            list-style: none outside none;
            float: left;
        }

        .container a {
            position: absolute;
            text-decoration: none;
            display: none;
            width: 50px;
            font-size: 3em;
            color: #ccc;
            border-radius: 50%;
            line-height: 1;
            height: 50px;
            text-align: center;
            top: 350px;
        }

        .container a.next {
            right: 0;
        }

        .container .bottom {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 50px;
            background: rgba(0, 0, 0, .5);
        }

        .container .bottom li {
            list-style: none outside none;
            float: left;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: rgba(208, 208, 208, .8);
            margin-left: 10px;
            margin-top: 20px;
        }

        .container .bottom li.active {
            background: #545353;
        }
    style>
head>

<body>
    <div class="container">
        <a href="javascript:;" class="previous"><a>
        <a href="javascript:;" class="next"> >a>
        <ul class="show">
            <li><img src="img/bg1.jpg" alt="">li>
            <li><img src="img/bg2.jpg" alt="">li>
            <li><img src="img/bg3.jpg" alt="">li>
        ul>
        <ul class="bottom"> ul>
    div>

    <script>
        let container = document.querySelector('.container');
        let left = document.querySelector('.previous');
        let right = document.querySelector('.next');
        let show = document.querySelector('.show');
        let bottom = document.querySelector('.bottom');
        
        container.addEventListener('mouseenter', function () {
            left.style.display = "block";
            right.style.display = "block";
        });
        container.addEventListener('mouseleave', function () {
            left.style.display = "none";
            right.style.display = "none";
        });

        for (let i = 0; i < show.childElementCount; i++) {
            let li = document.createElement('li');
            li.addEventListener('click', function () {
                for (let j = 0; j < bottom.childElementCount; j++)
                    bottom.children[j].removeAttribute("class");
                this.setAttribute("class", "active");
                move(show, -(show.children[i].clientWidth * i));
            });
            bottom.appendChild(li);
        }

        let index = 0;
        function slider() {
            bottom.children[index].click();
            index = (index + 1) % bottom.children.length;
        }
        
        left.addEventListener('click', function () {
            clearInterval(t);
            index = index == 0 ? bottom.children.length - 1 : index - 1;
            bottom.children[index].click();
            t = setInterval(slider, 3000);
        });
        right.addEventListener('click', function () {
            clearInterval(t);
            index = index == bottom.children.length - 1 ? 0 : index + 1;
            bottom.children[index].click();
            t = setInterval(slider, 3000);
        });

        slider();
        let t = setInterval(slider, 3000);

        function move(obj, target, callback) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                let step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    if (callback)
                        callback();
                }
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15);
        }
    script>
body>

html>

移动端

效果

在这里插入图片描述

源码


<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端轮播图title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }


        .container {
            position: relative;
            width: 150px;
            height: 100px;
            overflow: hidden;
        }

        .container .show {
            padding: 0;
            width: 300%;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;
        }

        .container .show li {
            list-style: none outside none;
            float: left;
        }

        .container .bottom {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 20px;
            background: rgba(0, 0, 0, .5);
        }

        .container .bottom li {
            list-style: none outside none;
            float: left;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: rgba(208, 208, 208, .8);
            margin-left: 10px;
            margin-top: 5px;
        }

        .container .bottom li.active {
            background: #545353;
        }
    style>
head>

<body>
    <div class="container">

        
        <ul class="show">
            <li><img src="img/bg4.jpg" alt="">li>
            <li><img src="img/bg5.jpg" alt="">li>
            <li><img src="img/bg6.jpg" alt="">li>
        ul>

        
        <ul class="bottom"> ul>

    div>

    <script>
        let container = document.querySelector('.container');
        let show = document.querySelector('.show');
        let bottom = document.querySelector('.bottom');

        //根据图片数目生成底部小圆点
        for (let i = 0; i < show.childElementCount; i++) {
            let li = document.createElement('li');
            //每个小圆点点击时事件
            li.addEventListener('click', function () {
                //底部原点样式改变
                for (let j = 0; j < bottom.childElementCount; j++)
                    bottom.children[j].removeAttribute("class");
                this.setAttribute("class", "active");
                //滑动
                move(show, -(show.children[i].clientWidth * i));
            });
            bottom.appendChild(li);
        }

        let startX = 0;
        container.addEventListener('touchstart', function (e) {
            startX = e.targetTouches[0].pageX;
        })

        container.addEventListener('touchend', function (e) {
            // e.preventDefault();
            if (e.changedTouches[0].pageX > startX) {
                clearInterval(t);
                index = index == 0 ? bottom.children.length - 1 : index - 1;
                bottom.children[index].click();
                t = setInterval(slider, 3000);
            } else if (e.changedTouches[0].pageX < startX) {
                clearInterval(t);
                index = index == bottom.children.length - 1 ? 0 : index + 1;
                bottom.children[index].click();
                t = setInterval(slider, 3000);
            }
        })

        let index = 0;
        function slider() {
            bottom.children[index].click();
            index = (index + 1) % bottom.children.length;
        }

        //定时器,间隔一定时间自动播放
        slider();
        let t = setInterval(slider, 3000);

        //将obj水平移动到target位置
        function move(obj, target) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                let step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetLeft == target)
                    clearInterval(obj.timer);
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15);
        }

    script>
body>

html>

你可能感兴趣的:(前端)