这样写有什么好的优化方法
var times3 = 0,times4 = 0;
$(".touch-right-3").on("click",function(){
times3 += 1;
left = -100 * times3 + "%";
if(times3<=4){
$(".list-content-3").animate({left:left});
}else if(times3>=4){
times3 = 4;
}
});
$(".touch-left-3").on("click",function(){
times3 -= 1;
left = -100 * times3 + "%";
if(times3>=0){
$(".list-content-3").animate({left:left});
}else if(times3<0){
times3 = 0;
}
});
$(".touch-right-4").on("click",function(){
times4 += 1;
left = -100 * times4 + "%";
if(times4<=4){
$(".list-content-4").animate({left:left});
}else if(times4>=4){
times4 = 4;
}
});
$(".touch-left-4").on("click",function(){
times4 -= 1;
left = -100 * times4 + "%";
if(times4>=0){
$(".list-content-4").animate({left:left});
}else if(times4<0){
times4 = 0;
}
});
//调用完pageNext函数之后,在pagePre方法中取到的page值仍然是初始设置的0,怎么取到pageNext方法中改变后的page的值?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这个很明显,你是把page的值传入到函数体内部了,然后再对函数的time参数进行赋值,
这是修改后的:可以直接看最后面的函数调用
问题出在
每次click点击,pageNext函数将page这个全局变量+1,pagePre将page这个全局变量-1,所以pagePre中alert的times在times的-1操作后始终为0;
另外不知道你return times有什么用。
解决办法:把pagePre中的-1放到DOM操作后面。