我正在制作一个菜单,显示您在我网站上的哪个部分。我有3个圆圈,显示您位于哪个部分。
如果您单击“向下”,则可以再走一段;如果您单击“向上”,则可以再走一段。
现在,我将其单击为“向下”,将其添加到变量“ 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
});
答案 0 :(得分:1)
您有两种选择:
ALTER TABLE `users_crime_masteries`
ADD UNIQUE KEY `uid` (`uid`),
ADD UNIQUE KEY `cid` (`cid`),
cid
对于if
情况类似:
if (sectionCounter < 3) {
sectionCounter += 1; // Side note: `++sectionCounter;` or `sectionCounter++;` would be more idiomatic
}
-= 1
运算符:if (sectionCounter > 1) {
sectionCounter -= 1; // Side note: `--sectionCounter;` or `sectionCounter--;` would be more idiomatic
}
对于&&
情况类似:
sectionCounter < 3 && ++sectionCounter;
之所以有效,是因为如果左侧操作数为true(或者为truthy),则不会评估右侧操作数。
-= 1
/ sectionCounter > 1 && --sectionCounter;
:Math.min
和
Math.max