Advertisement
Guest User

SLIDER

a guest
Jul 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', function() {
  2.     /**
  3.      *  Làm sự kiện nút cách 1
  4.      *  var nut = document.querySelectorAll('.chuyensilde ul li');
  5.      *  for (let index = 0; index < nut.length; index++) {
  6.      *  nut[index].onclick = function () {
  7.      *  if (this.classList[0] == 'kichhoat') {
  8.      *  this.classList.remove('kichhoat');
  9.      *  } else {
  10.      *  for (let i = 0; i < nut.length; i++) {
  11.      *  nut[i].classList.remove('kichhoat');
  12.      *  }
  13.      *  this.classList.add('kichhoat');
  14.      *  }
  15.      *  }
  16.      *  }
  17.      * */
  18.     // Làm sự kiện nút cách 2
  19.     var nut = document.querySelectorAll('.chuyensilde ul li');
  20.     var slides = document.querySelectorAll('.cacslide ul li');
  21.     var tg = setInterval(function() {
  22.         autosilde();
  23.     }, 4000);
  24.     for (let index = 0; index < nut.length; index++) {
  25.         nut[index].addEventListener('click', function() {
  26.             for (let index = 0; index < nut.length; index++) {
  27.                 nut[index].classList.remove('kichhoat');
  28.             }
  29.             this.classList.add('kichhoat');
  30.             //Hết phần xử lý nút
  31.             // previousElementSibling: trả về phần tử trước nó mà được chỉ định
  32.             //nextElementSibling: trả về phần tử sau nó mà được chỉ định
  33.             var nutkichhoat = nut[index];
  34.             var vtrinut = 0;
  35.             for (vtrinut = 0; nutkichhoat = nutkichhoat.previousElementSibling; vtrinut++) {
  36.                 /**
  37.                  * Để nutkichhoat là nut đang được kích. khởi tạo i=0 cho nutkichhoat=nutkichhoat.previousElementSibling
  38.                  * (tức bằng nút trước nó) rồi i++. Khi ta nhấn nút số 3 nó sẽ trả về nút 2. Nhấn nút 2 trả về nút 1.
  39.                  * Nhấn nút 1 trả về 0 nhưng vì k có nút 0 nên nó trả về null, đó là điều kiện dừng
  40.                  */
  41.             }
  42.             for (let i = 0; i < slides.length; i++) {
  43.                 slides[i].classList.remove('active');
  44.                 /*Cách 2 nếu k dùng từ dòng 30 đến 38 thì ta viết luôn dòng dưới đây
  45.                 là slides[index].classList.add('active'); tức cái nút đang được click ở trên trong vòng for index
  46.                 sẽ được thêm class active ở dưới đây luôn. Nhưng kb tại sao thầy lại k dùng
  47.                 */
  48.                 slides[vtrinut].classList.add('active');
  49.             }
  50.         })
  51.     }
  52.     //setInterval(function(),milisecond): hàm dùng để tự gọi hàm function sau 1 khoảng tg
  53.     /***********************VIẾT HÀM TỰ CHUYỂN SLIDE****************************/
  54.     function autosilde() {
  55.         //B1: Tính xem đang ở slide nào
  56.         var vtrislide = 0;
  57.         var slidehientai = document.querySelector('.cacslide ul li.active');
  58.         var other_textND=document.querySelectorAll('.textnoidung');
  59.         [].forEach.call(other_textND, function(el) {
  60.             el.className = el.className.replace(/\bbiemattext\b/, "");
  61.         });
  62.         var textND=document.querySelector('.cacslide ul li.active .motslide .textnoidung');
  63.         textND.classList.add('biemattext');
  64.         setTimeout(function(){
  65.             for (vtrislide = 0; slidehientai = slidehientai.previousElementSibling; vtrislide++) {}
  66.             //Nếu vị trí slide < độ dài của slide thì hoạt động bth
  67.             if (vtrislide < slides.length - 1) {
  68.                 //B2: Cho các slide còn lại ẩn hết đi
  69.                 for (let i = 0; i < slides.length; i++) {
  70.                      slides[i].classList.remove('active');
  71.                     nut[i].classList.remove('kichhoat');
  72.                 }
  73.                 //B3: Cho phần tử tiếp theo của slide hiển thị ra
  74.                 slides[vtrislide].nextElementSibling.classList.add('active');
  75.                 nut[vtrislide].nextElementSibling.classList.add('kichhoat');
  76.             } else {
  77.                 //Nếu lớn hơn thì gán lại cho vtrislide là 0
  78.                 vtrislide = 0
  79.                 //Xóa tất cả các slide
  80.                 for (let i = 0; i < slides.length; i++) {
  81.                     slides[i].classList.remove('active');
  82.                     nut[i].classList.remove('kichhoat');
  83.                 }
  84.                 //Chỉ để lại cho slide có vtrislide=0 active
  85.                 slides[vtrislide].classList.add('active');
  86.                 nut[vtrislide].classList.add('kichhoat');
  87.             }
  88.         }, 1000);
  89.     }
  90. }, false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement