轮播图 opacity

利用透明度来实现轮播图效果
话不多说直接上代码:


<!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>
        * {
            padding: 0;
            margin: 0;
        }
        *::selection {
            background: none;
        }
        div {
            width: 800px;
            height: 500px;
            margin: 50px auto;
            position: relative;
        }
        ul li {
            list-style: none;
            opacity: 0;
            position: absolute;
            transition: all .8s;
        }
        ul li:nth-child(1) {
            opacity: 1;
        }
       ol {
            position: absolute;
            bottom: 20px;
            left: 35%;
        }
        ol li {
            list-style: none;
            width: 20px;
            height: 20px;
            border: 1px solid white;
            line-height: 20px;
            float: left;
            margin-right: 10px;
            text-align: center;
            border-radius: 50%;
            cursor: pointer;
        }
        .yellow {
            background-color: yellow;
        }
        img {
            width: 800px;
            height: 500px;
            }
        .prve,
        .next {
            position: absolute;
            width: 50px;
            height: 20px;
            background-color: rgba(1, 0, 0, .7);
            top: 50%;
            margin-top: -25px;
            color: white;
            text-align: center;
            line-height: 20px;
            display: none;
        }
        .next {
            right: 0;
        }
        .prve:hover,
        .next:hover {
            cursor: pointer;
            background-color: rgba(1, 0, 0, .5);
        }
        .prve:active,
        .next:active {
            background-color: rgba(1, 0, 0, .3);
        }
    </style>
</head>

<body>
    <div>
        <ul>
            <li><img src="./img/f1.jpg" alt=""></li>
            <li><img src="./img/f2.jpg" alt=""></li>
            <li><img src="./img/f3.jpg" alt=""></li>
            <li><img src="./img/f4.jpg" alt=""></li>
            <li><img src="./img/f5.jpg" alt=""></li>
        </ul>
        <ol>
            <li class="yellow">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ol>
        <div class="prve">prve</div>
        <div class="next">next</div>
    </div>

    <script>
        var prve = document.getElementsByClassName('prve')[0]
        var next = document.getElementsByClassName('next')[0]
        var ullis = document.getElementsByTagName('ul')[0].getElementsByTagName('li')
        var ollis = document.getElementsByTagName('ol')[0].getElementsByTagName('li')
        var div = document.getElementsByTagName('div')[0]
        var index = 0

        div.onmouseenter = function () {
            prve.style.display = 'block'
            next.style.display = 'block'
            clearInterval(timer)
        }

        div.onmouseleave = function () {
            prve.style.display = 'none'
            next.style.display = 'none'
            timer = setInterval(function () {
                next.click()
            }, 1000)
        }
        next.onclick = function () {
            index++
            if (index == ullis.length) {

                index = 0
            }
            for (var i = 0; i < ullis.length; i++) {
                ullis[i].style.opacity = 0
                ollis[i].className = ''
            }
            ullis[index].style.opacity = 1
            ollis[index].className = 'yellow'
        }
        prve.onclick = function () {
            if (index == 0) {
                index = ullis.length
            }
            index--
            for (var i = 0; i < ullis.length; i++) {
                ullis[i].style.opacity = 0
                ollis[i].className = ''
            }
            ullis[index].style.opacity = 1
            ollis[index].className = 'yellow'
        }

        for (var i = 0; i < ollis.length; i++) {
            ollis[i].setAttribute('index', i)
            ollis[i].onclick = function () {
                for (var i = 0; i < ollis.length; i++) {
                    index = this.getAttribute('index')
                    ullis[index].style.opacity = 1
                    ollis[i].className = ''
                }
                this.className = 'yellow'
            }

        }

        var timer = setInterval(function () {
            next.click()
        }, 1000)
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值