js轮播图

本文介绍了一个使用HTML、CSS和JavaScript实现的轮播图组件,包括视频和图片切换、自动播放、左右箭头控制以及鼠标悬停效果。通过实例代码展示了如何创建轮播图并设置定时自动前进和用户交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html

<!-- 轮播图 -->
    <div class="banner">
        <div class="imgs">
            <a href="javascript:;">
                <video id="movie" muted src="" autoplay="autoplay"
                    loop></video>
            </a>
            <a href="javascript:;"><img class="swiper-img" src="../images/home/banner1.png" alt="图片加载失败"></a>
            <a href="javascript:;"><img class="swiper-img" src="../images/home/banner2.png" alt="图片加载失败"></a>
            <a href="javascript:;"><img class="swiper-img" src="../images/home/banner3.png" alt="图片加载失败"></a>
            <a href="javascript:;"><img class="swiper-img" src="../images/home/banner1.png" alt="图片加载失败"></a>
            <a href="javascript:;"><img class="swiper-img" src="../images/home/banner2.png" alt="图片加载失败"></a>
        </div>
        <div class="control">
            <div class="left"><i></i></div>
            <div class="right"><i></i></div>
        </div>
        <ul class="kinds"></ul>
    </div>

js

<script>

//轮播图
class testBanner {
    constructor() {
        this.imgs = $(".banner").find(".imgs").find("a");
        this.left = $(".banner").find(".control").find(".left");
        this.right = $(".banner").find(".control").find(".right");
        this.points = $(".banner").find(".kinds");
        this.aa = $(".banner").find(".imgs").find("a");
        this.delay = 3000;
        this.autoplay = true;
        this.current = 0;
        this.duration = 500;
        this.box = $(".banner");

        this.len = this.imgs.length;
        this.t = null;

        this.init()
    }
    init() {
        this.autoplay = this.autoplay == false ? false : true;
        this.delay = this.delay || 2000;
        this.current = this.current || 0;
        this.duration = this.duration || 500;
        this.addEvent();
        this.createCircle();
    }

    createCircle() {
        for (var i = 0; i < this.len; i++) {
            this.points.append('<li></li>');
        }
        this.points.find("li").eq(this.current).addClass("active").siblings().removeClass();
        this.hoverEvent();
        this.move();
    }

    auto() {
        var that = this;
        this.box.hover(function () {
            clearInterval(that.t);
        }, function () {
            that.t = setInterval(function () {
                that.next();
            }, that.delay)
        })
    }

    hoverEvent() {
        var that = this;
        this.points.find("li").on("mouseenter", function () {
            $(this).css('width', '24px').siblings().css('width', '14px');
            that.current = $(this).index();
            that.move();
        })
        this.points.find("li").on("mouseleave", function () {
            $(this).css('width', '14px');
            that.current = $(this).index();
            that.move();
        })
    }

    addEvent() {
        var that = this;
        this.left.on("click", function () {
            that.prev();
        });
        this.right.on("click", function () {
            that.next();
        });
    }

    next() {
        if (this.current == this.len - 1) {
            this.current = 0;
        } else {
            this.current++;
        }
        this.move();
    }
    prev() {
        if (this.current == 0) {
            this.current = this.len - 1;
        } else {
            this.current--;
        }
        this.move();
    }
    move() {
        var that = this;
		if(this.current == 0){
            clearInterval(that.t);
			that.t = setInterval(function () {
                that.next();
            }, 15000)
		}else{
            $('.swiper-img').eq(this.current -1).css({'width':'100%','height': '100%','left': '0','top': '0'})
            $('.swiper-img').eq(this.current -1).stop().animate({ width:'110%',height: '110%',left:'-5%',top: '-5%'},5000);
			clearInterval(that.t);
			that.t = setInterval(function () {
                that.next();
            }, that.delay)
        }
        this.points.find("li").eq(this.current).addClass("active").siblings().removeClass();
        this.aa.eq(this.current).stop().fadeIn(this.duration).siblings().stop().fadeOut(this.duration);
    }
}

new testBanner();

</script>

css

<style>
     /* 轮播图 */
  .banner {
    width: 100%;
     height: 26rem;
    /* height: 90%; */
    position: relative;
    cursor: pointer;
    flex: auto;
  }

  .banner .imgs {
    width: 100%;
    height: 90%;
    position: relative;
    overflow: hidden;
    flex: auto;
  }


  .banner .imgs img {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    display: none;
  }

  .banner .imgs img:nth-of-type(1) {
    display: block;
  }

  .banner .control .left,
  .banner .control .right {
    width: 40px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    position: absolute;
    cursor: pointer;
  }

  .banner .control .left {
    left: 80px;
    top: 40%;
  }

  .banner .control .left i {
    display: block;
    width: 100%;
    height: 100%;
    background: url(../images/home/banner-button-left.png) no-repeat;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  .banner .control .right {
    right: 80px;
    top: 40%;
  }

  .banner .control .right i {
    display: block;
    width: 100%;
    height: 100%;
    background: url(../images/home/banner-button-right.png) no-repeat;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  .banner .kinds {
    width: 128px;
    /* height: 36px; */
    display: flex;
    align-items: center;
    justify-content: space-around;
    position: absolute;
    bottom: 150px;
    left: 158px;
    z-index: 999999;
  }

  .banner .kinds li {
    display: inline-block;
    cursor: pointer;
    width: 14px;
    height: 8px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 4px;
  }

  .banner li.active {
    width: 24px!important;
    background: rgba(255, 255, 255, 1);
  }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值