原生js实现轮播图

深藏阁楼爱情的钟 2022-06-14 02:12 379阅读 0赞

原生js简单轮播图

实现原理

一系列大小相等的图片平铺,利用css布局,只显示其中一张图片,其它图片隐藏。通过偏移量来实现滑动,或者手动点击来实现滑动

![Image 1][]

Html布局

  1. <body>
  2. <div id="container">
  3. <div id="list" style="left: 0px">
  4. <!-- <img src="./img/pic_05.png" alt="1" />-->
  5. <img src="./img/pic_01.png" alt="1" />
  6. <img src="./img/pic_02.png" alt="2" />
  7. <img src="./img/pic_03.png" alt="3" />
  8. <img src="./img/pic_04.png" alt="4" />
  9. <img src="./img/pic_05.png" alt="5" />
  10. <!--<img src="./img/pic_01.png" alt="5" />-->
  11. </div>
  12. <div id="buttons">
  13. <span index="1" class="on"></span>
  14. <span index="2"></span>
  15. <span index="3"></span>
  16. <span index="4"></span>
  17. <span index="5"></span>
  18. </div>
  19. <a href="javascript:;" id="prev" class="arrow"><</a>
  20. <a href="javascript:;" id="next" class="arrow">></a>

父容器container存放所有内容,list子容器存放图片,buttons子容器存放按钮小圆点。下面的表示轮播的两个箭头。

进行优化,实现无缝轮播

当从最后一张图滚动到第一张图时,中间有很大的空白。

CSS部分

注意list部分,overfloat : hidden ; 显示一张图片,其它图片隐藏。

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. text-decoration: none;
  6. }
  7. body {
  8. padding: 20px;
  9. }
  10. #container {
  11. position: relative;
  12. width: 600px;
  13. height: 400px;
  14. border: 3px solid #333;
  15. margin: 50px auto;
  16. overflow: hidden;
  17. }
  18. #list {
  19. position: absolute;
  20. z-index: 1;
  21. width: 4200px;
  22. height: 400px;
  23. }
  24. #list img {
  25. float: left;
  26. width: 600px;
  27. height: 400px;
  28. }
  29. #buttons {
  30. position: absolute;
  31. left: 250px;
  32. bottom: 20px;
  33. z-index: 2;
  34. height: 10px;
  35. width: 100px;
  36. }
  37. #buttons span {
  38. float: left;
  39. margin-right: 5px;
  40. width: 10px;
  41. height: 10px;
  42. border: 1px solid #fff;
  43. border-radius: 50%;
  44. background: #333;
  45. cursor: pointer;
  46. }
  47. #buttons .on {
  48. background: orangered;
  49. }
  50. .arrow {
  51. position: absolute;
  52. top: 180px;
  53. z-index: 2;
  54. display: none;
  55. width: 40px;
  56. height: 40px;
  57. font-size: 36px;
  58. font-weight: bold;
  59. line-height: 39px;
  60. text-align: center;
  61. color: #fff;
  62. background-color: RGBA(0, 0, 0, .3);
  63. cursor: pointer;
  64. }
  65. .arrow:hover {
  66. background-color: RGBA(0, 0, 0, .7);
  67. }
  68. #container:hover .arrow {
  69. display: block;
  70. }
  71. #prev {
  72. left: 20px;
  73. }
  74. #next {
  75. right: 20px;
  76. }
  77. </style>

如下图所示:

![Image 1][]

JS部分

首先实现手动点击左右箭头,实现图片的偏移;

  1. function animate(offset) {
  2. console.log(list.style.left);
  3. //获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值, //且style.left获取的是字符串,需要用parseInt()取整转化为数字。 var newLeft = parseInt(list.style.left) + offset;
  4. list.style.left = newLeft + 'px';
  5. if(newLeft<-2400){
  6. list.style.left = 0 + 'px';
  7. }
  8. if(newLeft>0){
  9. list.style.left = -2400 + 'px';
  10. }
  11. }

但是现在问题来了,当点击图片到最后一张时候,无法跳到第一张。原因是我们用left偏移量来控制图片,在点击箭头时偏移量在发生变化,为了在最后一张图片点击时能跳到第一张图片,我们需要对偏移量做一个判断;

在animate函数里加上一个关于偏移量的判断;

  1. prev.onclick = function() {
  2. animate(600);
  3. }
  4. next.onclick = function() {
  5. animate(-600);
  6. }

加上该判断后运行下,能跳转到第一页,没问题。

自动轮播,顾名思义就是自己能切换图片,则此时需要给他添加一个定时器

这里我们是用setInterval(),因为我们的图片需要循环滚动

  1. var timer;
  2. function play(){
  3. timer = setInterval(function () {
  4. prev.onclick()
  5. }, 1500)
  6. }
  7. play();

当把鼠标移到图片上面时,停止自动切换,鼠标移开时继续自动切换

  1. function stop(){
  2. clearInterval(timer);
  3. }
  4. //为什么不用stop(); container.onmouseover = stop;
  5. container.onmouseout = play;

(有必要强调一下对于定时器,有必要说明一下setInterval()跟setTimeout的区别了。简单来说,setInterval()执行多次,setTimeout()只执行一次。)

小圆点部分

  1. var buttons = document.getElementById('buttons').getElementsByTagName('span');
  2. var index = 1;
  3. function buttonsShow() {
  4. //这里需要清除之前的样式 for (var i = 0; i < buttons.length; i++) {
  5. if (buttons[i].className == 'on') {
  6. buttons[i].className = '';
  7. }
  8. }
  9. //数组从0开始,故index需要-1 buttons[index - 1].className = 'on';
  10. }
  11. prev.onclick = function() {
  12. index -= 1;
  13. if (index < 1) {
  14. index = 5;
  15. }
  16. buttonsShow();
  17. animate(600);
  18. }
  19. next.onclick = function() {
  20. //由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断 index += 1;
  21. if (index > 5) {
  22. index = 1;
  23. }
  24. buttonsShow();
  25. animate(-600);
  26. }
  27. }

好了~把上述代码拼在一起,就能组成一个简单的轮播咯。

[Image 1]:

发表评论

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

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

相关阅读

    相关 原生js实现

            很多很多网站经常会用到一个特效,那就是轮播图,对于日新月异的前端技术来说其实就是一个框架一个接口的事,但轮播的原理是什么?用最原始的javascript来写又是

    相关 原生js实现

    轮播图基本上是前端所必须面临的一个功能。而且在网上可以找到各种各样的插件或者写法。 但是我个人觉得还是写一下比较好。这里用到的是原生JS、CSS3相结合的写法。 对IE 8

    相关 原生js实现

    原生js实现轮播图  很多网站上都有轮播图,但却很难找到一个系统讲解的,因此这里做一个简单的介绍,希望大家都能有所收获,如果有哪些不正确的地方,希望大家可以指出。  [