jq的方法写一个轮播特写

在网页中轮播是经常遇到大页面效果之一,在平时都是使用组件直接调用。但是其实用jq写的话企事业很简单。代码如下:

<!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;
            list-style: none;
        }

        .container {
            width: 700px;
            position: relative;
            margin: 100px auto;
            align-items: center;
            display: flex;
            justify-content: center;
        }

        .container img {
            width: 700px;
        }

        .imgs li {
            display: none;
        }

        .imgs li:first-child {
            display: block;
        }

        .indexes {
            position: absolute;
            bottom: 10px;
            display: flex;
            height: 20px;
        }

        .indexes li {
            width: 15px;
            height: 15px;
            background: #fff;
            margin: 0 10px;
        }

        .indexes li.active {
            background: red;
        }

        .arrow {
            height: 50px;
            width: 30px;
            position: absolute;
            background: #000;
        }

        .arrow.arrowleft {
            left: -50px
        }

        .arrow.arrowright {
            right: -50px
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="arrow arrowleft"></div>
        <div class="arrow arrowright"></div>

        <ul class="imgs">
            <li>
                <img src="http://p1.music.126.net/UbZ8uE01SIY-Jmo8c3fGEQ==/109951165097027317.jpg?imageView&quality=89">
            </li>
            <li>
                <img src="http://p1.music.126.net/U4aWX25bkqj1A4bKHaeNAQ==/109951165097467733.jpg?imageView&quality=89">
            </li>
            <li>
                <img src="http://p1.music.126.net/WDK1Xf03KHiwLEi_PWvgoQ==/109951165097312543.jpg?imageView&quality=89">
            </li>
            <li>
                <img src="http://p1.music.126.net/w9_4EDtBaLAsg8E3k11Rkw==/109951165097376119.jpg?imageView&quality=89">
            </li>
        </ul>
        <div class="indexes">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(function () {
            let i = 0

            let timer = null

            function run() {
                timer = setInterval(() => {
                    if (++i === 4) i = 0
                    $('.imgs li').eq(i).show().siblings().hide()
                    $('.indexes li').eq(i).addClass('active').siblings('.active').removeClass('active')
                }, 2000);
            }

            run()

            $('.arrowright').click(function () {
                clearInterval(timer)
                if (++i > 3) i = 0
                $('.imgs li').eq(i).show().siblings().hide()
                $('.indexes li').eq(i).addClass('active').siblings('.active').removeClass('active')
                run()
            })

            $('.arrowleft').click(function () {
                clearInterval(timer)
                if (--i < 0) i = 3
                $('.imgs li').eq(i).show().siblings().hide()
                $('.indexes li').eq(i).addClass('active').siblings('.active').removeClass('active')
                run()
            })

            $('.indexes li').click(function () {
                clearInterval(timer)
                i = $(this).index()
                $('.imgs li').eq(i).show().siblings().hide()
                $('.indexes li').eq(i).addClass('active').siblings('.active').removeClass('active')
                run()
            })

        })
    </script>
</body>

</html>

相比于用js原生的写法代码量明显少了很多。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
由于jq主要是用于操作DOM,实现三维轮播图需要使用CSS3 3D transforms和一些jQuery的动画方法。下面是一个简单的例子: HTML结构: ```html <div class="carousel"> <div class="carousel-item"> <img src="image1.jpg"> </div> <div class="carousel-item"> <img src="image2.jpg"> </div> <div class="carousel-item"> <img src="image3.jpg"> </div> </div> ``` CSS样式: ```css .carousel { width: 600px; height: 400px; margin: 0 auto; position: relative; perspective: 1000px; /* 设置透视 */ } .carousel-item { width: 100%; height: 100%; position: absolute; top: 0; left: 0; transform-style: preserve-3d; /* 设置为3D空间 */ transition: transform 1s; /* 动画过渡效果 */ } .carousel-item img { width: 100%; height: 100%; display: block; } .carousel-item.active { transform: translateZ(0) rotateY(0); /* 设置当前显示的图片为正面 */ } .carousel-item.right { transform: translateZ(-200px) rotateY(-90deg); /* 设置右侧图片为侧面 */ } .carousel-item.left { transform: translateZ(-200px) rotateY(90deg); /* 设置左侧图片为侧面 */ } ``` jQuery脚本: ```js $(document).ready(function() { var carousel = $(".carousel"); var items = carousel.find(".carousel-item"); var currentIndex = 0; var total = items.length; function rotateCarousel() { var currentItem = items.eq(currentIndex); var nextIndex = (currentIndex + 1) % total; var nextItem = items.eq(nextIndex); currentItem.removeClass("active").addClass("left"); nextItem.removeClass("right").addClass("active"); setTimeout(function() { currentItem.removeClass("left"); }, 1000); setTimeout(function() { nextItem.addClass("right"); }, 1000); currentIndex = nextIndex; } setInterval(rotateCarousel, 3000); }); ``` 上面的代码中,我们首先获取了轮播图的容器和所有的图片元素,然后定义了一个currentIndex变量来记录当前显示的图片索引,使用setInterval函数来定时调用rotateCarousel函数来实现轮播效果。在rotateCarousel函数中,我们首先获取当前显示的图片元素和下一张图片元素,然后分别给它们添加或移除类名来实现3D旋转的效果。具体来说,我们将当前显示的图片向左边旋转并设置为侧面,将下一张图片向右边旋转并设置为正面,最后更新currentIndex变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值