jQuery轮播图

博客展示了JavaScript和jQuery相关的效果图,并给出了代码示例,聚焦于这两种前端技术的实际呈现。

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

效果图:
在这里插入图片描述
代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  *{
    margin: 0;
    padding: 0;
  }
  li{
    list-style: none;
  }
  .box{
    position: relative;
    width: 600px;
    height: 400px;
    border: 1px solid;
    margin: 100px auto;
    overflow: hidden;
  }

  .img{
    width:2500px;
    height: 400px;
    /* 不用js里animate时用它会看着舒服一些 */
    transition: all 1.5s;
  }
  .img li{
    float: left;
    /* display: none; */
  }
  .img li img{
    width: 600px;
    height: 400px;
  }
  .prev,.next{
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 64px;
    height: 64px;
    cursor: pointer;
    line-height: 30px;
    text-align: center;
  }
  .prev{
    left: 0;
  }
  .next{
    right: 0;
  }
  .page{
    list-style: none;
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform:translateX(-50%);
  }
  .page li{
    width: 20px;
    height: 20px;
    float: left;
    background-color: #500;
    border-radius: 50%;
    margin-left: 10px;
  }
  .page li.current{
    background-color: red;
  }
</style>
</head>
<body>
  <div class="box">
    <ul class="img">
      <li><img src="images/pic1.png" alt=""></li>
      <li><img src="images/pic2.png" alt=""></li>
      <li><img src="images/pic3.png" alt=""></li>
      <li><img src="images/pic4.png" alt=""></li>
    </ul>
    <div class="prev"><img src="images/left.png" alt=""></div>
    <div class="next"><img src="images/right.png" alt=""></div>
    <ul class="page">
      <li class="current"></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
</body>
</html>
<script src="jquery.js" charset="utf-8"></script>
<script type="text/javascript">
  class Banner{
    constructor(){
      this.index = 0;
      this._time = null;
      this.width = $(".box").width();
    }
    //设置图片
    setPic(){
      // $(".img").animate({marginLeft:`-${this.width*this.index}px`},2000,"linear");//有点太连续了,感觉不太好
      $(".img").css({marginLeft:`-${this.width*this.index}px`});
      $(".page li").eq(this.index).addClass('current').siblings('').removeClass('current');
    }
    //设置索引
    setIndex(){
      if(this.index > 3){
        this.index = 0
      }
      if(this.index < 0){
        this.index = 3;
      }
      this.setPic();
    }
    //设置定时器
    setTime(){
      clearInterval(this._time);
      let that = this;
      this._time = setInterval(function(){
        that.index++;
        that.setIndex();
      },2000);
    }
    //前一张
    prev(){
      this.index--;
      this.setIndex();
    }
    //下一张
    next(){
      this.index++;
      this.setIndex();
    }
    //添加事件
    eventBind(){
      let that = this;
      $(".prev").click(function(){
        clearInterval(that._time);
        that.prev();
        that.setTime();
      });
      $(".next").click(function(){
        clearInterval(that._time);
        that.next();
        that.setTime();
      });
      $(".page li").mouseover(function(){
        clearInterval(that._time);
        that.index = $(this).index();
        that.setIndex();
      });
      $(".page li").mouseout(function(){
        that.index = $(this).index();
        that.setTime();
      });
    }
  }
  let b = new Banner();
  b.setTime();
  b.eventBind();
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值