js原生轮播图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <!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>Document</title> <script src= "https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" ></script> <style> .swiper { width: 800px; height: 500px; position: relative; overflow: hidden; background-color: bisque; } .swiper-box { background-color: pink; position: absolute; left: 0; top: 0; z-index: 1; height: 500px; } .swiper-box .swiper-item { width: 800px; height: 500px; background-color: aqua; float : left; } .swiper-box .swiper-item img { width: 800px; height: 500px; } </style> </head> <body> <div> <div class = "swiper" > <div class = "swiper-box" style= "left: -800px;" > <div class = "swiper-item" > 6 <!-- <img src= "./index_active.png" alt= "" > --> </div> <div class = "swiper-item" > 1 <!-- <img src= "./index_gray.png" alt= "" > --> </div> <div class = "swiper-item" > 2 <!-- <img src= "./my_active.png" alt= "" > --> </div> <div class = "swiper-item" > 3 <!-- <img src= "./my_gray.png" alt= "" > --> </div> <div class = "swiper-item" > 4 <!-- <img src= "./Tablet.png" alt= "" > --> </div> <div class = "swiper-item" > 5 <!-- <img src= "./shouxie.png" alt= "" > --> </div> <div class = "swiper-item" > 6 <!-- <img src= "./index_active.png" alt= "" > --> </div> <div class = "swiper-item" > 1 <!-- <img src= "./index_gray.png" alt= "" > --> </div> </div> </div> <div> <button onclick= "prev()" >左</button> <button onclick= "next()" >右</button> </div> </div> </body> </html> <script> var wrap = $( '.swiper-box' )[0] let ok = $( '.swiper-item' ).length let domArr = $( '.swiper-item' ) let total = 0 for ( let i = 0; i < ok; i++) { let aa = $($( '.swiper-item' )[i]).width() total += aa console.log( 'aa' , aa); } console.log( 'ok' , ok); $( '.swiper-box' ).css({ width: total + 'px' }) function prev() { console.log( '左' , wrap.style.left); var newLeft if (wrap.style.left === '-800px' ) { newLeft = -4800 } else { newLeft = parseInt(wrap.style.left) + 800 } $(wrap).animate({left: newLeft + 'px' }) } function next() { console.log( '右' , wrap.style.left); var newLeft if (wrap.style.left === '-4800px' ) { newLeft = -800 } else { newLeft = parseInt(wrap.style.left) - 800 } $(wrap).animate({left: newLeft + 'px' },1000) } var timer = null function autoPlay() { timer = setInterval(() => { next() }, 3000); } autoPlay() $( '.swiper' ).mouseenter(function () { clearInterval(timer) }) $( '.swiper' ).mouseleave(function () { autoPlay() }) </script> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)