我想在run为1时运行一个函数check(),然后才运行 我遇到的问题是check()函数向左滚动,然后再次运行该函数,
如何创建一个布尔值并将其白色翻转,函数检查一直运行?
我很抱歉这是我的第一个问题,感谢所有的帮助!!
$( "#element" ).scrollstop(function() {
run = 0
check()
});
function check(){
console.log(run)
if(run === 1){
$('#element').animate({
scrollLeft: '10px'
})
run=0
}
}
答案 0 :(得分:1)
run
无法看到 check()
。你应该把它作为一个参数传递:
$( "#element" ).scrollstop(function() {
var run = 0;
check(run);
});
function check(run){
console.log(run);
if(run === 1){
$('#element').animate({
scrollLeft: '10px';
});
}
}
但是,您无法在run
中将check()
恢复为0,您必须在其他地方执行此操作。
你也错过了分号。我将它们添加到代码中。
我建议你阅读一些关于javascript范围的教程,例如:http://www.w3schools.com/js/js_scope.asp
答案 1 :(得分:0)
我已全局声明run
变量并将值更改为布尔值而不是整数,因此代码更清晰。
var run = false;
$("#element").scrollstop(function() {
run = true; // this must be true before calling check()
check();
});
function check(){
console.log(run)
if(run){
run = false;
$("#element").animate({
scrollLeft: '10px'
});
}
}
希望它有所帮助。
答案 2 :(得分:0)
尝试以下代码,
var documentScroll = function() {
$(document).animate({
scrollLeft: '10px'
}, function() {
$(document).one('scroll', documentScroll);
});
};
$(document).one('scroll', documentScroll);