利用jquery实现轮播图

 

 最近在看jquery东西,一时兴起纯手写了一份jquery版的轮博图,在此记录一下。。。

 

实现效果如下: 

 

 

 

代码如下:

 

 

<!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>
            .content {
                  position: relative;
            }

            .carousel {
                  position: relative;
                  width: 800px;
                  height: 350px;
                  margin: 0 auto;
            }

            .carousel>div:first-child {
                  overflow: hidden;
                  width: 800px;
                  height: 350px;
            }

            .carousel img {
                  width: 800px;
                  height: 350px;
            }

            .item ul {
                  display: flex;
                  position: absolute;
                  left: 50%;
                  bottom: 0;
                  transform: translateX(-50%)
            }

            .item ul li {
                  list-style: none;
                  width: 50px;
                  height: 15px;
                  background: rgba(0, 0, 0, 0.3);
                  margin: 0 20px;
            }

            .active {
                  background: orangered !important;
            }

            .left,
            .right {
                  position: absolute;
                  width: 30px;
                  height: 60px;
                  border: 1px solid #000;
                  font-weight: 900;
                  font-size: 22px;
                  line-height: 60px;
                  text-align: center;
            }

            .left {
                  left: -35px;
                  top: 50%;
                  transform: translateY(-50%);
            }

            .right {
                  right: -35px;
                  top: 50%;
                  transform: translateY(-50%);
            }
      </style>
</head>

<body>
      <div class="content">
            <div class="carousel">
                  <div>
                        <img src="https://aecpm.alicdn.com/simba/img/TB1XotJXQfb_uJkSnhJSuvdDVXa.jpg" alt="">
                        <img src="https://aecpm.alicdn.com/simba/img/TB183NQapLM8KJjSZFBSutJHVXa.jpg" alt="">
                        <img src="https://img.alicdn.com/tfs/TB16AtDhzMZ7e4jSZFOXXX7epXa-520-280.jpg_q90_.webp" alt="">
                        <img src="https://img.alicdn.com/tfs/TB1PQ00TUH1gK0jSZSyXXXtlpXa-520-280.png_q90_.webp" alt="">
                  </div>
                  <div class="left">&lt</div>
                  <div class="right">&gt</div>
            </div>
            <div class="item">
                  <ul>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                  </ul>
            </div>
      </div>
      <script src="./jquery-1.12.4.js"></script>
      <script>
            let i = 0
            function run() {
                  // 获取下标为 i 的图片,让下标为 i 的图片显示, 其他图片隐藏
                  $('.carousel img').eq(i).show().siblings().hide()
                  //  获取轮播图小标为 i 的小长方形按钮,给 i 下标的按钮添加类名active,删除其他元素的类名active
                  $('.item ul li').eq(i).addClass('active').siblings().removeClass('active')
            }
            function times() {// 每2s执行一次run函数
                  return setInterval(() => {
                        if (++i === 4) { i = 0 }
                        run()
                  }, 2000)
            }
            let timer = times() //调用

            //  获取轮播图小按钮,绑定鼠标事件
            $('.item ul li').mouseenter(function () {
                  clearInterval(timer) //清除计时器
                  i = $(this).index() // 获取当前鼠标所在的按钮下标
                  run()
            })

            //  获取轮播图小按钮,绑定鼠标离开事件,当鼠标离开后重新启动定时器
            $('.item ul li').mouseleave(function () {
                  timer = times()
            })


            // 左边< , 点击后图片向左轮播
            $('.left').click(function () {
                  clearInterval(timer)
                  if (--i < 0) {
                        i = 3
                  }
                  run()
                  timer = times()
            })

            // 右边> ,点击后图片向右边轮播
            $('.right').click(function () {
                  clearInterval(timer)
                  if (++i === 4) {
                        i = 0
                  }
                  run()
                  timer = times()
            })

      </script>
</body>

</html>

 

posted @ 2020-09-05 15:51  javascript9527  阅读(2094)  评论(0编辑  收藏  举报