javascript焦点图轮播效果实现代码示例

朱雀 2020-04-20 10:08 1097阅读 0赞

JS实现焦点图轮播

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>JS实现焦点图轮播效果</title>
  6. <style type="text/css">
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. text-decoration: none;
  11. }
  12. body {
  13. padding: 20px;
  14. }
  15. #container {
  16. width: 600px;
  17. height: 400px;
  18. border: 3px solid #333;
  19. overflow: hidden;
  20. position: relative;
  21. }
  22. #list {
  23. width: 4200px;
  24. height: 400px;
  25. position: absolute;
  26. z-index: 1;
  27. }
  28. #list img {
  29. float: left;
  30. }
  31. #buttons {
  32. position: absolute;
  33. height: 10px;
  34. width: 100px;
  35. z-index: 2;
  36. bottom: 20px;
  37. left: 250px;
  38. }
  39. #buttons span {
  40. cursor: pointer;
  41. float: left;
  42. border: 1px solid #fff;
  43. width: 10px;
  44. height: 10px;
  45. border-radius: 50%;
  46. background: #333;
  47. margin-right: 5px;
  48. }
  49. #buttons .on {
  50. background: orangered;
  51. }
  52. .arrow {
  53. cursor: pointer;
  54. display: none;
  55. line-height: 39px;
  56. text-align: center;
  57. font-size: 36px;
  58. font-weight: bold;
  59. width: 40px;
  60. height: 40px;
  61. position: absolute;
  62. z-index: 2;
  63. top: 180px;
  64. background-color: RGBA(0, 0, 0, .3);
  65. color: #fff;
  66. }
  67. .arrow:hover {
  68. background-color: RGBA(0, 0, 0, .7);
  69. }
  70. #container:hover .arrow {
  71. display: block;
  72. }
  73. #prev {
  74. left: 20px;
  75. }
  76. #next {
  77. right: 20px;
  78. }
  79. </style>
  80. <script type="text/javascript">
  81. window.onload = function() {
  82. var container = document.getElementById('container');
  83. var list = document.getElementById('list');
  84. var buttons = document.getElementById('buttons').getElementsByTagName('span');
  85. var prev = document.getElementById('prev');
  86. var next = document.getElementById('next');
  87. var index = 1;
  88. var len = 5;
  89. var animated = false;
  90. var interval = 3000;
  91. var timer;
  92. function animate(offset) {
  93. if(offset == 0) {
  94. return;
  95. }
  96. animated = true;
  97. var time = 300;
  98. var inteval = 10;
  99. var speed = offset / (time / inteval);
  100. var left = parseInt(list.style.left) + offset;
  101. var go = function() {
  102. if((speed > 0 && parseInt(list.style.left) < left) ||
  103. (speed < 0 && parseInt(list.style.left) > left)) {
  104. list.style.left = parseInt(list.style.left) + speed + 'px';
  105. setTimeout(go, inteval);
  106. } else {
  107. list.style.left = left + 'px';
  108. if(left > -200) {
  109. list.style.left = -600 * len + 'px';
  110. }
  111. if(left < (-600 * len)) {
  112. list.style.left = '-600px';
  113. }
  114. animated = false;
  115. }
  116. }
  117. go();
  118. }
  119. function showButton() {
  120. for(var i = 0; i < buttons.length; i++) {
  121. if(buttons[i].className == 'on') {
  122. buttons[i].className = '';
  123. break;
  124. }
  125. }
  126. buttons[index - 1].className = 'on';
  127. }
  128. function play() {
  129. timer = setTimeout(function() {
  130. next.onclick();
  131. play();
  132. }, interval);
  133. }
  134. function stop() {
  135. clearTimeout(timer);
  136. }
  137. next.onclick = function() {
  138. if(animated) {
  139. return;
  140. }
  141. if(index == 5) {
  142. index = 1;
  143. } else {
  144. index += 1;
  145. }
  146. animate(-600);
  147. showButton();
  148. }
  149. prev.onclick = function() {
  150. if(animated) {
  151. return;
  152. }
  153. if(index == 1) {
  154. index = 5;
  155. } else {
  156. index -= 1;
  157. }
  158. animate(600);
  159. showButton();
  160. }
  161. for(var i = 0; i < buttons.length; i++) {
  162. buttons[i].onclick = function() {
  163. if(animated) {
  164. return;
  165. }
  166. if(this.className == 'on') {
  167. return;
  168. }
  169. var myIndex = parseInt(this.getAttribute('index'));
  170. var offset = -600 * (myIndex - index);
  171. animate(offset);
  172. index = myIndex;
  173. showButton();
  174. }
  175. }
  176. container.onmouseover = stop;
  177. container.onmouseout = play;
  178. play();
  179. }
  180. </script>
  181. </head>
  182. <body>
  183. <div id="container">
  184. <div id="list" style="left: -600px;">
  185. <!-- 放最后的一张图片 -图片需要改为自己的图片路径 -->
  186. <img src="img/5.jpg" alt="1" />
  187. <img src="img/1.jpg" alt="1" />
  188. <img src="img/2.jpg" alt="2" />
  189. <img src="img/3.jpg" alt="3" />
  190. <img src="img/4.jpg" alt="4" />
  191. <img src="img/5.jpg" alt="5" />
  192. <!-- 放第一张的图片 -->
  193. <img src="img/1.jpg" alt="5" />
  194. </div>
  195. <div id="buttons">
  196. <span index="1" class="on"></span>
  197. <span index="2"></span>
  198. <span index="3"></span>
  199. <span index="4"></span>
  200. <span index="5"></span>
  201. </div>
  202. <a href="javascript:;" id="prev" class="arrow">&lt;</a>
  203. <a href="javascript:;" id="next" class="arrow">&gt;</a>
  204. </div>
  205. </body>
  206. </html>

发表评论

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

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

相关阅读

    相关 原生JavaScript实现焦点

      不管是高校的网站还是电商的页面,焦点图的切换和轮播应该是一项不可或缺的应用。今天把焦点图轮播制作的技术要点做下笔记,以供日后查看。   一、结构层(HTML)   焦