轮播图写法

1.Jquery:

JS:

const $spans = $('.span>span')

console.log($spans);


//jq里的foreach是each:
//参数1:需要遍历的嘟嘟
$.each($spans, function () {

    //小圆点
    $(this).mouseover(function () {

        //增加小圆点active
        $(this).addClass('active').siblings().removeClass('active')

        //100%移动图片
        $('.img>img').css('left', `-${$spans.index(this)}00%`) //xx.index(this) 0/1/2...

    })
})

//上一页按钮:
$('.btn>button').eq(0).click(function () {

    //不能这么写:jq里的prev()是个数组,不是单纯的null/undefined
    // if (!$('.active').prev()) {
    //     $('.active').prev() = $spans[2]
    // }


    //函数的执行结果不能作为左边的值!!!!!!!
    let before = $('.active').prev()

    //函数的执行结果不能作为左边的值!!!!!!!
    //  if ($('.active').prev().length === 0) 不能这么写
    if (before.length === 0) {
        before = $spans.last()
    }
    before.mouseover()
})

//下一页按钮:
$('.btn>button').eq(1).click(function () {

    let after = $('.active').next()

    if (after.length === 0) {
        after = $spans.first()
    }
    after.mouseover()
})

//定时器:
//需写在变量里
let interval = setInterval(() => {
    $('.btn>button').eq(1).click()
}, 2000)

//指针放上时停止定时器
$('.img>img').mouseover(function () {
    clearInterval(interval)
})

//指针挪走时开启定时器
$('.img>img').mouseleave(function () {
    //一定要这么写,不然就坏了
    interval = setInterval(() => {
        $('.btn>button').eq(1).click()
    }, 2000)
})

HTML:

<!-- 轮播图 -->
<div>
    <div class="img">
        <img src="/pet/pic/img/home/carousel/test1.jpg" alt="">
        <img src="/pet/pic/img/home/carousel/test2.jpg" alt="">
        <img src="/pet/pic/img/home/carousel/test3.jpg" alt="">
    </div>
    <div class="btn">
        <button>上一页</button>
        <button>下一页</button>
    </div>

    <p>================111</p>
    <div class="span">
        <span class="active">1</span>
        <span>2</span>
        <span>3</span>

    </div>

</div>

2.swiper:

HTML:

<div class="swiper">

        <!-- 图片 -->
        <div class="swiper-wrapper">

            <!-- 循环 -->
            <div class="swiper-slide">
                <img src="/pet/pic/img/home/carousel/test1.jpg" alt="">
                <p>====穿衣服的柯基====</p>
            </div>
            <div class="swiper-slide">
                <img src="/pet/pic/img/home/carousel/test2.jpg" alt="">
                <p>====揣进袋子的柯基====</p>
            </div>
            <div class="swiper-slide">
                <img src="/pet/pic/img/home/carousel/test3.jpg" alt="">
                <p>====歪头杀的柯基====</p>
            </div>
            <div class="swiper-slide">
                <img src="/pet/pic/img/home/carousel/test4.png" alt="">
                <p>====乱入的边牧====</p>
            </div>

        </div>

        <!-- 分页器点点 -->
        <div class="swiper-pagination"></div>

        <!-- 前后按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>

    </div>

CSS注意事项:

//轮播点点,固定的class
.swiper-pagination-bullet{
    background-color: var(--colorMain4);                
}

//轮播按钮
.swiper-button-prev,.swiper-button-next{
    color: var(--colorMain4);
}

//如果想在img上面覆盖文字,需要绝对定位弄出来
p{
    position:absolute;
}

JS:

var mySwiper = new Swiper('.swiper', {
    //自动播放
    autoplay: {
        delay: 2000,//间隔
        pauseOnMouseEnter: true,//鼠标进入时,暂时停止
        disableOnInteraction: false,//鼠标离开时,继续播放
    },

    //分页器点点
    pagination: {
        el: '.swiper-pagination',
        clickable: true,//点击小圆点可以切换
    },

    //前后按钮
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值