Build a JavaScript IMAGE SLIDER in 15 minutes! 🖼️

Поділитися
Вставка
  • Опубліковано 27 сер 2024
  • #javascript #tutorial #course
    This is an image slider program that you can create as a beginner. We will be using element selectors to create a nodelist of images to cycle through. An image to be displayed will have a class added to their classList that contains display: block. You will need at least a few images of your choosing to work with. I recommend image that are roughly the same size.

КОМЕНТАРІ • 32

  • @BroCodez
    @BroCodez  8 місяців тому +30

    // IMAGE SLIDER
    const slides = document.querySelectorAll(".slides img");
    let slideIndex = 0;
    let intervalId = null;
    document.addEventListener("DOMContentLoaded", initializeSlider);
    function initializeSlider(){
    if(slides.length > 0){
    slides[slideIndex].classList.add("displaySlide");
    intervalId = setInterval(nextSlide, 5000);
    }
    }
    function showSlide(index){
    if(index >= slides.length){
    slideIndex = 0;
    }
    else if(index < 0){
    slideIndex = slides.length - 1;
    }
    slides.forEach(slide => {
    slide.classList.remove("displaySlide");
    });
    slides[slideIndex].classList.add("displaySlide");
    }
    function prevSlide(){
    clearInterval(intervalId);
    slideIndex--;
    showSlide(slideIndex);
    }
    function nextSlide(){
    slideIndex++;
    showSlide(slideIndex);
    }


    Document











    .slider{
    position: relative;
    width: 100%;
    margin: auto;
    overflow: hidden;
    }
    .slider img{
    width: 100%;
    display: none;
    }
    img.displaySlide{
    display: block;
    animation-name: fade;
    animation-duration: 1.5s;
    }
    .slider button{
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 2rem;
    padding: 10px 15px;
    background-color: hsla(0, 0%, 0%, 0.5);
    color: white;
    border: none;
    cursor: pointer;
    }
    .prev{
    left: 0;
    }
    .next{
    right: 0;
    }
    @keyframes fade {
    from {opacity: .5}
    to {opacity: 1}
    }

    • @usereternalyt
      @usereternalyt 8 місяців тому +1

      bro when you will begin python again😮‍💨

    • @l213dhanesh3
      @l213dhanesh3 8 місяців тому +1

      React do bro

    • @cinna_talks
      @cinna_talks 8 місяців тому +2

      bro continue the react series please...

    • @johnissac7
      @johnissac7 2 місяці тому

      O😂😆😆😆😃😃😃😀😀😀😁😁😁

  • @fixauto5434
    @fixauto5434 8 місяців тому +20

    I could never thank you more.
    I'm a student in prog at montreal and holy shit if I had you as a teacher I would have a perfect 100% attendance. You just get straight to the point and I love this so much. I usually don't comment but I'm at a point where I watch your videos even tho I don't need it. Keep up big love

  • @umarmuzammil7332
    @umarmuzammil7332 3 місяці тому +2

    the best videos for creating slider using javascript for beginners, love you bro

  • @ntech2761
    @ntech2761 4 місяці тому

    Your coding is the real thing! I have been figuring it out, went to others (youtube) and still could not make my slides move without getting a gap! Thank you so much!

  • @NawafXRMFC
    @NawafXRMFC 8 місяців тому +3

    very big fan and love your vids

  • @mysticaccent
    @mysticaccent 8 місяців тому +1

    I hope you get more views (millions of them) again, because you deserve all the happiness in the world. about a year ago my friend suggested to me a channel called 'brocode' and I searched it up on youtube and that was the best decision I ever made. I am 15 now, they are teaching us java at school but I am also learning python from your tutorials. thank you bro. it's people like you that make a difference.

  • @Tariquejamallkr
    @Tariquejamallkr 5 місяців тому

    Thanks Bro code, you have fixed all my issues regarding slider in one video.

  • @abdulbasit9364
    @abdulbasit9364 4 місяці тому

    Love you brother 💓 finally I found after wasting many hours

  • @luisgonzales9853
    @luisgonzales9853 3 місяці тому

    Thanks for the video! Glad you explained things instead of using stupid music! You rock. 🚀

  • @masengeshobruno-ug1fj
    @masengeshobruno-ug1fj 4 місяці тому +1

    thank you

  • @user-kr5so3xk8f
    @user-kr5so3xk8f 8 місяців тому

    Bro: Thank you, for taking the time to give me A private lessons. You helped me grow, I cc now you must have great parenting skills. I have much to learn from you. So I will get to work on my "eye of the tiger". Thank you bro once more.....

  • @umarmuzammil7332
    @umarmuzammil7332 3 місяці тому

    thanks a lot

  • @pastori2672
    @pastori2672 8 місяців тому +19

    ah so this is how tiktok works

  • @megcodm8961
    @megcodm8961 Місяць тому

    can anyone help me im stuck at the initialize slider part it wont show the photo it keeps saing this Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'classList')
    at initializeSlider i copied the exact same steps for the code but its not working

  • @futrozryby8474
    @futrozryby8474 Місяць тому

    Are you using classes cuz they are "less important" than id?

  • @Hairynigga776
    @Hairynigga776 8 місяців тому

    Can you make a Q&A video

  • @abdulbasit9364
    @abdulbasit9364 4 місяці тому

    I wanna ask something brother how to connect my sql private server data with JavaScript made software

  • @user-bd5tn7ki6q
    @user-bd5tn7ki6q 6 місяців тому

    Please help me reduce my image size. It is very big on screen. Entire code working fine. Thank you for yout

  • @BinaryMaestro1
    @BinaryMaestro1 8 місяців тому

    dont you think Arrays would be easier?

    • @BinaryMaestro1
      @BinaryMaestro1 8 місяців тому

      let x = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
      i = -1;
      //next
      document.getElementById("button3").onclick = function() {
      i = i + 1;
      if (i >= x.length) {
      i = 0
      };
      document.getElementById("h1").textContent = x[i];
      };
      //previous
      document.getElementById("button4").onclick = function() {
      i = i - 1;
      if (i < 0) {
      i = x.length - 1
      };
      document.getElementById("h1").textContent = x[i];
      }
      heres a project i did from your firsts videos and thats only a small part of the code tho
      also this is changing the h1 element name only because as i said its my old project

    • @peaklegacy146
      @peaklegacy146 7 місяців тому

      He's not doing what's easiest. He's teaching a lesson on how to apply skills to a situation

  • @thelavagod
    @thelavagod 8 місяців тому +1

    Early bro

  • @laperplayz1388
    @laperplayz1388 8 місяців тому +1

    day #2 of asking: yo bro can you please make an advanced c++ tutorial that talks about vectors and polymorphism, encapsulation, abstraction, and the auto keyword, and also new features added to the latest version please.

    • @batteo3318
      @batteo3318 8 місяців тому +1

      Give a try at Ferrabacus' course for encapsulation but dude, our bro is one man you can't ask him stuff like the newest features of c++ while he's doing a Javascript course. There are specific channels for that or wikis online.

    • @laperplayz1388
      @laperplayz1388 8 місяців тому

      @@batteo3318 thank you for the channel

  • @troonder6274
    @troonder6274 8 місяців тому

    F*ck, vette and JS in my recommendations. Good that's not November

  • @OdysseyAviation
    @OdysseyAviation 8 місяців тому

    9th comment