JS透明度和水平轮播源码

本文分享了如何使用JavaScript实现元素的透明度变化和水平轮播效果,包括关键代码实现、交互逻辑和优化技巧,适用于网页动态展示和用户体验提升。
摘要由CSDN通过智能技术生成

JS透明度轮播

<!DOCTYPE html>
<html lang="en">

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

        ul {
            list-style: none;
        }

        .slider {
            width: 700px;
            height: 400px;
            border: 5px solid crimson;
            position: relative;
        }

        .slider .list .item {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
        }

        .slider .list .item:first-of-type {
            opacity: 1;
        }

        .slider .list .item img {
            width: 700px;
            height: 400px;
            display: block;
        }

        .slider .next,
        .slider .prev {
            position: absolute;
            top: 190px;
        }

        .slider .next {
            right: 0;
        }

        .slider .pagination {
            position: relative;
            top: 300px;
        }

        .slider .pagination .bullet {
            width: 20px;
            height: 20px;
            background-color: #000;
            margin-left: 5px;
            float: left;
            border: 2px solid yellow;
            cursor: pointer;
            color: white;
            line-height: 20px;
            text-align: center;
        }

        .slider .pagination .bullet.focus {
            background-color: hotpink;
        }
    </style>
</head>

<body>
    <div class="slider">
        <ul class="list">
            <li class="item"><img src="img(1).jpg" alt=""></li>
            <li class="item"><img src="img(2).jpg" alt=""></li>
            <li class="item"><img src="img(3).jpg" alt=""></li>
            <li class="item"><img src="img(4).jpg" alt=""></li>
        </ul>
        <button class="prev">prev</button>
        <button class="next">next</button>
        <ul class="pagination">
            <li class="bullet focus">1</li>
            <li class="bullet">2</li>
            <li class="bullet">3</li>
            <li class="bullet">4</li>
        </ul>
    </div>
    <script>
        function getStyle(el, property) {
            if (getComputedStyle) {
                return getComputedStyle(el)[property];
            } else {
                return el.currentStyle[property];
            }
        }
        function animate(el, properties) {
            clearInterval(el.timerId);
            el.timerId = setInterval(function () {
                for (var property in properties) {
                    var current;
                    var target = properties[property];
                    if (property === 'opacity') {
                        current = Math.round(parseFloat(getStyle(el, 'opacity')) * 100);
                    } else {
                        current = parseInt(getStyle(el, property));
                    }
                    var speed = (target - current) / 30;

                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if (property === 'opacity') {
                        el.style.opacity = (current + speed) / 100;
                    } else {
                        el.style[property] = current + speed + 'px';
                    }
                }
            }, 20)
        }
    </script>
    <script>
        (function () {
            var prevIndex, nextIndex;
            var len;
            var id;
            init();
            function init() {
                prevIndex = nextIndex = 0;
                len = document.querySelectorAll('.list .item').length;

                document.querySelector('.prev').onclick = function () {
                    sliderPrev();
                }

                document.querySelector('.next').onclick = function () {
                    sliderNext();
                }

                var bullets = document.querySelectorAll('.slider .pagination .bullet');
                for (var i = 0; i < bullets.length; i++) {
                    bullets[i].index = i;
                    bullets[i].onclick = function () {
                        prevIndex = nextIndex;
                        nextIndex = this.index;
                        sliderTo(prevIndex, nextIndex);
                    }
                }
                var slider = document.querySelector('.slider');
                slider.onmouseover = function () {
                    stop();
                }
                slider.onmouseout = function () {
                    auto();
                }
                auto();
            }
            function sliderPrev() {
                prevIndex = nextIndex;
                nextIndex--;
                if (nextIndex === -1) {
                    nextIndex = len - 1;
                }
                sliderTo(prevIndex, nextIndex);
            }
            function sliderNext() {
                prevIndex = nextIndex;
                nextIndex++;
                if (nextIndex === len) {
                    nextIndex = 0;
                }
                sliderTo(prevIndex, nextIndex);
            }
            function sliderTo(prev, next) {
                var lis = document.querySelectorAll('.list .item');
                var bullets = document.querySelectorAll('.slider .pagination .bullet');
                bullets[prev].className = 'bullet';
                bullets[next].className = 'bullet focus';
                animate(lis[prev], { opacity: 0 });
                animate(lis[next], { opacity: 100 });
            }
            function auto() {
                clearInterval(id);
                id = setInterval(function () {
                    sliderNext();
                }, 2000)
            }
            function stop() {
                clearInterval(id);
            }
        })()
    </script>
</body>

</html>

JS水平轮播

<!DOCTYPE html>
<html lang="en">

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

        ul {
            list-style: none;
        }

        .slider {
            width: 700px;
            height: 400px;
            position: relative;
            overflow: hidden;
        }

        .slider .list {
            position: absolute;
        }

        .slider .list .item {
            width: 700px;
            height: 400px;
            float: left;
        }

        .slider .list .item img {
            width: 700px;
            height: 400px;
            display: block;
        }

        .slider .prev,
        .slider .next {
            position: absolute;
            top: 190px;
        }

        .slider .next {
            right: 0;
        }

        .slider .pagination .bullet {
            width: 20px;
            height: 20px;
            background-color: #000;
            margin-left: 5px;
            top: 300px;
            z-index: 999;
            position: relative;
            float: left;
            cursor: pointer;
        }

        .focus {
            background-color: yellow !important;
        }
    </style>
</head>

<body>
    <div class="slider">
        <ul class="list">
            <li class="item"><img src="img(1).jpg" alt=""></li>
            <li class="item"><img src="img(2).jpg" alt=""></li>
            <li class="item"><img src="img(3).jpg" alt=""></li>
            <li class="item"><img src="img(4).jpg" alt=""></li>
        </ul>
        <button class="prev">prev</button>
        <button class="next">next</button>
        <ul class="pagination">
            <li class="bullet focus"></li>
            <li class="bullet"></li>
            <li class="bullet"></li>
            <li class="bullet"></li>
        </ul>
    </div>
    <script>
        function getStyle(el, property) {
            if (getComputedStyle) {
                return getComputedStyle(el)[property];
            } else {
                return el.currentStyle[property];
            }
        }
        function animate(el, properties) {
            console.log(el)
            clearInterval(el.timerId);
            el.timerId = setInterval(function () {
                for (var property in properties) {
                    var current;
                    var target = properties[property];
                    if (property === 'opacity') {
                        current = Math.round(parseFloat(getStyle(el, 'opacity')) * 100);
                    } else {
                        current = parseInt(getStyle(el, property));
                    }
                    var speed = (target - current) / 30;

                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if (property === 'opacity') {
                        el.style.opacity = (current + speed) / 100;
                    } else {
                        el.style[property] = current + speed + 'px';
                    }
                }
            }, 20)
        }
    </script>
    <script>
        (function () {
            var currIndex;
            var lis;
            var liWidth;
            var len;
            var id;
            init();
            function init() {
                currIndex = 1;
                var li_1 = document.querySelector('.slider .list .item:first-of-type');
                var copy_1 = li_1.cloneNode(true);
                var li_last = document.querySelector('.slider .list .item:last-of-type');
                var copy_last = li_last.cloneNode(true);

                var list = document.querySelector('.slider .list');
                list.appendChild(copy_1);
                list.insertBefore(copy_last, li_1);

                lis = document.querySelectorAll('.list .item');
                liWidth = lis[0].offsetWidth;
                len = lis.length;

                list.style.width = liWidth * len + 'px';
                list.style.left = -liWidth + 'px';

                document.querySelector('.prev').onclick = function () {
                    sliderPrev();
                }
                document.querySelector('.next').onclick = function () {
                    sliderNext();
                }

                var bullets = document.querySelectorAll('.slider .pagination .bullet');
                for (var i = 0; i < bullets.length; i++) {
                    bullets[i].index = i;
                    bullets[i].onclick = function () {
                        currIndex = this.index + 1;
                        sliderTo(currIndex);
                    }
                }

                document.querySelector('.slider').onmouseover = function () {
                    stop();
                }
                document.querySelector('.slider').onmouseout = function () {
                    auto();
                }
                auto();
            }
            function sliderNext() {
                currIndex++;
                sliderTo(currIndex);
            }
            function sliderPrev() {
                currIndex--;
                sliderTo(currIndex);
            }
            function sliderTo(index) {
                var list = document.querySelector('.slider .list');
                if (index === len) {
                    currIndex = index = 2;
                    list.style.left = -liWidth + 'px';
                }
                if (index === -1) {
                    currIndex = index = len - 3;
                    list.style.left = -(len - 2) * liWidth + 'px';
                }

                var focusIndex;
                var bullets = document.querySelectorAll('.pagination .bullet');
                if (index === 0) {
                    focusIndex = bullets.length - 1;
                } else if (index === len - 1) {
                    focusIndex = 0;
                } else {
                    focusIndex = index - 1;
                }
                document.querySelector('.focus').className = 'bullet';
                bullets[focusIndex].className = 'bullet focus';

                var left = -index * liWidth;
                animate(list, {
                    left: left
                })

            }
            function auto() {
                clearInterval(id);
                id = setInterval(function () {
                    sliderNext();
                }, 2000);
            }
            function stop() {
                clearInterval(id);
            }
        })()
    </script>
</body>

</html>

img(1).jpg
在这里插入图片描述
img(2).jpg
在这里插入图片描述
img(3).jpg
在这里插入图片描述
img(4).jpg
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值