轮播图:京东为例

末蓝、 2022-02-26 02:50 320阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>小小争520</title>
  6. <link rel="stylesheet" href="index.css">
  7. </head>
  8. <body>
  9. <!--相框-->
  10. <div class="Photo-frame">
  11. <!--照片条-->
  12. <ul class="picBox clearFix" style="left: 0">
  13. <li><a href="#"><img src="zhutu1.jpg" alt=""></a></li>
  14. <li><a href="#"><img src="zhutu3.jpg" alt=""></a></li>
  15. <li><a href="#"><img src="zhutu4.jpg" alt=""></a></li>
  16. <li><a href="#"><img src="zhutu5.jpg" alt=""></a></li>
  17. <li><a href="#"><img src="zhutu6.jpg" alt=""></a></li>
  18. </ul>
  19. <!--指示 小圆圈-->
  20. <div class="circles">
  21. <span in="0" style=" box-shadow: 0 0 10px 5px rgba(255,255,255,.7) inset"></span>
  22. <span in="1"></span>
  23. <span in="2"></span>
  24. <span in="3"></span>
  25. <span in="4"></span>
  26. </div>
  27. <!--箭头 next 右箭头-->
  28. <div class="arrow" id="next"> > </div>
  29. <!--左箭头-->
  30. <div class="arrow" id="prev"> < </div>
  31. </div>
  32. <!--不能互换顺序-->
  33. <script src="获取元素.js"></script>
  34. <script src="index.js"></script>
  35. </body>
  36. </html>
  37. /*获取元素 ---》 一个 */
  38. function my$(selector ) {
  39. /*判断? # */
  40. return document.querySelector(selector)
  41. }
  42. function my$All(selector ) {
  43. /*判断? # */
  44. return document.querySelectorAll(selector)
  45. }
  46. window.onload = function () {
  47. // ☆☆☆☆☆ 如果你是先加后判断 判断条件应该是下标+1
  48. //1:手动播放
  49. /*
  50. * (1)当用户点击右箭头 下一张 照片盒子向左移动 left-=590px
  51. * (2)当用户点击左箭头 上一张 照片盒子向右移动
  52. * left+=590px
  53. *
  54. * */
  55. //相框
  56. var Photo_frame = my$('.Photo-frame')
  57. //照片盒子
  58. var picBox = my$('.picBox')
  59. //箭头
  60. var next = my$('#next') //右
  61. var prev = my$('#prev') //左
  62. var circles = my$('.circles')
  63. var circlesSpans = my$All('.circles span') //bug my$() ===>my$All()
  64. console.log(circlesSpans)
  65. var index = 0 ;// 标记当前有光圈的小圆点的下标
  66. /***********照片盒子运动函数 picBoxAnimation()
  67. * 参数:偏移量 +590 向右 上一张
  68. * -590 向左 下一张
  69. * 返回值 无
  70. * ************/
  71. function picBoxAnimation(offset){
  72. var leftValue = parseInt(picBox.style.left); //数值类型
  73. var newLeft = leftValue + offset //照片盒子要运动的距离
  74. if(newLeft < -2360 ){ //向左走的趋势 当前已经展示的是第5张
  75. newLeft = 0;
  76. }
  77. if(newLeft > 0 ){
  78. newLeft = -2360
  79. }
  80. picBox.style.left = newLeft + 'px';
  81. console.log(picBox.style.left) //只能获取行内样式
  82. }
  83. //右箭头
  84. next.onclick = function () {
  85. picBoxAnimation(-590)
  86. //右箭头朝右移动 加+
  87. index++; //4
  88. if(index === 5){
  89. index = 0;
  90. }
  91. buttonShow();
  92. }
  93. prev.onclick = function () {
  94. picBoxAnimation(+590)
  95. index--;
  96. if(index === -1){
  97. index = 4;
  98. }
  99. buttonShow();
  100. }
  101. //2:自动播放 定时器
  102. function play() {
  103. timer = setInterval(function () {
  104. next.onclick();
  105. },1000)
  106. }
  107. play();
  108. //s鼠标移入不动 清定时器
  109. Photo_frame.onmouseover = function () {
  110. clearInterval(timer) //清除定时器 停止自动轮播
  111. }
  112. //鼠标移开 重新自动轮播 play()
  113. Photo_frame. function () {
  114. play()
  115. }
  116. /*********小圆点
  117. * 1:随着图片移动
  118. * 2:鼠标切换相应(自定义属性 )图片
  119. * **********/
  120. //小圆点移动函数 buttonShow() 参数 无 返回值 无
  121. function buttonShow() {
  122. for(var i =0;i<circlesSpans.length ;i++){
  123. circlesSpans[i].style.boxShadow = '';//清除样式
  124. }
  125. circlesSpans[index].style.boxShadow = "0 0 10px 5px rgba(255,255,255,.7) inset"
  126. }
  127. //鼠标移入哪个小圆圈 哪张图就显示
  128. //图片盒子运动偏移量 = (旧下标 index -新下标 )*590
  129. for(var i =0;i<circlesSpans.length ;i++){
  130. //给小圆圈绑定鼠标移入事件
  131. circlesSpans[i]. function () {
  132. //获取当前鼠标移入的那个小圆圈的下标 ===》 新下标
  133. var newIndex = this.getAttribute('in')
  134. console.log(newIndex)
  135. //根据产生的新下标和旧下标的差的到了照片移动的偏移量
  136. var currentOffset = (index-newIndex)*590
  137. //调用运动函数 ===》图片已经切换
  138. picBoxAnimation(currentOffset)
  139. //新旧更替 旧下标保存新下标
  140. index = newIndex ;
  141. //调用小圆圈展示的函数 为什么在这调用? 能不能拿到前面? 为什么?
  142. buttonShow()
  143. }
  144. }
  145. }
  146. @charset "UTF-8";
  147. * {
  148. padding: 0;
  149. margin: 0;
  150. }
  151. img {
  152. display: block; /*转换块级元素*/
  153. }
  154. ul li {
  155. list-style: none;
  156. }
  157. /*清浮动的衣服*/
  158. .clearFix::after,.clearFix::before {
  159. content: '';
  160. display: block;
  161. line-height: 0;
  162. clear: both;
  163. }
  164. .Photo-frame {
  165. position: relative;
  166. width: 590px;
  167. height: 470px;
  168. border: 1px solid #ccc;
  169. margin: 100px auto;
  170. /*溢出隐藏*/
  171. overflow: hidden;
  172. }
  173. /*照片条*/
  174. .picBox {
  175. /*定位*/
  176. position: absolute;
  177. width: 2950px;
  178. height: 470px;
  179. top:0;
  180. }
  181. .picBox > li {
  182. float: left;
  183. }
  184. .arrow {
  185. position: absolute;
  186. top:50%;
  187. margin-top: -20px;
  188. width: 24px;
  189. height: 40px;
  190. background-color: rgba(45,45,45,.3);
  191. color: #fff;
  192. line-height: 40px;
  193. text-align: center;
  194. font-size: 25px;
  195. cursor: pointer;/*鼠标小手样式*/
  196. }
  197. #next {
  198. right: 0;
  199. }
  200. #prev {
  201. left: 0;
  202. }
  203. .arrow:hover {
  204. background-color: rgba(45,45,45,.7);
  205. }
  206. /*小圆点*/
  207. .circles {
  208. position: absolute;
  209. bottom:20px;
  210. left: 50%;
  211. margin-left:-76px ;
  212. height: auto;
  213. }
  214. .circles span {
  215. display: inline-block;
  216. box-sizing: border-box;
  217. width: 14px;
  218. height: 14px;
  219. border: 3px solid rgba(255,255,255,.3);
  220. border-radius: 50%;
  221. margin-left: 10px;
  222. cursor: pointer;
  223. background-color: darkblue;
  224. }
  225. /*
  226. .circles span:hover {
  227. box-shadow: 0 0 10px 5px rgba(255,255,255,.7) inset;
  228. }*/

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,320人围观)

还没有评论,来说两句吧...

相关阅读