我正在尝试制作幻灯片/轮播,并且在“ https://www.w3schools.com”上找到了一些参考。 代码非常简单明了,但我不理解JavaScript中的逻辑,周长“ n”让我发疯。 有人可以向我解释“ n”在做什么以及其用途吗?
(我无法发布问题,我需要更多的单词)Lorem ipsum dolor坐在,谦虚地奉献了精英。 Sint Nihil est假定您对Namlor的看法不正确,请务必在错误之前将Dolorem转让给Deserunt quisquam quis porro labouriosam eaque perferendis nulla,dictamagno magni。埃塞俄比亚的福吉特·尼尔·希尔斯(Pugit nihil)造成的后果是什么?
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";
}
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.prev, .next,.text {font-size: 11px}
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="https://helpx.adobe.com/in/stock/how-to/visual-reverse-image-search/_jcr_content/main-pars/image.img.jpg/visual-reverse-image-search-v2_1000x560.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
</body>
</html>
答案 0 :(得分:1)
n
只是传递给相应函数的参数,
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
在以上两个函数中,n
是传递给它的参数。
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
在这里您可以看到您正在将1、2、3个值传递给currentSlide()
函数。 n将保存您传递给函数的值。在这里是1或2或3。
答案 1 :(得分:1)
相关代码为:
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
plusSlides
以下是一个更清晰的plusSlides
版本:
function plusSlides(n) {
slideIndex = slideIndex + n;
showSlides(slideIndex);
}
如果您调用它并传递一个数字,则该函数会将该数字作为参数n
接收,它是:
n
添加到slideIndex
(这是函数关闭的变量),并将结果保存在slideIndex
中;和showSlides
(该值刚刚存储在slideIndex
例如,如果slideIndex
为2,并且您将1传递给plusSlides
,它将slideIndex
设置为3(其旧值2加1),并在索引3。
currentSlide
currentSlide
可能更清晰的版本是:
function currentSlide(n) {
slideIndex = n;
showSlides(slideIndex);
}
如果您用数字呼叫currentSlide
,则它将以n
的形式接收该数字,并且
slideIndex
到该数字;和showSlides
例如,如果slideIndex
为2,并且您将1传递给currentSlide
,它将slideIndex
设置为1(替换其旧值)并显示幻灯片在索引1处。