Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Johna

Frontend Developer
Staff Team
Guardian
I've been trying to make an image carousel for quite a while not, I've finally got somewhere, but there's still one issue. When the carousel moves to the next image, rather that the last image sliding in, it just appears. Same with going to previous Image, but then it's the first image.

I know why it's doing that, I just want to know how do it so it doesn't do that.


Here's my code:

HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js" charset="utf-8"></script>
<link rel="stylesheet" href="style.css">

<div class="slider-outer">
  <div class="container">
      
    <!--Slider images-->
    <div class="slider-wrapper">
      <div class="inner-wrapper">
        <div class="slide"><img src="../images/SPRING.png" alt="Spring"></div>
        <div class="slide"><img src="../images/ALBUMS.png" alt="Albums"></div>
        <div class="slide"><img src="../images/HIRE.png" alt="Hire Me"></div>
        <div class="slide"><img src="../images/BN.png" alt="Burn Notice"></div>
        <div class="slide"><img src="../images/SG.png" alt="Stargirl"></div>
      </div>
    </div>
      
    <!--Slider buttons-->
    <div class="button prev"><img src="../images/LA.png" alt="Previous image"></div>
    <div class="button next"><img src="../images/RA.png" alt="Next image"></div>
  </div>
</div>

<script src="script.js" charset="utf-8"></script>

CSS:
.slider-outer {
  height: 468px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.container {
  max-width: 5000px;
  height: 468px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}


.slider-wrapper {
  width: 5000px;
  height: 468px;
  overflow: hidden;
}

.inner-wrapper {
  width: 5000px;
  height: 468px;
  position: relative;
  left: 0px;
}

.slide {
  width: 1000px;
  height: 468px;
  display: flex;
  justify-content: center;
  align-items: center;
  float: left;
}

.button {
  width: 22px;
  height: 40px;
  position: absolute;
  cursor: pointer;
  overflow: hidden;
  user-select: none;
}

.button img {
  user-select: none;
}

.button img:hover {
  margin-top: -40px;
}

.prev {
  right: calc(50% + 540px);
}

.next {
  left: calc(50% + 540px);
}

JavaScript:
$(function() {
  var inWrap = $('.inner-wrapper'),
      $slide = $('.slide');

  function nextImage() {
    inWrap.animate({'left': '-1000px'}, 200, function() {
      inWrap.css('left', '0px');
      $('.slide').last().after($('.slide').first());
    });
  };

  sliderInterval = setInterval(nextImage, 7000);

  $(".container").hover(function(){
    clearInterval(sliderInterval);
  }, function(){
    sliderInterval = setInterval(nextImage, 7000);
  });

  $('.prev').on('click', function() {
    inWrap.animate({'left': '1000px'}, 200, function() {
      inWrap.css('left', '0px');
      $('.slide').first().before($('.slide').last());
    });
  });

  $('.next').on('click', function() {
    nextImage();
  });
});


Thanks.
 
Back
Top Bottom