当前位置: 首页 > news >正文

【轮播图】HTML+CSS+JavaScript实现轮播图

轮播图实现

第一步,构建html结构和css样式

<div class="header"><div class="content"></div></div><div class="center"><div class="box"><div class="first"><div class="firstChild1"><div class="firstChild1_box"><div class="firstChild1_box_left hover"><</div><div class="firstChild1_box_center"><!-- 这里可以进行for循环 --><!-- <img src="https://p5.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/62825130816/57e3/05be/dbaa/35ba17ba02d8485482440213c7bfb115.jpg?imageView&quality=89" alt=""> --><!-- <img src="https://p5.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/62821641259/9168/ff56/496a/c8e1f7f69f747c564cc5b8a91c9dd9cf.jpg?imageView&quality=89" alt=""> --><div class="login"><div class="card"><div class="title">登录</div><input type="text" class="input" placeholder="用户名"><input type="password" class="input" placeholder="密码"><input type="submit" class="button"></div></div></div><div class="firstChild1_box_right hover">></div></div></div><div class="firstChild2">4324243</div></div><div class="second ">323</div></div></div><div class="footer"></div>
<style>body{position: relative;height: 100vh;margin: 0;padding: 0;background-color: #ccc;}.header{border: 1px solid black;height: 70px;width: 100%;}.center{display: flex;justify-content: center;align-items: center;border: 1px solid black;width: 100%;}.box{/* 先设置宽度,然后之后再重新修改宽度 *//* width: 1200px; */border: 1px solid black;}.first{background-color: #fff;}.firstChild1{background-color: #d48080;}.firstChild1_box{display: flex;justify-content: center;align-items: center;}.firstChild1_box_center{display: flex;}.firstChild1_box_center .login{display: flex;justify-content: center;align-items: center;background-color: rgb(47, 43, 43);border: 1px solid black;}.firstChild1_box_center .login .card{display: flex;flex-direction: column;justify-content: space-evenly;width: 300px;background-color: gray;}.firstChild1_box_center .login .card .title{/* text-align: center; */margin: 20px;}.firstChild1_box_center .login .card .input{margin: auto;width: 200px;height: 18px;margin-bottom: 15px;}.firstChild1_box_center .login .card .button{margin: auto;width: 200px;margin-bottom: 20px;}.firstChild1_box_left{cursor: pointer;font-size: 30px;border: 1px solid black;}.firstChild1_box_right{cursor: pointer;font-size: 30px;border: 1px solid black;}.firstChild1_box_left:hover{background-color: #ccc;}.firstChild1_box_right:hover{background-color: #ccc;}.firstChild2{background-color: #c46e6e;}.second{background-color: #7b6363;}.footer{position: fixed;bottom: 0;width: 100%;height: 70px;border: 1px solid black;}</style>

第二步,确认三个数据

const imgData = [{id: 1,imgSrc: "https://p5.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/62825130816/57e3/05be/dbaa/35ba17ba02d8485482440213c7bfb115.jpg?imageView&quality=89"},{id: 2,imgSrc: "https://p5.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/62821641259/9168/ff56/496a/c8e1f7f69f747c564cc5b8a91c9dd9cf.jpg?imageView&quality=89"},{id: 3,imgSrc: "https://p5.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/62801174495/263c/d484/615b/2fdf2104fd80a45b430fc4f4607fc3ac.jpg?imageView&quality=89"}]

第三步,构建js函数

let currentId = 0let currentData = imgData.slice(currentId,currentId+1)[0] //splice会改变原的数组console.log("currentId", currentId)console.log("imgData", imgData)console.log("currentData" , currentData)//添加图片的函数function changeImg(){console.log(currentId)currentData = imgData.slice(currentId,currentId+1)[0]console.log("currentData", currentData)const img = document.createElement("img")img.src = currentData.imgSrcimg.dataset.id = currentData.idconst firstChild1_box_center = document.querySelector(".firstChild1_box_center")const login = document.querySelector(".login")//如何解决dom渲染次数错误?//可以先渲染图片之后再调整顺序firstChild1_box_center.innerHTML = ""firstChild1_box_center.append(img)firstChild1_box_center.append(login)}function slideshow() {setInterval(() => {currentId = (currentId + 1) % imgData.length; // 0→1→2→0 循环changeImg();}, 15000);}changeImg()slideshow()//点击按钮事件const firstChild1_box_left = document.querySelector(".firstChild1_box_left")const firstChild1_box_right = document.querySelector(".firstChild1_box_right")firstChild1_box_left.addEventListener("click", function(){console.log("left")if(currentId === 0){alert("已经是第一页了")return}currentId--;changeImg()})  firstChild1_box_right.addEventListener("click", function(){console.log(currentId)console.log("right")if(currentId === imgData.length -1){alert("已经是最后一页了")return}currentId++;changeImg()})

第四步:总结

在这一次构建html+css样式中,学会了如何通过flex布局构建页面,

在JavaScript编写的代码中学习到了函数和变量提升,并利用函数先声明后使用

写出来较为整洁的代码。

通过currentId = (currentId + 1) % imgData.length 学习到:

% 运算本身在任意数据量下都是常数时间,无需替换;如果5张图呢?

5 张图 = “数据量小”,直接沿用 (currentIndex + 1) % 5 即可,不用任何虚拟化。

通过JavaScript代码还学习到了如何渲染节点和dataset的使用,可以直接通过dom节点操作,

for循环也可以进行节点操作。

仓库见:轮播图

http://www.dtcms.com/a/415247.html

相关文章:

  • Low-Overhead Sensing RS Design for Integrated Sensing and Communication (ISAC)
  • 如何快速收录一个网站的信息网页设计与制作作业成品免费
  • MyEclipse在高分辨率显示屏上图标显示太小的解决方案
  • 网站 多语言处理wordpress搜索表单
  • Python 2025:物联网与边缘计算的智能融合新纪元
  • 小迪安全v2023学习笔记(九十讲)—— 小程序篇反编译外在主包分包配置泄露算法逆向未授权
  • 机器学习模型中异常样本、特征的三种常见分类与鉴别方法
  • 有口碑的常州网站建设建设网银怎么提高转账限额
  • 湖南响应式网站哪里有58同城怎么发布信息
  • 《前端开发中常用的快捷键大全》
  • 跳舞游戏做的广告视频网站平度建设局网站
  • 众筹网站建设公司金蝶官网首页
  • 智能汽车安全基石:通过CAS密钥管理系统实现全周期密钥管理与固件签名
  • 基于Python CNN推荐的电影资讯App软件的设计与实现
  • 如何识别网站的建站程序做网站的客户多吗
  • 找人做网站推广wordpress二次开发手册chm
  • 如何降低重复率?卷卷降AI
  • 建网站哪家好新闻wordpress 修改404
  • 基于微信小程序的智能在线预约挂号系统【2026最新】
  • 网站域名管理怎么登陆深圳服务网站建设
  • 前端开发中的事件冒泡
  • 《Rust 程序设计语言》第二十一章:期末项目:构建多线程 Web 服务器
  • 作品 上海高端网站设计wordpress logo 编辑
  • day9.27
  • 做动画人设有哪些网站可以借鉴谷歌chrome浏览器下载
  • c++ 之多态虚函数表
  • 全屏网站 图片优化网站主机免费
  • 谷歌广告联盟网站同一个网站绑定多个域名
  • Java 大视界 -- Java 大数据机器学习模型在金融产品创新与客户需求匹配中的实战应用(417)
  • 美团网站是用什么做的网站开发企业开发