• js实现轮播图自动轮播


    咱们今天看下怎么实现轮播图的自动轮播,用原生js,

    一、思路

    首先,我们要对要实现的功能有一个明确的认知,知道了功能才能够实现相应的逻辑
    完整的轮播图需要具备的功能有:
    1、点击左右浮块实现单张图片切换
    2、在图片切换的同时底部圆点同时更新
    3、点击圆点切换到对应的图片
    定义一个图片切换函数,通过判断形参的类型,实现不同的切换需求,如果形参是布尔值,则实现单张图片切换,如果形参是下标,则实现多张图片切换,在图片切换的尾部调用圆点更新函数
    圆点更新函数:同样判断形参的类型,如果是布尔值,每次跳变一个,如果是数字,则跳到对应的圆点,这里使用排他法更新小圆点。

    html部分

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>轮播图之自动轮播title>
    8. <style>
    9. * {
    10. margin: 0;
    11. padding: 0;
    12. }
    13. ul {
    14. list-style: none;
    15. }
    16. .box {
    17. position: relative;
    18. margin: 30px auto;
    19. width: 600px;
    20. height: 600px;
    21. border: 1px solid red;
    22. }
    23. #parent {
    24. width: 600px;
    25. height: 400px;
    26. overflow: hidden;
    27. }
    28. #child {
    29. width: 2400px;
    30. height: 400px;
    31. }
    32. #child>img {
    33. width: 600px;
    34. height: 400px;
    35. display: block;
    36. float: left;
    37. }
    38. #leftBtn,
    39. #rightBtn {
    40. position: absolute;
    41. top: 170px;
    42. width: 40px;
    43. height: 60px;
    44. background-color: black;
    45. opacity: 0.3;
    46. color: white;
    47. text-align: center;
    48. line-height: 60px;
    49. font-size: 30px;
    50. }
    51. #leftBtn {
    52. left: 0;
    53. }
    54. #rightBtn {
    55. right: 0;
    56. }
    57. #dotted {
    58. position: absolute;
    59. left: 60px;
    60. top: 350px;
    61. width: 120px;
    62. height: 30px px;
    63. border: 1px solid blue;
    64. }
    65. #dotted>li {
    66. float: left;
    67. width: 30px;
    68. height: 30px;
    69. text-align: center;
    70. line-height: 30px;
    71. font-size: 20px;
    72. box-sizing: border-box;
    73. }
    74. #dotted>li:nth-child(2),
    75. #dotted>li:nth-child(3),
    76. #dotted>li:nth-child(4) {
    77. border-left: 1px solid blue;
    78. }
    79. style>
    80. head>
    81. <body>
    82. <div class="box">
    83. <div id="parent">
    84. <div id="child">
    85. <img src="images/1.jpg" alt="">
    86. <img src="images/2.jpg" alt="">
    87. <img src="images/3.jpg" alt="">
    88. <img src="images/4.jpg" alt="">
    89. div>
    90. div>
    91. <div id="leftBtn">div>
    92. <div id="rightBtn">div>
    93. <ul id="dotted">
    94. <li style="background-color: red;">1li>
    95. <li>2li>
    96. <li>3li>
    97. <li>4li>
    98. ul>
    99. div>
    100. body>
    101. html>

    js部分

    相关知识点

    1、获取DOM元素:
    (1)document.getElementsById():通过id获取对象,id是唯一的,可以不获取。
    (2)document.getElementsByClassName():通过class属性获取对象。
    (3)document.getElementsByTagName():通过标签名获取对象。
    (4)document.querySelectorAll():可通过所有获取。
    2、鼠标事件

    (1)onmouseover():鼠标移上时事件;
    (2)onmouseout():鼠标移开时事件;

    (3)onclick():单击事件。

  • 相关阅读:
    【数据结构】链表C++编写的,它定义了一个链表,并实现了一些基本的链表操作,如创建新节点、插入节点、清空链表、输出链表以及查找节点
    设计模式之单例模式
    多媒体技术1-颜色在计算机中的表示
    西电软件体系结构CH4:理解质量属性
    Google Earth Engine ——
    重大技术问题,iPhone 15 Pro Max面临“烧屏门”风波 | 百能云芯
    torch车牌字符识别-数据集搭建(四)
    UE4 使用材质后期 制作玻璃有雨效果
    小红书运营:商家如何选择小红书博主?选择小红书博主需要注意什么?
    Windows入口点函数
  • 原文地址:https://blog.csdn.net/weixin_47619284/article/details/126375594