Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript slider

//Splide is a lightweight, flexible and accessible slider/carousel written in TypeScript.
//No dependencies, no Lighthouse errors.

//Get it on:
https://splidejs.com/
Comment

Slider JS

const slider = function () {
  let currentSlide = 0;
  const maxSlide = slides.length;

  /////////////functions
  //create dots
  const createDots = function () {
    slides.forEach(function (_s, i) {
      dotContainer.insertAdjacentHTML(
        "beforeend",
        `<button class="dots__dot" data-slide="${i}"></button>`
      );
    });
  };

  //activate dot
  const activateDot = function (slide) {
    const allDots = document.querySelectorAll(".dots__dot");
    allDots.forEach((dot) => dot.classList.remove("dots__dot--active"));

    //select dot based on data set attribute
    document
      .querySelector(`.dots__dot[data-slide="${slide}"]`)
      .classList.add("dots__dot--active");
  };

  //default slide
  const goToSlide = function (slide) {
    slides.forEach(function (s, index) {
      s.style.transform = `translateX(${100 * (index - slide)}%)`;
    });
  };

  //going next slide
  const nextSlide = function () {
    if (currentSlide === maxSlide - 1) {
      currentSlide = 0;
    } else {
      currentSlide++;
    }

    goToSlide(currentSlide);
    activateDot(currentSlide);
  };

  //going prev slide
  const prevSlide = function () {
    if (currentSlide === 0) {
      currentSlide = maxSlide - 1;
    } else {
      currentSlide--;
    }

    goToSlide(currentSlide);
    activateDot(currentSlide);
  };

  const init = function () {
    goToSlide(0);
    createDots();
    activateDot(0);
  };
  init();

  //Event handlers
  btnRight.addEventListener("click", nextSlide);
  btnLeft.addEventListener("click", prevSlide);

  //short circuiting
  document.addEventListener("keydown", function (e) {
    e.key === "ArrowRight" && nextSlide();
    e.key === "ArrowLeft" && prevSlide();
  });

  dotContainer.addEventListener("click", function (e) {
    if (e.target.classList.contains("dots__dot")) {
      const { slide } = e.target.dataset;
      goToSlide(slide);
      activateDot(slide);
    }
  });
};
slider();
Comment

slide js

var slide = new Array("foret-peuplier.jpg", "paysage-montagne.jpg", "chemin-automne.jpg", "prairie-alpes.jpg");
var numero = 0;

function ChangeSlide(sens) {
    numero = numero + sens;
    if (numero < 0)
        numero = slide.length - 1;
    if (numero > slide.length - 1)
        numero = 0;
    document.getElementById("slide").src = slide[numero];
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: post requests javascript 
Javascript :: js sort array 
Javascript :: call javascript function from python 
Javascript :: react image preview npm 
Javascript :: get last element from array javascript 
Javascript :: javascript set query parameters in url 
Javascript :: javascript array remove last 
Javascript :: js object destructuring 
Javascript :: for loop vue object 
Javascript :: for loop in javacript 
Javascript :: reactjs debounce 
Javascript :: JavaScript ForEach This Argument 
Javascript :: react animation 
Javascript :: TypeError: Converting circular structure to JSON 
Javascript :: dependency list useeffect 
Javascript :: javascript object as key 
Javascript :: npm whatsapp api 
Javascript :: Return with an "IF" Statement 
Javascript :: inertia js 
Javascript :: redux toolkit reducer 
Javascript :: redux reducer example 
Javascript :: vue prop using variable 
Javascript :: namespace javascript 
Javascript :: date time react component 
Javascript :: json to dart 
Javascript :: es6 import 
Javascript :: js timer 
Javascript :: line graph view click event 
Javascript :: calculate init code hash nodejs 
Javascript :: javascript program name 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =