slider-Animation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict'
  2. var body = document.querySelector("body");
  3. var slider = document.querySelector(".slider");
  4. var slides = document.querySelector(".slides");
  5. var bodyWidth = +getComputedStyle(body).width.replace("px","");
  6. var arrowLeft = document.getElementById("arrow-left");
  7. var arrowRight = document.getElementById("arrow-right");
  8. var slideNum = 1;
  9. var isCanSlide = true;
  10. for (let i = 0; i < slides.children.length; i++) {
  11. slides.children[i].style.left = (100 + bodyWidth * i) + "px";
  12. }
  13. function nextSlide(){
  14. if(slideNum < slides.children.length && isCanSlide){
  15. isCanSlide = false;
  16. for (var k = 0; k < slides.children.length; k++) {
  17. slides.children[k].style.left = +getComputedStyle(slides.children[k]).left.replace("px","") - bodyWidth + "px";
  18. }
  19. slideNum++;
  20. setTimeout(function(){
  21. isCanSlide = true;
  22. },1500)
  23. for (let i = 0; i < dotContainer.children.length; i++) {
  24. dotContainer.children[i].style.border = "1px solid #fff";
  25. dotContainer.children[i].style.backgroundColor = "transparent";
  26. }
  27. var activeDot = document.getElementById(slideNum);
  28. activeDot.style.border = "1px solid #00e0d0";
  29. activeDot.style.backgroundColor = "#00e0d0";
  30. }
  31. }
  32. arrowRight.onclick = nextSlide;
  33. function previousSlide(){
  34. if(slideNum > 1 && isCanSlide){
  35. isCanSlide = false;
  36. for (var k = 0; k < slides.children.length; k++) {
  37. slides.children[k].style.left = +getComputedStyle(slides.children[k]).left.replace("px","") + bodyWidth + "px";
  38. }
  39. slideNum--;
  40. setTimeout(function(){
  41. isCanSlide = true;
  42. },1500)
  43. for (let i = 0; i < dotContainer.children.length; i++) {
  44. dotContainer.children[i].style.border = "1px solid #fff";
  45. dotContainer.children[i].style.backgroundColor = "transparent";
  46. }
  47. var activeDot = document.getElementById(slideNum);
  48. activeDot.style.border = "1px solid #00e0d0";
  49. activeDot.style.backgroundColor = "#00e0d0";
  50. }
  51. }
  52. arrowLeft.onclick = previousSlide;
  53. setTimeout(function(){
  54. for (let i = 0; i < slides.children.length; i++) {
  55. slides.children[i].style.transition = "all 1.5s ease";
  56. }
  57. },1);
  58. ///////////////////////////////////////////
  59. var dotContainer = document.createElement("div");
  60. dotContainer.className = "dot-container";
  61. dotContainer.setAttribute("style","position: absolute; top: 97%; left: 48%;")
  62. for (let i = 0; i < slides.children.length; i++) {
  63. var dot = document.createElement("div");
  64. dot.setAttribute("id",i + 1);
  65. dot.onclick = function(){
  66. if(isCanSlide){
  67. for (let i = 0; i < dotContainer.children.length; i++) {
  68. dotContainer.children[i].style.border = "1px solid #fff";
  69. dotContainer.children[i].style.backgroundColor = "transparent";
  70. }
  71. this.style.border = "1px solid #00e0d0";
  72. this.style.backgroundColor = "#00e0d0";
  73. }
  74. if(this.getAttribute("id") > slideNum){
  75. if(slideNum < slides.children.length && isCanSlide){
  76. isCanSlide = false;
  77. for (var k = 0; k < slides.children.length; k++) {
  78. slides.children[k].style.left = +getComputedStyle(slides.children[k]).left.replace("px","") - bodyWidth * (this.getAttribute("id") - slideNum) + "px";
  79. }
  80. slideNum += this.getAttribute("id") - slideNum;
  81. setTimeout(function(){
  82. isCanSlide = true;
  83. },1500)
  84. }
  85. }
  86. else if(this.getAttribute("id") < slideNum){
  87. if(slideNum > 1 && isCanSlide){
  88. isCanSlide = false;
  89. for (var k = 0; k < slides.children.length; k++) {
  90. slides.children[k].style.left = +getComputedStyle(slides.children[k]).left.replace("px","") + bodyWidth * (slideNum - this.getAttribute("id")) + "px";
  91. }
  92. slideNum -= slideNum - this.getAttribute("id");
  93. setTimeout(function(){
  94. isCanSlide = true;
  95. },1500)
  96. }
  97. }
  98. }
  99. dot.setAttribute("style","border-radius: 50%; margin-right: 5px; border:1px solid #fff; width: 10px; height: 10px; display: inline-block; ");
  100. if(i === 0){
  101. dot.setAttribute("style","border-radius: 50%; margin-right: 5px; background-color: #00e0d0; border:1px solid #00e0d0; width: 10px; height: 10px; display: inline-block; ");
  102. }
  103. dotContainer.appendChild(dot);
  104. }
  105. slider.appendChild(dotContainer);