以下完整代码
我想在变量中添加一个数字到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);
}
});
});
答案 0 :(得分:2)
在减法时,您不需要解析字符串编号(" 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);
}