jQuery - 滚动时淡出/“滚动停止”时淡入
我有一个 div 定位工作,它由滚动事件触发。如果滚动事件被多次触发,导致 div 闪烁,会发生什么情况?我的计划是淡出该 div 并在不再触发滚动事件后立即淡入。如何检查滚动是否结束?我考虑了超时的组合<->滚动但实际上没有任何效果如我所愿。这是我到目前为止所得到的。
$(document).ready(function(){
//var animActive = false;
$(window).scroll(function() {
/*
if (animActive == false){
animActive = true;
$('.mceExternalToolbar').fadeOut(100, function () {
$('.mceExternalToolbar').fadeIn(3000, function () {
animActive = false;
console.log("NOW");
});
});
}
*/
topParentx = $('#tinyMCEwrapper').position().top;
if ($(this).scrollTop() >= topParentx){
$('.mceExternalToolbar').css('top', ($(this).scrollTop()-topParentx) + "px");
} else {
$('.mceExternalToolbar').css('top', "0px");
};
});
});
正如您所看到的,我将最后一次尝试留在那里,但是淡入淡出函数的回调并未按预期工作。
I have a div positioning working which gets fired by the scroll-event. What happens it that the scroll event gets fired a bunch of times which results in a flickering div. My plan is to fade out that div and fade back in as soon as no more scroll event is fired. How can I check that scrolling is over? I thought about a combination of timeout <-> scroll but actually nothing worked as I hoped. Here's what i got so far.
$(document).ready(function(){
//var animActive = false;
$(window).scroll(function() {
/*
if (animActive == false){
animActive = true;
$('.mceExternalToolbar').fadeOut(100, function () {
$('.mceExternalToolbar').fadeIn(3000, function () {
animActive = false;
console.log("NOW");
});
});
}
*/
topParentx = $('#tinyMCEwrapper').position().top;
if ($(this).scrollTop() >= topParentx){
$('.mceExternalToolbar').css('top', ($(this).scrollTop()-topParentx) + "px");
} else {
$('.mceExternalToolbar').css('top', "0px");
};
});
});
As you can see I left one of my last attempts in there, but the callbacks of the fade-function didn't work as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,没有滚动停止的概念,因此您无法真正从中触发动画。更好的方法可能是为 div 的“top”属性设置动画,以便它平滑地滑动到新位置而不是闪烁。
Unfortunately there is no concept of scroll-stop so you can't really trigger an animation from that. What may work better is to instead animate the 'top' property of your div so that it smoothly slides to it's new position instead of flickering.
您可以使用 jQuery 特殊事件来创建滚动停止事件。 James Padolsey 编写了一个很棒的scrollstop 事件示例。
You can use jQuery special events for creating a scrollstop event. James Padolsey has written a great example of scrollstop event.
修复滚动时不脉冲!设置超时
Fix to not pulse on scroll! settimeout
好吧,虽然我昨天很高兴……今天现实又回来了……SAFARI 的反应是不重新渲染移动 div 后面的所有必要片段。特别是在tinyMCE 的iframe 上。
所以我最终得到了以下结果,并且效果很好。淡出 div ->动画到位置 ->仅当回调被触发时淡入。
Ok while i was happy yesterday... Reality stroke back today... SAFARI reacts with not re-rendering all necessary fragments behind the moving div. Especially over tinyMCE's iframe.
So i ended up with the following and this works quite well. Fades out the div -> animation to position -> Fade in only if callback is fired..