【发布时间】:2018-10-17 10:31:35
【问题描述】:
所以我一直在尝试制作一个动态滚动动画,它可以反复上下滚动,但是一旦鼠标进入网页的主 div 就可以停止,然后在光标离开上述 div 时重新启动,它将需要如果用户滚动了自己,以及内容的数量是否发生了变化,则计入计数
但它所做的只是正确启动第一个动画,然后放弃在底部停止,根本拒绝让用户滚动。
当我尝试触发停止功能时,它只会给我一个错误“TypeError: animate(...) is undefined, can't access property 0 of it”,我不知道为什么。
有什么想法吗?
setTimeout(function(){
if(screen.width >= 1300 && $('#animatediv')[0].scrollHeight > 700){
var screenview = document.getElementById('animatediv').clientHeight;
var to = $('#animatediv')[0].scrollHeight;
var animatetime = ((to - 700) * 800) / 80;
var used = 0;
var delay = 2000;
var userscroll = 0;
var animatescroll = 0;
var running = 0;
$('#main').mouseleave(function() {
running = 1;
userscroll = $('#animatediv').scrollTop();
setTimeout(function(){animate(screenview, to, animatetime, used, delay, userscroll, animatescroll, running)}, delay);
});
$('#main').mouseenter(function() {
running = 0;
var news = animate(screenview, to, animatetime, used, delay, userscroll, animatescroll, running)
used = news[0];
to = news[1];
$('#animatediv').stop();
animatescroll = $('#animatediv').scrollTop();
});
}
}, 600);
function animate(screenview, to, animatetime, used, delay, userscroll, animatescroll, running){
to = $('#animatediv')[0].scrollHeight;
var time = (animatetime - used) + (animatescroll - userscroll);
if(to != 0){
animateto = to - screenview;
}
else{
animateto = 0;
}
$('#animatediv').animate({ scrollTop: animateto}, time, 'linear');
setInterval(function(){
if(running == 0){
var returns = [used, to]
return returns;
}
if(used == animatetime){
if(to != 0){
to = 0;
}
else{
to = $('#animatediv')[0].scrollHeight;
}
used = 0;
setTimeout(function(){animate(screenview, to, animatetime, used, delay, userscroll, animatescroll)}, delay);
}
else{
used++;
}
}, 1);
}
【问题讨论】:
-
制作一个 sn-p 或一个 jsfiddle
-
好吧,我不知道为什么,因为我使用的所有 javascript 都已提供,但这里你去jsfiddle.net/QXQXQX/xpvt214o/892750 也很抱歉响应缓慢。
标签: javascript jquery scroll jquery-animate