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 :: Create a react project easily 
Javascript :: flask sqlalchemy json 
Javascript :: toast notification angular bootstrap 8 
Javascript :: javascript object properties 
Javascript :: app script append two list 
Javascript :: Cypress failed to make a connection to the Chrome DevTools Protocol after retrying for 50 seconds. 
Javascript :: useEfefct react 
Javascript :: javascript ajax receive multiple values 
Javascript :: sidebar scroll css 
Javascript :: javascript split text after x characters 
Javascript :: replace in string all occurance jquery 
Javascript :: dataset javascript 
Javascript :: JavaScript Checking in switch Statement 
Javascript :: draft js insert text example 
Javascript :: isodate mongodb nodejs 
Javascript :: clock picker jquery 
Javascript :: count duplicate array javascript 
Javascript :: react event for modals 
Javascript :: sum function in javascript 
Javascript :: how to upload picture on canvas in react 
Javascript :: jquery alertify 
Javascript :: bubbling and capturing in javascript 
Javascript :: js variable to string 
Javascript :: could not decode base64 cloudinary 
Javascript :: jquery datatable server side pagination asp.net core 
Javascript :: freecodecamp cdn 
Javascript :: login with facebook expo react native 
Javascript :: display unique id number in angular 
Javascript :: class in javascript 
Javascript :: message.channel.name.includes 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =