javascript - How to slide a banner using jquery?

980

code:

<script>
  var slideIndex = 1;
  showSlides(slideIndex);
  function plusSlides(n) {
    showSlides(slideIndex += n);
  }
  function currentSlide(n) {
    showSlides(slideIndex = n);
  }
  function showSlides(n) {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    if (n > slides.length) {slideIndex = 1}  
    if (n < 1) {slideIndex = slides.length}
    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
    }
    for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[slideIndex-1].style.display = "block"; 
    dots[slideIndex-1].className += " active";
  }
</script>

<div >
  <?php
    if(empty($banner))
    {
      echo $this->session->flashdata('error');
    }
    else
    {
      foreach($banner as $ban)
      {
  ?>
        <a href="<?php echo base_url(); ?>product/<?php echo $ban['category']; ?>">
          <div >
            <img src="<?php echo base_url(); ?>asset/banner/<?php echo $ban['banner']; ?>" >
          </div>
        </a>
  <?php
      }
    }
  ?>
  <a onclick="plusSlides(-1)">&#10094;</a>
  <a onclick="plusSlides(1)">&#10095;</a>
</div>

In this code I am creating banner slider section where images are showing dynamically perfect but images are not sliding automatically one by one. So, How can I slide images one by one? Please help me.

Thank You

209

Answer

Solution:

I don't see any element with class dot. Removing references to those you can simply add a setInterval() callback.

var slideIndex = 1;
function plusSlides(n) {
    showSlides(slideIndex += n);
}
function currentSlide(n) {
    showSlides(slideIndex = n);
}
function showSlides(n) {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    if (n > slides.length) {slideIndex = 1}
    if (n < 1) {slideIndex = slides.length}
    for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }
    for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[slideIndex-1].style.display = "block";
    //dots[slideIndex-1].className += " active";
}

showSlides(slideIndex);
setInterval(function() {
    plusSlides(1);
}, 300);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div >
  <a href="">
    <div >
      <img src="https://dummyimage.com/100x100/000/fff&text=1">
    </div>
  </a>
  <a href="">
    <div >
      <img src="https://dummyimage.com/100x100/000/fff&text=2">
    </div>
  </a>
  <a href="">
    <div >
      <img src="https://dummyimage.com/100x100/000/fff&text=3">
    </div>
  </a>
  <br/>
  <a onclick="plusSlides(-1)">&#10094;</a>
  <a onclick="plusSlides(1)">&#10095;</a>
</div>



6
votes

Answer

Solution:

  • Useitv = setInterval(callback, timing) to autoplay the slideshow
  • UseclearInterval(itv) on'mouseenter' Event.
  • Don't use inline STYLE and JS likeonclick=""
  • Don't limit yourself on the number of slideshows per-page

Here's a slightly modified example:

!(function() {

  const slideshow = EL => {
  
    const ELS_prevNext = EL.querySelectorAll('.prev, .next');
    const ELS_slide = EL.querySelectorAll('.slide');
    const t = ELS_slide.length;
    let c = 0;
    let itv = null;
    
    const slide = () => {
      c = c<0 ? t-1 : c%t;
      ELS_slide.forEach(EL => EL.classList.remove('is-active'));
      ELS_slide[c].classList.add('is-active');
    };
    
    const play = () => itv = setInterval(() => {++c, slide()}, 3000);
    const stop = () => clearInterval(itv);
    
    ELS_prevNext.forEach(EL => EL.addEventListener('click', ev => {
      ev.preventDefault();
      c = (ev.currentTarget.classList.contains('next') ? ++c : --c);
      slide();
    }));
    
    EL.addEventListener('mouseenter', stop);
    EL.addEventListener('mouseleave', play);
    play(); // Start slideshow!!!
  };
  
  document.querySelectorAll('.slideshow').forEach(slideshow);
  
}());
* {
  margin: 0;
  box-sizing: border-box;
}

.slideshow {
  position: relative;
  width: 100%;
  height: 250px;
}

.slideshow .slide {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: opacity 0.4s, visibility 0.4s;
  visibility: hidden;
  opacity: 0;
}

.slideshow .slide.is-active {
  visibility: visible;
  opacity: 1;
}

.slideshow .slide img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.slideshow .prev,
.slideshow .next {
  cursor: pointer;
  user-select: none;
  position: absolute;
  font-size: 5em;
  color: #fff;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}

.slideshow .prev {
  right: auto;
}
<h2>SLIDESHOW AUTOPLAY AND PAUSE ON HOVER</h2>

<div >
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/0bf?text=1">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/f0b?text=2">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/bf0?text=3">
  </div>
 </a>
 <a >&#10094;</a>
 <a >&#10095;</a>
</div>

<h2>AS MANY SLIDESHOWS AS YOU WANT</h2>

<div >
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/fb0?text=A">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/0fb?text=B">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/b0f?text=C">
  </div>
 </a>
 <a >&#10094;</a>
 <a >&#10095;</a>
</div>



People are also looking for solutions of the problem: trying to access array offset on value of type null
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

204 javascript - How to slide a banner using jquery?
261 javascript - How to read Excel File data in php while sending it via jquery
41 javascript - jQuery - How to return value from php file and set it as a value to a textbox?
846 javascript - Using JS OOP with PHP
979 javascript - Can't pass PHP variables through JQUERY and insert them into MYSQL
512 php - Infinite loop when using cURL to access a REST API, what is causing it?
884 javascript - Is there a way to exchange data between pages with PHP without using SQL?
646 javascript - Hiding an element with jQuery doesn't work
432 javascript - JQuery - Correct way to populate data in form
575 php javascript causing blink on clicking any link in site
6

Answer

Solution:

  • Useitv = setInterval(callback, timing) to autoplay the slideshow
  • UseclearInterval(itv) on'mouseenter' Event.
  • Don't use inline STYLE and JS likeonclick=""
  • Don't limit yourself on the number of slideshows per-page

Here's a slightly modified example:

!(function() {

  const slideshow = EL => {
  
    const ELS_prevNext = EL.querySelectorAll('.prev, .next');
    const ELS_slide = EL.querySelectorAll('.slide');
    const t = ELS_slide.length;
    let c = 0;
    let itv = null;
    
    const slide = () => {
      c = c<0 ? t-1 : c%t;
      ELS_slide.forEach(EL => EL.classList.remove('is-active'));
      ELS_slide[c].classList.add('is-active');
    };
    
    const play = () => itv = setInterval(() => {++c, slide()}, 3000);
    const stop = () => clearInterval(itv);
    
    ELS_prevNext.forEach(EL => EL.addEventListener('click', ev => {
      ev.preventDefault();
      c = (ev.currentTarget.classList.contains('next') ? ++c : --c);
      slide();
    }));
    
    EL.addEventListener('mouseenter', stop);
    EL.addEventListener('mouseleave', play);
    play(); // Start slideshow!!!
  };
  
  document.querySelectorAll('.slideshow').forEach(slideshow);
  
}());
* {
  margin: 0;
  box-sizing: border-box;
}

.slideshow {
  position: relative;
  width: 100%;
  height: 250px;
}

.slideshow .slide {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: opacity 0.4s, visibility 0.4s;
  visibility: hidden;
  opacity: 0;
}

.slideshow .slide.is-active {
  visibility: visible;
  opacity: 1;
}

.slideshow .slide img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.slideshow .prev,
.slideshow .next {
  cursor: pointer;
  user-select: none;
  position: absolute;
  font-size: 5em;
  color: #fff;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}

.slideshow .prev {
  right: auto;
}
<h2>SLIDESHOW AUTOPLAY AND PAUSE ON HOVER</h2>

<div >
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/0bf?text=1">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/f0b?text=2">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/bf0?text=3">
  </div>
 </a>
 <a >&#10094;</a>
 <a >&#10095;</a>
</div>

<h2>AS MANY SLIDESHOWS AS YOU WANT</h2>

<div >
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/fb0?text=A">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/0fb?text=B">
  </div>
 </a>
 <a href="#!">
  <div >
   <img src="//placehold.it/800x600/b0f?text=C">
  </div>
 </a>
 <a >&#10094;</a>
 <a >&#10095;</a>
</div>



People are also looking for solutions of the problem: trying to access array offset on value of type null
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

204 javascript - How to slide a banner using jquery?
261 javascript - How to read Excel File data in php while sending it via jquery
41 javascript - jQuery - How to return value from php file and set it as a value to a textbox?
846 javascript - Using JS OOP with PHP
979 javascript - Can't pass PHP variables through JQUERY and insert them into MYSQL
512 php - Infinite loop when using cURL to access a REST API, what is causing it?
884 javascript - Is there a way to exchange data between pages with PHP without using SQL?
646 javascript - Hiding an element with jQuery doesn't work
432 javascript - JQuery - Correct way to populate data in form
575 php javascript causing blink on clicking any link in site

People are also looking for solutions to the problem: php - My login form returns the same form after authentication,my LDAP connection is not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.