简体   繁体   English

使用Bootstrap3更改滚动Jquery上导航栏的高度

[英]Change Height of Navbar on Scroll Jquery with Bootstrap3

Hello i have a problem with my navbar. 您好,我的导航栏有问题。 I want to animate it on scroll and change his height. 我想在滚动上为其设置动画并更改他的身高。 When i scroll a bit down it should animate smaller and when im at the top of the page it should aniamte bigger. 当我向下滚动时,它应该设置较小的动画,而当我在页面顶部时,应该设置较大的动画。 The standard height is 100px. 标准高度为100像素。 The problem is when im at the top of the page it takes a delay, which i need to wait, until it animates. 问题是当im在页面顶部时,我需要等待一段时间,直到它动画为止。 They delays gets longer if i scroll first to the bottom of the page and then back to the top. 如果我先滚动到页面底部,然后再滚动到顶部,则延迟会变得更长。 The has a height of 11000px. 的高度为11000px。 This is my code for it: 这是我的代码:

 $(document).on("scroll",function(){
        if($(document).scrollTop()>500)
        {
            $( ".navbar" ).animate({height: 50} ,{duration:100});
        }
        else if($(document).scrollTop()==0)
        {
            alert("dhsihsp");
            $( ".navbar" ).animate({height: 100} ,{duration:100});
        }
    });

Maybe u can help me. 也许你可以帮助我。 I use Google Chrome and Bootstrap 3. 我使用Google Chrome和Bootstrap 3。

The problem you are having is that the "scroll" fires every single time the scrollbar moves. 您遇到的问题是滚动条每次移动都会触发“滚动条”。 So every single time the scrollbar moves a pixel, it will do the IF checks. 因此,滚动条每移动一个像素一次,它将进行IF检查。 That's why you delay your animation for so long. 这就是为什么您将动画延迟这么长时间的原因。 The queue of things to run stacks up immensely if you move the scrollbar too much. 如果您将滚动条移动得太多,那么将要运行的事物队列就会大量堆积。

DEMO DEMO

The scroll event seems to fire a lot when you scroll so all the events get queued. 当您滚动时,scroll事件似乎触发很多,因此所有事件都排队。 So the event that actually changes you header seems to take a long time to appear. 因此,实际上更改标题的事件似乎需要很长时间才能出现。

I added a css transition on the height of a .navbar . 我在.navbar的高度上添加了CSS过渡。 for making this happen almost instantly. 使这几乎立即发生。 Are the events not still there? 事件还不在那里吗? True , but changing css is a lot less demanding then adding animations (with a duration of 100ms). 的确如此 ,但是更改css的要求比添加动画(持续时间为100ms)要少得多。 The transition does have a duration but it does not have to finish so an other event can come in at any time. 过渡确实有一定的持续时间,但不一定要结束,因此可以随时引入其他事件。

CSS CSS

.navbar {
    transition: height 0.1s;
}

Jquery jQuery的

$(window).scroll(function () {
    var scrollh = $(this).scrollTop();
    if (scrollh == 0) {
        $(".navbar").css({
            'height':'100px',
        });
    } else {
        $(".navbar").css({
            'height':'50px',
        });
    }
});

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

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