使用html和js实现的一个简单小训练轮播图。大概功能紧张是:
1、使用时间函数自动切换图片;
2、在图片及按钮及小圆点的父节点身上绑定事故署理,添加mouseenter及mouseleave事故范例,让鼠标移入,图片暂停,移出,图片规复轮播;
3、在按钮的父节点身上绑定事故署理,事故范例为click,使用event.target判断点击的目的范围,让左右两个按钮,点击可以切换上一张或下一张;
4、给图片添加样式,让下面的四个小圆点会随图片变颜色;
5、在小圆点的父节点身上绑定事故署理,事故范例为click,同样使用event.target判断点击的目的范围,让小圆点可以点击并切换到对应的图片上。
仍旧使用的是DOM2事故署理,详细的表明和代码步调我都解释在下面的代码中的,请君一阅。
【注:仅作自己检察和分享学习之用】
结果图如下:

代码如下:
- <!DOCTYPE html>
- <html lang="en">
- <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>轮播图</title>
- <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_3881267_wfv3iyzbijg.css">
- </head>
- <style>
- section {
- position: relative;
- height: 500px;
- width: 780px;
- border: 1px solid;
- margin: 100px auto;
- }
- #img {
- height: 100%;
- width: 100%;
- background-size: 100% 100%;
- }
- p {
- position: absolute;
- left: 50%;
- bottom: 0px;
- transform: translate(-50%, -50%);
- }
- i {
- height: 15px;
- width: 15px;
- background-color: gray;
- border-radius: 50%;
- display: inline-block;
- margin-right: 10px;
- }
- i:nth-child(1) {
- background-color: white;
- }
- i:nth-child(4) {
- margin-right: 0;
- }
- .left,
- .right {
- color: rgba(255, 255, 255, 0.7);
- font-size: 50px;
- font-weight: bolder;
- position: absolute;
- top: 50%;
- font-weight: 500;
- }
- .left {
- left: 0px;
- transform: translate(15%, -50%);
- }
- .right {
- right: 0px;
- transform: translate(-15%, -50%);
- }
- </style>
- <body>
- <section>
- <span class="left iconfont icon-anniu_jiantouxiangzuo"></span>
- <img src="./img/冬至竹林1.jpg" alt="" id="img">
- <span class="right iconfont icon-anniu-jiantouxiangyou"></span>
- <p></p>
- </section>
- <script src="./index.js"></script>
- </body>
- </html>
复制代码
来源:https://blog.csdn.net/m0_71734538/article/details/129100213
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |