简体   繁体   English

jQuery滚动一次动画

[英]jQuery animate once on scroll

I have a simple script that basically waits till my navigation has scrolled off the screen, resizes it slightly then it animates in from the top of the screen and remains fixed till i scroll back up. 我有一个简单的脚本,基本上可以等到导航从屏幕上滚动下来后再稍加调整大小,然后从屏幕顶部开始进行动画处理,并保持固定状态,直到我向上滚动为止。

When you scroll past the menu it animates back in, but then repeats this animation every time I finish scrolling. 当您滚动浏览菜单时,它会重新设置动画,但是每次我完成滚动时,都会重复此动画。 I have tried adding the animation outside of the scroll function but then it doesn't animate at all. 我尝试将动画添加到滚动功能之外,但是它根本没有动画。

Have i missed something simple? 我错过了一些简单的事情吗?

    var Header = $('#header'),
        Navbar = $('.navbar'),
        links = $(".navbar ul.nav > li > a"),
        HeaderH = Header.height(),
        NavbarH = Navbar.height();

$(window).scroll(function () {

    if ($(this).scrollTop() > (HeaderH + NavbarH)) {
        Navbar.addClass('navbar-fixed-top')
        links.css('padding', '10px 20px 10px 20px');
        Header.css('margin-bottom', '64px');
        Navbar.css('top', '-64px').animate({'top' : '0'}, 1000);

    }
    else{
        Navbar.removeClass('navbar-fixed-top')
        links.css('padding', '20px');
    }
});

The site is built on joomla with bootstrap, hence the navbar-fixed-top style classes. 该网站建立在具有引导程序的joomla上,因此是navbar-fixed-top样式类。

Your scroll handler will be called multiple times as the user scrolls. 用户滚动时,滚动处理程序将被多次调用。

To illustrate this, if you add a console log 为了说明这一点,如果您添加控制台日志

    $(window).on('scroll', function(){
    console.log('scroll');
});

You'll see "scroll" output multiple times as you scroll down. 向下滚动时,您会多次看到“滚动”输出。 This is because each little gradation of scroll fires off a scroll event. 这是因为滚动的每个小层次都会触发滚动事件。

A common approach is to wrap your callback in a debounce function, so it only runs once in a given period, even if more scroll events are fired. 一种常见的方法是将您的回调包装在一个反跳函数中,因此即使在触发了更多滚动事件的情况下,它也只能在给定的时间内运行一次。

  • Include the throttle/debounce jQuery plugin in your page - get it from http://benalman.com/projects/jquery-throttle-debounce-plugin/ . 在您的页面中包含节流阀/去抖动jQuery插件-从http://benalman.com/projects/jquery-throttle-debounce-plugin/获取。
  • Tweak your code like so: 像这样调整代码:

    function scrollHandler() { 函数scrollHandler(){

      if ($(this).scrollTop() > (HeaderH + NavbarH)) { Navbar.addClass('navbar-fixed-top') links.css('padding', '10px 20px 10px 20px'); Header.css('margin-bottom', '64px'); Navbar.css('top', '-64px').animate({'top': '0'}, 1000); } else { Navbar.removeClass('navbar-fixed-top') links.css('padding', '20px'); } } $(window).scroll($.debounce(750, scrollHandler)); 

This will make it respond only for one scroll event per 750 milliseconds - change the duration to your taste. 这将使其每750毫秒仅响应一次滚动事件-更改您的口味的持续时间。

Stackoverflow code block is freaking out for some reason - please note the "function scrollHandler(){" line above. 由于某些原因,Stackoverflow代码块变得异常奇怪-请注意上面的“ function scrollHandler(){”行。

A more economical approach 更经济的方法

A more concise way of doing the same thing is to check if the scroll event's been fired in a given period and if it hasn't to assume the user's stopped scrolling. 做同样事情的一种更简洁的方法是检查滚动事件是否在给定的时间内触发,以及是否没有假定用户已停止滚动。 This solution is borrowed from this other question . 该解决方案是从另一个问题中借用的。

function scrollHandler() {
    if ($(this).scrollTop() > (HeaderH + NavbarH)) {
        Navbar.addClass('navbar-fixed-top');
        links.css('padding', '10px 20px 10px 20px');
        Header.css('margin-bottom', '64px');
        Navbar.css('top', '-64px').animate({'top': '0'}, 1000);
    }
    else {
        Navbar.removeClass('navbar-fixed-top');
        links.css('padding', '20px');
    }
}

$(window).on('scroll', function () {
    clearTimeout($.data(this, 'timer'));
    $.data(this, 'timer', setTimeout(function () {
        scrollHandler();
    }, 250));
});

This method doesn't give you the same options and potential for re-use you get with the debounce plugin - the plugin lets you choose whether to call your callback at the start or the end of your period - but it's slimmer. 这种方法不能为您提供与debounce插件相同的选项和重复使用的潜力-该插件可让您选择是在周期的开始还是结束时调用回调函数-但它更苗条。

Try something like 尝试类似

    var Header = $('#header'),
    Navbar = $('.navbar'),
    links = $(".navbar ul.nav > li > a"),
    HeaderH = Header.height(),
    NavbarH = Navbar.height();
    var scrolled = false;

$(window).scroll(function () {

 if ($(this).scrollTop() > (HeaderH + NavbarH) && !scrolled) {
    Navbar.addClass('navbar-fixed-top')
    links.css('padding', '10px 20px 10px 20px');
    Header.css('margin-bottom', '64px');
    Navbar.css('top', '-64px').animate({'top' : '0'}, 1000);
    scrolled = true;
 }
 else{
    Navbar.removeClass('navbar-fixed-top')
    links.css('padding', '20px');
    scrolled = false;
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM