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 :: const { something} javascript 
Javascript :: return then javascript 
Javascript :: split text javascript 
Javascript :: Material-ui aircon icon 
Javascript :: add image inside a div on specific position javascript 
Javascript :: final-form reset form 
Javascript :: How to Subtract the numbers in the array, starting from the right in javascript 
Javascript :: javascript ean13 checksum generate 
Javascript :: get last element of array javascript 
Javascript :: add pdf in react app 
Javascript :: replaceAll vs replace vs split join 
Javascript :: expected an identifier and instead saw const 
Javascript :: type conversions in javascript 
Javascript :: von click 
Javascript :: generator function 
Javascript :: alex morgan 
Javascript :: nodejs ERR_CONNECTION_REFUSED 
Javascript :: get response from window.open 
Javascript :: document middleware in express 
Javascript :: codeceptjs "waitForClickable" 
Javascript :: data-parsley-errors-container 
Javascript :: Referrer Policy: strict-origin-when-cross-origin angular 
Javascript :: javascript wait to execute function on keyup 
Javascript :: lowest common ancestor leetcode 
Javascript :: jest tocontain 
Javascript :: upload multiple image using jquery 
Javascript :: react add splite image 
Javascript :: java script append element to array 
Javascript :: angular router navigate inside setTimeout 
Javascript :: js regex 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =