js 轮播图

目录

一、html、css部分

二、js部分

三、animate动画函数部分

四、效果


分为以下步骤:

1、鼠标经过显示左右箭头,鼠标离开隐藏左右箭头。

2、循环遍历按钮,给按钮添加点击事件。设置高亮类名

3、给左右箭头添加点击事件,点击右箭头left增加,点击左箭头left减少。并添加节流阀。

4、添加计时器,重复右箭头操作。并在鼠标经过时停止定时器,鼠标离开时开启定时器

轮播图效果集合了前面学的大部分知识点,可以很好的练习。话不多说,上代码

一、html、css部分

<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 导入动画函数 -->
  <script src="./js/animate.js"></script>
  <!-- 导入js文件 -->
  <script src="./js/index.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    /* 大盒子 */
    .banner {
      margin: 0 auto;
      position: relative;
      width: 610px;
      height: 340px;
      background-color: blue;
      overflow: hidden;
    }

    /* ul */
    .ul {
      position: absolute;
      left: 0;
      top: 0;
      list-style: none;
      width: 500%;
    }

    .ul li {
      float: left;

    }

    .ul li img {
      width: 610px;
      height: 340px;
    }

    /* ol */
    .ol {
      list-style: none;
      position: absolute;
      bottom: 10px;
      left: 50%;
      transform: translateX(-70%);
    }

    .ol li {
      float: left;
      margin-left: 5px;
      width: 13px;
      height: 13px;
      background-color: #fff;
      border-radius: 50%;
      cursor: pointer;
      transition: all .3s;
    }

    /* 小圆圈选中状态 */
    .active {
      background-color: #000 !important;
    }

    /* 左右箭头 */
    .left-arrow,
    .right-arrow {
      display: none;
      position: absolute;
      cursor: pointer;
      text-align: center;
      line-height: 30px;
      width: 35px;
      height: 30px;
      background-color: #fff;
      z-index: 2;
      top: 50%;
      transform: translateY(-50%);
      transition: all .2s;
    }

    .left-arrow:hover,
    .right-arrow:hover {
      font-size: 20px;
      opacity: .8;
      line-height: 40px;
      width: 45px;
      height: 40px;
    }

    .left-arrow {
      left: 0;
      border-radius: 0 50px 50px 0;
    }

    .right-arrow {
      right: 0;
      border-radius: 50px 0 0 50px;
    }
  </style>
</head>

<body>

  <div class="banner">
    <span class="left-arrow">←</span>
    <span class="right-arrow">→</span>
    <ul class="ul">
      <li><img src="./img/1.jpg" alt="1"></li>
      <li><img src="./img/2.jpg" alt="2"></li>
      <li><img src="./img/3.jpg" alt="3"></li>
      <li><img src="./img/4.jpg" alt="4"></li>
    </ul>
    <ol class="ol">
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ol>
  </div>
</body>

</html>

二、js部分

// 页面加载完事件 load
window.addEventListener('load', function () {
  // 最大盒子
  var banner = document.querySelector('.banner')
  // 右箭头
  var left_arrow = document.querySelector('.left-arrow')
  // 左箭头
  var right_arrow = document.querySelector('.right-arrow')
  var ul = document.querySelector('.ul')
  // 计数器
  var sum = 0
  // 按钮索引 记录按钮当前高亮
  var state = 0
  // 节流阀 使动画执行完毕才能点击下一张
  var flag = true
  // 1、经过大盒子 右箭头左箭头显示隐藏
  banner.addEventListener('mouseenter', function () {
    left_arrow.style.display = 'block'
    right_arrow.style.display = 'block'
    // 鼠标经过关闭定时器
    clearInterval(timer)
    // 清空定时器
    timer = null
  })

  banner.addEventListener('mouseleave', function () {
    left_arrow.style.display = 'none'
    right_arrow.style.display = 'none'
    // 鼠标离开开启定时器
    timer = setInterval(function () {
      right_arrow.click() // 手动点击
    }, 2000)
  })

  // 2、获取按钮 排他思想 干掉其他人 留下我自己
  var ol = document.querySelector('.ol')
  ol.children[0].className = 'active'
  // 循环注册点击事件
  for (var i = 0; i < ol.children.length; i++) {
    // 给每个按钮设置index属性
    ol.children[i].setAttribute('index', i)
    ol.children[i].onclick = function () {
      // 干掉其他人
      for (var i = 0; i < ol.children.length; i++) {
        ol.children[i].className = ''
      }
      // 留下我自己
      this.className = 'active'
      // 获取当前按钮的index属性
      var index = this.getAttribute('index')
      sum = index
      state = index
      animate(ul, -index * ul.children[0].offsetWidth)
    }
  }
  // 克隆第一张图片
  var img = ul.children[0].cloneNode(true)
  // 添加到最后
  ul.appendChild(img)

  // 3、点击右箭头 left值增加
  // 计数器 left值 = sum * img的大小
  right_arrow.addEventListener('click', function () {
    if (flag) {
      // 关闭节流阀
      flag = false
      // 如果计数器等于 4 清空计数器 清空按钮状态 left等于0
      if (sum == ul.children.length - 1) {
        sum = 0
        state = 0
        ul.style.left = 0
      }
      // 每点击一次sum、state加1 更新当前状态
      sum++
      state++
      // 动画
      animate(ul, -ul.children[0].offsetWidth * sum, function () {
        // 动画执行完毕开启节流阀
        flag = true
      })

      oltate()
    }
  })

  // 3、点击左箭头 left值减少
  // 计数器 left值 = sum * img的大小

  left_arrow.addEventListener('click', function () {
    if (sum == 0) {
      sum = 4
      state = 4
    }
    // 每点击一次sum、state减1 更新当前状态
    sum--
    state--
    // 动画
    animate(ul, -ul.children[0].offsetWidth * sum)

    oltate()
  })

  function oltate() {
    // 清空所有btn类名
    for (var i = 0; i < ol.children.length; i++) {
      ol.children[i].className = ''
    }
    // 如果按钮到了最后一个 下一个切换到第一个,否则正常运行
    if (state == 4) {
      ol.children[0].className = 'active'
    } else {
      ol.children[state].className = 'active'
    }
  }

  // 4、自动轮播 相当于重复右箭头操作
  var timer = setInterval(function () {
    clearInterval(timer) // 关闭上一个定时器
    right_arrow.click() // 手动点击
  }, 2000)
})

三、animate动画函数部分

function animate(obj, target, callback) {
  /**
   * obj:对象
   * target:目标距离
   * callback:回调函数
   */
  clearInterval(obj.timer)
  obj.timer = setInterval(function () {
    var step = (target - obj.offsetLeft) / 10
    step = step < 0 ? Math.floor(step) : Math.ceil(step)
    obj.style.left = obj.offsetLeft + step + 'px'

    if (obj.offsetLeft == target) {
      clearInterval(obj.timer)
      // 判断是否传入了回调函数
      // if (callback) {
      //   callback();
      // }

      callback && callback()
    }
  }, 10)
}

四、效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript轮可以通过以下步骤来实现: 1. 创建一个HTML结构,包含轮容器和轮项。 2. 使用CSS样式来设置轮容器和轮项的样式,如宽度、高度、位置、层级等。 3. 在JavaScript中获取轮容器和轮项,并根据需要设置初始状态。 4. 使用定时器或事件监听器来控制轮的切换,如定时切换、按钮点击切换、鼠标悬停切换等。 5. 切换时需要设置轮项的样式,如透明度、位置、过渡效果等。 6. 可以添加一些特效或功能,如自动放、无限循环、指示器、缩略等。 以下是一个简单的JavaScript轮示例: ```html <div class="carousel"> <div class="carousel-item active"><img src="1.jpg"></div> <div class="carousel-item"><img src="2.jpg"></div> <div class="carousel-item"><img src="3.jpg"></div> <div class="carousel-item"><img src="4.jpg"></div> </div> ``` ```css .carousel { position: relative; width: 600px; height: 400px; overflow: hidden; } .carousel-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s ease-in-out; } .carousel-item.active { opacity: 1; } ``` ```javascript var carousel = document.querySelector('.carousel'); var items = carousel.querySelectorAll('.carousel-item'); var index = 0; var timer = null; function showItem(index) { for (var i = 0; i < items.length; i++) { items[i].classList.remove('active'); } items[index].classList.add('active'); } function nextItem() { index++; if (index >= items.length) { index = 0; } showItem(index); } timer = setInterval(nextItem, 3000); carousel.addEventListener('mouseover', function() { clearInterval(timer); }); carousel.addEventListener('mouseout', function() { timer = setInterval(nextItem, 3000); }); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值