我正在尝试使用iScrolljs构建包含轮播的页面。旋转木马的位置应该遵循视频中的位置I。这与问题并不相关,但我认为我应该提供尽可能多的信息。
自动滚动有效但我也希望用户可以自己滚动。问题是当你每次调用间隔时滚动现在,轮播滚动到前一个位置。我尝试使用清除间隔并重置它们但它似乎不起作用。这就是我所拥有的:
video.onplay = function(){
myScroll = new IScroll('#carWrapper',{
scrollX: true,
scrollY: false,
momentum: false,
click:true,
mouseWheel:true,
tap: true,
keyBindings: true
});
scroll();
};
function checkMoved(){
if(document.getElementById('carWrapper')){
if(!scroller.moved){
var scrollPos = (video.currentTime/video.duration)*4800;
myScroll.scrollTo(-scrollPos,0,1000,IScroll.utils.ease.elastic);
}
}
if(myScroll.moved){
clearInterval(scrollInterval);
myScroll.moved = false;
setTimeout(scroll(),10000);
}
}
function scroll(){
scrollInterval = setInterval(checkMoved,5000);
}
提前致谢!
答案 0 :(得分:1)
我认为浏览器没有看到scrollInterval函数checkMoved() 你必须全局声明它然后在滚动函数
中分配它var scrollInterval;
video.onplay = function () {
myScroll = new IScroll('#carWrapper', {
scrollX: true,
scrollY: false,
momentum: false,
click: true,
mouseWheel: true,
tap: true,
keyBindings: true
});
scroll();
};
function checkMoved() {
if (document.getElementById('carWrapper')) {
if (!scroller.moved) {
var scrollPos = (video.currentTime / video.duration) * 4800;
myScroll.scrollTo(-scrollPos, 0, 1000, IScroll.utils.ease.elastic);
}
}
if (myScroll.moved) {
clearInterval(scrollInterval);
myScroll.moved = false;
setTimeout(scroll(), 10000);
}
}
function scroll() {
scrollInterval = setInterval(checkMoved, 5000);
}
答案 1 :(得分:1)
我稍微更改了代码并使其正常工作。
video.onplay = function(){
myScroll = new IScroll('#carWrapper',{
scrollX: true,
scrollY: false,
momentum: false,
click:true,
mouseWheel:true,
tap: true,
keyBindings: true
});
scrollInterval = setInterval(function(){
if(myScroll.moved){
resetInterval();
myScroll.moved = false;
}
else if(document.getElementById('carWrapper')){
if(!scroller.moved){
var scrollPos = (video.currentTime/video.duration)*4800;
myScroll.scrollTo(-scrollPos,0,1000,IScroll.utils.ease.elastic);
}
}
},5000);
};
function resetInterval(){
clearInterval(scrollInterval);
scrollTimeout = setTimeout(function(){
scrollInterval = setInterval(function(){
if(myScroll.moved){
resetInterval();
myScroll.moved = false;
}
else if(document.getElementById('carWrapper')){
if(!scroller.moved){
var scrollPos = (video.currentTime/video.duration)*4800;
myScroll.scrollTo(-scrollPos,0,1000,IScroll.utils.ease.elastic);
}
}
},5000);
},10000);
}
这可能不是最干净的代码,但我很高兴它有效。