如何在 javascript 中用另一个变量递增一个变量
How to increment a variable by another variable in javascript
完整代码如下
我想将一个变量中的数字添加到 incrimented 变量中,但它们不相加。相反,他们得到了联系。
这一行
countScroll-=parseInt(scrollWidth_g);
如果 "scrollWidth_g" 是 300 并且 "countScroll" 是 10,我得到 10300,我想要 310;
$(document).ready(function(e) {
var countScroll = 10;
$(document).on('click', '.scroller_g_nav', function(){
var nav = $(this).attr('data-nav');
var scrollWidth = $('.scroller_g').attr('data-scrollwidth');
if (typeof scrollWidth == 'undefined'){
var scrollWidth_g = 200;
} else {
var scrollWidth_g = scrollWidth;
}
if (nav == 'left'){
countScroll-=parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
} else {
countScroll+=scrollWidth_g;
$('.scroller_g').animate({scrollLeft:countScroll},600);
}
});
});
减法时不需要解析字符串数字("123"),但加法时需要解析。
Example: "12" - 1 = 11
"12" + 1 = 121 //here you need to parse string to number to get correct result
parseInt("12") + 1 = 11
It works :
if (nav == 'left'){
countScroll -= parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
} else {
countScroll += parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
}
完整代码如下
我想将一个变量中的数字添加到 incrimented 变量中,但它们不相加。相反,他们得到了联系。
这一行
countScroll-=parseInt(scrollWidth_g);
如果 "scrollWidth_g" 是 300 并且 "countScroll" 是 10,我得到 10300,我想要 310;
$(document).ready(function(e) {
var countScroll = 10;
$(document).on('click', '.scroller_g_nav', function(){
var nav = $(this).attr('data-nav');
var scrollWidth = $('.scroller_g').attr('data-scrollwidth');
if (typeof scrollWidth == 'undefined'){
var scrollWidth_g = 200;
} else {
var scrollWidth_g = scrollWidth;
}
if (nav == 'left'){
countScroll-=parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
} else {
countScroll+=scrollWidth_g;
$('.scroller_g').animate({scrollLeft:countScroll},600);
}
});
});
减法时不需要解析字符串数字("123"),但加法时需要解析。
Example: "12" - 1 = 11
"12" + 1 = 121 //here you need to parse string to number to get correct result
parseInt("12") + 1 = 11
It works :
if (nav == 'left'){
countScroll -= parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
} else {
countScroll += parseInt(scrollWidth_g);
$('.scroller_g').animate({scrollLeft:countScroll},600);
}