简体   繁体   English

jQuery对滚动标题的影响

[英]jQuery on scroll header effect

Hi guys 嗨,大家好

I would like to make a jquery on scroll header effect . 我想在滚动标题效果上做一个jQuery I've just added some changes in to the code but it's not working they way I want. 我刚刚在代码中添加了一些更改,但是它们无法按我想要的方式工作。 I want to make the effect - when I scroll down a bit pixel for the header to change the height to small. 我想产生效果-当我向下滚动标题的一点像素以将高度更改为较小时。 Or show the hidden header with other styles 或以其他样式显示隐藏的标题

So when scrolling the normal header to slide up and then show the hidden header like slide down... I've spent much time on this but it's only working with fadeIn and fadeOut 因此,当滚动普通标题以向上滑动然后将隐藏的标题像向下滑动一样显示时...我已经花了很多时间,但是只能与fadeInfadeOut一起使用

 jQuery code

 $(function() {
    $('.header-small').hide();
    var header = $(".header-small");
    $(window).scroll(function() {    
        var scroll = $(window).scrollTop();

        if (scroll >= 200) {
            $('.header-small').show();


            header.removeClass('header').addClass("header-small");
            $('.header-small').addClass('animated fadeInUp'), {duration:500};


        } else {
            $('header').show();
            $('.header-small').hide();
            header.removeClass("header-small").addClass('header');
            $('header').addClass('animated fadeInDown');
        }
    });
});

Working demo here 在这里 工作演示

Please could somebody help me with this? 请有人可以帮我吗?

Thanks 谢谢

You may try using callbacks, 您可以尝试使用回调,

$(function() {
        $('.header-small').hide();
        var bar = $('header');
        var top = bar.css('top');
        $(window).scroll(function() {
            if($(this).scrollTop() > 50) {
                bar.stop().animate({'top' : '0px'}, 200);
                $('.header-small').slideDown('slow',function(){

                    $('header').slideUp('slow');});
            } else {
                bar.stop().animate({'top' : '20px'}, 200);
                    $('header').slideDown('slow');
                    $('.header-small').slideUp('slow');
            }
        });
    });

And also some styling for cleaner effect 还有一些造型以获得更清洁的效果

header
{
    position:absolute;
    width:400px;
    height:200px;
}

Jsfiddle updated example jsfiddle更新示例

Why not just addClass and do the rest in CSS? 为什么不只是添加类,其余的都在CSS中呢?

$(window).scroll(function() {
  if($(window).scrollTop() > 50) {
    $('header').addClass('header-small');
  }
  else {
    $('header').removeClass('header-small');
  }
});

jsFiddle DEMO jsFiddle演示

$(function () {
    $('.header-small').hide();
    var bar = $('header');
    var top = bar.css('top');
    $(window).scroll(function () {
        if ($(this).scrollTop() > 50) {
            bar.stop().animate({
                'top': '0px'
            }, 200);
            $('header').slideUp('fast', function () {
                $('.header-small').slideDown('slow');
            });
        } else {
            bar.stop().animate({
                'top': '20px'
            }, 200);
            $('.header-small').slideUp('fast', function () {
                $('header').slideDown('fast');
            });
        }
    });
});

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

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