简体   繁体   English

带有箭头的Javascript滑块

[英]Javascript slider with arrows

I want to make a slider with arrows i tried several methods but i failed still new so i dont really know where to begin here is my html, 我想制作一个带有箭头的滑块,我尝试了几种方法,但是我还是失败了,所以我真的不知道从哪里开始是我的html,

    <div class="previous-butn"></div>
<div class="slider">
<a href="">
<img src="assets/media/images/colormood-slide.jpg" alt="">
</a>
<div class="next-butn"></div>

and here is my js 这是我的js

const currentSlide = document.querySelector('.slider img');
const projectSlide = [
'assets/media/images/colormood-slide.jpg',
'assets/media/images/cancellato-slide.jpg',
'assets/media/images/grazia-slide.jpg',
'assets/media/images/maxnextdoor-slide.jpg',
'assets/media/images/sasha-slide.jpg',
'assets/media/images/windinthecity-slide.jpg',
'assets/media/images/gullsnitt-slide.jpg'
];
let slidesCounter = 1;

First of all create the HTML and CSS for your slider. 首先,为滑块创建HTML和CSS。 Use one single div that contains everything else. 使用一个包含其他所有内容的单个div。 In your slider case, the container div should contain two major divs, one for the slider images and another containing control buttons, that is, the arrows. 在您的滑块情况下,容器div应该包含两个主要的div,一个用于滑块图像,另一个包含控制按钮,即箭头。

In the container of slider images, put multiple images. 在滑块图像的容器中,放置多个图像。

Use CSS a class such as 'active' on the first image only. 仅在第一张图片上使用CSS这样的类,例如“ active”。 Now write CSS to hide all images except the one having the 'active' class. 现在编写CSS以隐藏除具有“ active”类的图像以外的所有图像。 Also use CSS to place the navigator arrows where you want them to be. 还可以使用CSS将导航器箭头放置在想要的位置。

Now open the page in the browser and see if it is okay. 现在,在浏览器中打开页面,看看是否可以。 When its okay move forward to JS 当可以的时候前进到JS

Add click events to your arrow buttons (not considering autoplay at this moment) 将点击事件添加到箭头按钮(目前不考虑自动播放)

Following is the logic for NEXT button 以下是NEXT按钮的逻辑

  1. CurrentActiveImageIndex = Get the index of the image with the class active CurrentActiveImageIndex =在活动类的情况下获取图像的索引
  2. TotalNumberOfImages = Count total number of images in the slider TotalNumberOfImages =计算滑块中的图像总数
  3. NextActiveImageIndex = CurrentActiveImageIndex + 1 NextActiveImageIndex = CurrentActiveImageIndex +1
  4. IF NextActiveImageIndex == TotalNumberOfImages 如果NextActiveImageIndex == TotalNumberOfImages
  5. THEN NextActiveImageIndex = 0 然后NextActiveImageIndex = 0
  6. Remove the class active from all images 从所有图像中删除活动班级
  7. Add the class active to the image at NextActiveImageIndex 将活动类添加到NextActiveImageIndex上的图像

Logic for the PREVIOUS button would just the opposite, following are the changed lines only PREVIOUS按钮的逻辑正好相反,以下仅是更改的行

  1. NextActiveImageIndex = CurrentActiveImageIndex - 1 NextActiveImageIndex = CurrentActiveImageIndex-1
  2. IF NextActiveImageIndex < 0 如果NextActiveImageIndex <0
  3. THEN NextActiveImageIndex = TotalNumberOfImages - 1 然后,NextActiveImageIndex = TotalNumberOfImages-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM