当前位置:   article > 正文

轮播图(含全部代码:html、css、javascript)_轮播图html代码

轮播图html代码

代码

h1.html:

  1. <!DOCTYPE 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>Document</title>
  8. <link rel="stylesheet" href="index.css">
  9. <script src="animate.js"></script>
  10. </head>
  11. <body>
  12. <div class="box">
  13. <a href="javascript:;" class = 'left jiantou'>&lt;</a>
  14. <a href="javascript:;" class = 'right jiantou'>&gt;</a>
  15. <ul class = 'pic'>
  16. <li>
  17. <a href="#"><img src="./images/1.png" alt=""></a>
  18. </li>
  19. <li>
  20. <a href="#"><img src="./images/2.png" alt=""></a>
  21. </li>
  22. <li>
  23. <a href="#"><img src="./images/3.png" alt=""></a>
  24. </li>
  25. <li>
  26. <a href="#"><img src="./images/4.png" alt=""></a>
  27. </li>
  28. <li>
  29. <a href="#"><img src="./images/5.png" alt=""></a>
  30. </li>
  31. </ul>
  32. <ul class="lis">
  33. <!-- <li class="selected"></li>
  34. <li></li>
  35. <li></li>
  36. <li></li>
  37. <li></li> -->
  38. </ul>
  39. </div>
  40. <script src="index.js"></script>
  41. </body>
  42. </html>

index.css:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. li{
  6. list-style: none;
  7. }
  8. .box{
  9. position: relative;
  10. overflow: hidden;
  11. margin: 100px auto;
  12. width: 520px;
  13. height: 280px;
  14. background-color: red;
  15. }
  16. .jiantou{
  17. font-size: 24px;
  18. text-decoration: none;
  19. display: block;
  20. text-align: center;
  21. width: 20px;
  22. height: 30px;
  23. line-height: 30px;
  24. background: rgba(158, 154, 154, 0.7);
  25. color: white;
  26. z-index: 999;
  27. }
  28. .left{
  29. position: absolute;
  30. top: 125px;
  31. left: 0px;
  32. border-top-right-radius: 15px;
  33. border-bottom-right-radius: 15px;
  34. }
  35. .right{
  36. position: absolute;
  37. top:125px;
  38. left: 500px;
  39. border-top-left-radius: 15px;
  40. border-bottom-left-radius: 15px;
  41. }
  42. img{
  43. width: 520px;
  44. height: 280px;
  45. }
  46. .box .pic{
  47. width: 600%;
  48. }
  49. .pic{
  50. position: absolute;
  51. }
  52. .pic li {
  53. float: left;
  54. }
  55. .lis{
  56. position: absolute;
  57. bottom: 15px;
  58. left: 50%;
  59. margin-left: -35px;
  60. width: 70px;
  61. height:13px;
  62. border-radius: 7px;
  63. background: rgba(158, 154, 154, 0.7);
  64. }
  65. .lis li {
  66. float: left;
  67. width: 8px;
  68. height: 8px;
  69. margin: 3px;
  70. border-radius: 50%;
  71. background-color: #fff;
  72. }
  73. .lis .selected{
  74. background-color: cyan;
  75. }

index.js:

  1. var pic = document.querySelector('.pic');
  2. var lis = document.querySelector('.lis');
  3. var leftarrowhead = document.querySelector('.left');
  4. var rightarrowhead = document.querySelector('.right');
  5. var box = document.querySelector('.box');
  6. var index = 0;
  7. //形成小圆圈li
  8. //注意此模块要放上面从而事先形成li
  9. for (var i = 0; i < pic.children.length; i++) {
  10. var li = document.createElement('li');
  11. lis.appendChild(li);
  12. //顺便设置li的索引
  13. li.setAttribute('index', i);
  14. }
  15. lis.children[0].className = 'selected';
  16. var pictureWidth = box.offsetWidth; //获取图片宽度
  17. //排他思想,设置li的点击效果
  18. for (var j = 0; j < lis.children.length; j++) {
  19. lis.children[j].onclick = function () {
  20. for (var i = 0; i < lis.children.length; i++) {
  21. lis.children[i].className = '';
  22. }
  23. index = this.getAttribute('index');
  24. this.className = 'selected';
  25. animate(pic, -index * pictureWidth);
  26. }
  27. }
  28. var flag = true;
  29. //克隆第一张图片放最后
  30. var first = pic.children[0].cloneNode(true);
  31. pic.appendChild(first);
  32. //设置箭头的移动
  33. //左箭头
  34. leftarrowhead.addEventListener('click', function () {
  35. if (flag) {
  36. flag = false;
  37. //当移动到最左边时,立即跳到最后一张图片
  38. if (index == 0) {
  39. pic.style.left = -lis.children.length * pictureWidth + 'px';
  40. index = 5;
  41. }
  42. index--;
  43. //节流阀,当动画运行完毕,再释放flag,从而可放开下一次点击箭头效果
  44. animate(pic, -index * pictureWidth, function () {
  45. flag = true;
  46. });
  47. for (var i = 0; i < lis.children.length; i++) {
  48. lis.children[i].className = '';
  49. }
  50. lis.children[index].className = 'selected';
  51. }
  52. })
  53. //右箭头
  54. rightarrowhead.addEventListener('click', function () {
  55. if (flag) {
  56. flag = false;
  57. if (index == lis.children.length) {
  58. pic.style.left = 0;
  59. index = 0;
  60. }
  61. index++;
  62. animate(pic, -index * pictureWidth, function () {
  63. flag = true;
  64. });
  65. for (var i = 0; i < lis.children.length; i++) {
  66. lis.children[i].className = '';
  67. }
  68. if (index == lis.children.length) {
  69. lis.children[0].className = 'selected';
  70. }
  71. else {
  72. lis.children[index].className = 'selected';
  73. }
  74. }
  75. })
  76. //设置自动轮播(相当于点右箭头),时间间隔2
  77. var timer = setInterval(function () {
  78. rightarrowhead.click();
  79. }, 2000);
  80. var left = document.querySelector('.left');
  81. var right = document.querySelector('.right');
  82. //设置箭头是否显示
  83. box.addEventListener('mouseenter', function () {
  84. left.style.display = 'block';
  85. right.style.display = 'block';
  86. clearInterval(timer);
  87. })
  88. box.addEventListener('mouseleave', function () {
  89. left.style.display = 'none';
  90. right.style.display = 'none';
  91. timer = setInterval(function () {
  92. rightarrowhead.click();
  93. }, 2000);
  94. })

animate.js:

  1. function animate(obj, target, callback) {
  2. // console.log(callback); callback = function() {} 调用的时候 callback()
  3. // 先清除以前的定时器,只保留当前的一个定时器执行
  4. clearInterval(obj.timer);
  5. obj.timer = setInterval(function() {
  6. // 步长值写到定时器的里面
  7. // 把我们步长值改为整数 不要出现小数的问题
  8. // var step = Math.ceil((target - obj.offsetLeft) / 10);
  9. var step = (target - obj.offsetLeft) / 10;
  10. step = step > 0 ? Math.ceil(step) : Math.floor(step);
  11. if (obj.offsetLeft == target) {
  12. // 停止动画 本质是停止定时器
  13. clearInterval(obj.timer);
  14. // 回调函数写到定时器结束里面
  15. // if (callback) {
  16. // // 调用函数
  17. // callback();
  18. // }
  19. callback && callback();
  20. }
  21. // 把每次加1 这个步长值改为一个慢慢变小的值 步长公式:(目标值 - 现在的位置) / 10
  22. obj.style.left = obj.offsetLeft + step + 'px';
  23. }, 15);
  24. }

 效果

文件存放格式:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/655383
推荐阅读
相关标签
  

闽ICP备14008679号