【发布时间】:2019-05-23 01:17:32
【问题描述】:
我正在制作一个菜单,显示您在我的网站上的哪个部分。我有 3 个圈子可以显示您所在的部分。
如果您点击“向下”,您将进入一个部分,如果您点击“向上”,您将返回一个部分。
现在我做到了,如果您单击“向下”,它会将 1 添加到变量“sectionCounter”,如果您单击向上,它会从中删除 1。
我想添加一个最小值为 1,一个最大值为 3,因为没有更多的部分,我该怎么做?
这是我的 jQuery:
var sectionCounter = 1;
sectionCounter == 1;
$('.down1').click(function() {
var section2 = $('.section2');
var pos = section2.offset().top;
sectionCounter += 1;
$('h1').html(sectionCounter);
if (sectionCounter == 1){
$('.count1').addClass('countActive');
}else {
$('.count1').removeClass('countActive');
};
if (sectionCounter == 2){
$('.count2').addClass('countActive');
}else {
$('.count2').removeClass('countActive');
};
if (sectionCounter == 3){
$('.count3').addClass('countActive');
}else {
$('.count3').removeClass('countActive');
};
$('html, body').animate({scrollTop:pos},2000); // will take two seconds to scroll to the element
});
$('.up1').click(function() {
var section1 = $('.section1');
var pos2 = section1.offset().top;
sectionCounter -= 1;
$('h1').html(sectionCounter);
if (sectionCounter == 1){
$('.count1').addClass('countActive');
}else {
$('.count1').removeClass('countActive');
};
if (sectionCounter == 2){
$('.count2').addClass('countActive');
}else {
$('.count2').removeClass('countActive');
};
if (sectionCounter == 3){
$('.count3').addClass('countActive');
}else {
$('.count3').removeClass('countActive');
};
$('html, body').animate({scrollTop:pos2},2000); // will take two seconds to scroll to the element
});
【问题讨论】:
-
旁注:不要将
;放在附加到控制流语句的块之后(if、else、while、for、switch) .这样做是无害的(因为 JavaScript 有 EmptyStatement,这实际上意味着它会忽略无关的;s),但也毫无意义。
标签: javascript jquery variables max min