轮播图html

题十二:轮播图

要求:

1.鼠标不在图片上方时,进行自动轮播,并且左右箭头不会显示;当鼠标放在图片上方时,停止轮播,并且左右箭头会显示;

2.图片切换之后,图片中下方的小圆点会同时进行切换,并且点击相应的小圆点可以切换到相应的图片上;

3.点击左右箭头可以进行左右图片的切换;

4.图片上需有介绍的文字,会随图片切换一起进行切换。

原理:

  1. 当鼠标进入时,左走箭头出现,定时器关闭,离开时,箭头消失,定时器开启。

  2. 写DOM对应的小圆点,找让图片文字小圆点的高亮移除,再当索引值到了之后,添加高亮事件.

  3. 当点击左图右图是分别修改其对应的索引号.

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF - 8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>自定义轮播图</title> <style> #container { margin: 0 auto; position: relative; width: 600px; height: 600px; overflow: hidden; } #container img { width: 100%; height: 100%; background-size: cover; display: none; } #container img.active { display: block; } .arrow { position: absolute; top: 50%; transform: translateY(-50%); font-size: 30px; color: white; background: rgba(0, 0, 0, 0.3); padding: 10px 15px; cursor: pointer; display: none; } #left { left: 10px; } #right { right: 10px; } #dots { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); display: flex; } #dots span { width: 10px; height: 10px; border-radius: 50%; background: gray; margin: 0 5px; cursor: pointer; } #dots span.active { background: white; } .name { position: absolute; bottom: 0; width: 100%; background: rgba(0, 0, 0, 0.5); color: white; padding: 10px; text-align: center; font-size: 30px; } </style> </head> <body>
    图片1
    fall'in out
    图片2
    找自己
    图片3
    单车恋人
    < >
     <script>
         const Container = document.querySelector('#container')
         const images = document.querySelectorAll('#container img')
         const names = document.querySelectorAll('.name')
         const dotsContainer = document.getElementById('dots')
         const Left = document.querySelector('#left')
         const Right = document.querySelector('#right')
         //当前显示图片的索引
         let currentIndex = 0
         //命名自动播放定时器
         let autoplayInterval
    
          function onMouseEnter() {
             //鼠标一进入就关闭定时器
             clearInterval(autoplayInterval)
             Left.style.display = 'block'
             Right.style.display = 'block'
         }
         function onMouseLeave() {
             //开启
             startAutoplay()
             Left.style.display = 'none'
             Right.style.display = 'none'
         }
         Left.addEventListener('click', () => {
             clearInterval(autoplayInterval)
             //一定要调用
             leftImage()
         });
         Right.addEventListener('click', () => {
             clearInterval(autoplayInterval)
             rightImage()
         })
    
         Container.addEventListener('mouseenter', function() {
             onMouseEnter()
         })
         Container.addEventListener('mouseleave', function() {
             onMouseLeave()
         })
         
         // 显示指定索引的图片
         function showImage(index) {
             images.forEach((img, i) => {
                 //先全部移除高亮
                 img.classList.remove('active')
                 //文字也先不显示
                 names[i].style.display = 'none'
             })
             dotsContainer.querySelectorAll('span').forEach((dot, i) => {
                 //小圆点高亮移除
                 dot.classList.remove('active')
             })
             //到相应的索引号,高亮和文字显示
             images[index].classList.add('active')
             names[index].style.display = 'block'
             //小圆点高亮添加
             dotsContainer.querySelectorAll('span')[index].classList.add('active')
             //修改索引号
             currentIndex = index
         }
    
         function createDots() {
             //回调函数,函数对应的DOM元素及索引号
             images.forEach((_, index) => {
                 //新建小圆点,有几个DOM建几个
                 const dot = document.createElement('span')
                 if (index === 0) {
                     //默认高亮类名
                     dot.classList.add('active')
                 }
                 dot.addEventListener('click', function() {
                     //一旦点击,关闭定时器
                     clearInterval(autoplayInterval)
                     showImage(index)
                 })
                 //根据DOM数量,添加小圆点
                 dotsContainer.appendChild(dot)
             });
         }
         // 右图
         function rightImage() {
             //index+1
             currentIndex = (currentIndex + 1) % images.length
             showImage(currentIndex)
         }
    
         // 左图
         function leftImage() {
             //同理
             currentIndex = (currentIndex - 1 + images.length) % images.length
             showImage(currentIndex);
         }
    
         function startAutoplay() {
             autoplayInterval = setInterval(rightImage, 1500)
         }
    
         // 调用小圆点和自动播放,使页面一开始就有效果
         createDots()
         startAutoplay()
       
     </script>
    
    </body> </html>
相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_4 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome
鱼樱前端5 小时前
深入JavaScript引擎与模块加载机制:从V8原理到模块化实战
前端·javascript