简体   繁体   English

如何在一个页面中多次重复使用一块Javascript

[英]How to reuse a piece of Javascript multiple times in one page

I'm new to building websites and have a (probably quite basic) question about using a Javascript function that's been written for a slideshow.我是建立网站的新手,并且有一个(可能是非常基本的)关于使用为幻灯片编写的 Javascript function 的问题。 I have a one-page website which uses multiple slideshow units – made up of the slideshow div, holder and a counter (eg 1/4), all of which is controlled by the following js:我有一个使用多个幻灯片单元的单页网站——由幻灯片 div、支架和一个计数器(例如 1/4)组成,所有这些都由以下 js 控制:

var currentSlide = 0
var totalSlides = $('.holder div').length

var nextSlide = function() {
console.log('nextSlide')
currentSlide = currentSlide + 1

if (currentSlide >= totalSlides) {
    currentSlide = 0
}

var leftPosition = (-currentSlide * 100) + 'vw'
$('.holder').css('left', leftPosition)

var slideNumber = currentSlide + 1
$('.steps').text(slideNumber + '/' + totalSlides)
}


var prevSlide = function() {

  currentSlide = currentSlide - 1


if (currentSlide < 0) {
  currentSlide = totalSlides - 1
}


var leftPosition = (-currentSlide * 100) + 'vw'
$('.holder').css('left', leftPosition)

var slideNumber = currentSlide + 1
$('.steps').text(slideNumber + '/' + totalSlides)

}

var autoSlide = setInterval(function() {
  nextSlide()
}, 100000000)

$('.next').on('click', function() {
  clearInterval(autoSlide)
  nextSlide()
})

$('.prev').on('click', function() {
  clearInterval(autoSlide)
  prevSlide()
})


var slideNumber = currentSlide + 1
$('.steps').text(slideNumber + '/' + totalSlides)

$('body').on('keydown', function(event) {

  var keyCode = event.keyCode

  if (keyCode == 37) {
    clearInterval(autoSlide)
    prevSlide()
  }

  if (keyCode == 39) {
    clearInterval(autoSlide)
    nextSlide()
  }

})

My set up in HTML is this:我在 HTML 中的设置是这样的:

<main class="scroll-container">
         <section id="project-1" class="toxic-yellow">
         <div class="slideshow">
           <div class="holder">
             <div class="slide-1 image next"><img src="images/01.jpg"></div>
             <div class="slide-2 image next">
               <p class="project-text">
                 Project 1, 3D, 2020<br>
                 Insert the project text here insert the project text here insert the project text here insert the project text here insert the project text here
               </p>
             </div>
             <div class="slide-3 image next"><img src="images/05.jpg"></div>
             <div class="slide-4 image next"><img src="images/02.jpg"></div>
             <div class="slide-5 image next"><img src="images/04.jpg"></div>
             <div class="slide-6 image next"><img src="images/06.jpg"></div>
           </div>
         </div>
           <p class="caption">Project 1</p>
           <p class="steps"></p>
         </section>
         <section id="project-2" class="lilac">
         <div class="slideshow">
           <div class="holder">
             <div class="slide-1 image next"><img src="images/05.jpg"></div>
             <div class="slide-2 image next">
               <p class="project-text">
                 Project 1, 3D, 2020<br>
                 Insert the project text here insert the project text here insert the project text here insert the project text here insert the project text here
               </p>
             </div>
             <div class="slide-3 image next"><img src="images/05.jpg"></div>
             <div class="slide-4 image next"><img src="images/02.jpg"></div>
             <div class="slide-5 image next"><img src="images/04.jpg"></div>
             <div class="slide-6 image next"><img src="images/06.jpg"></div>
           </div>
         </div>
           <p class="caption">Project 2</p>
           <p class="steps"></p>
         </section>

And so on.等等。 I have 12 slideshows in total on the same page.我在同一页面上总共有 12 个幻灯片。 The problem I'm encountering is that the script applies globally across the whole page but I'd like to control each slideshow independently of each other.我遇到的问题是脚本在整个页面上全局应用,但我想独立控制每个幻灯片。 Is there a way to reuse this script and apply it separately to each instance of the slideshow?有没有办法重用此脚本并将其分别应用于幻灯片的每个实例?

Thanks for your help!谢谢你的帮助!

You can use JavaScript Functions or Classes.您可以使用 JavaScript 函数或类。

W3Schools Reference Functions W3Schools 参考函数

W3Schools Reference Classes W3Schools 参考类

$('body').on('keydown', function(event)

With this you are applying change to all of the elements.有了这个,您正在对所有元素应用更改。

Judging by your code, you can trigger the event listener to change the slides only to the child elements of the parent element( section ) which has id of "project-X".从您的代码来看,您可以触发事件侦听器将幻灯片仅更改为 id 为“project-X”的父元素( section )的子元素。 Have a look here看看这里

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

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