最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bootstrap Navbar-Brand change after scroll - Stack Overflow

programmeradmin0浏览0评论

I am just newbie at Bootstrap and Web Developing, so my question could be really simple and funny for experts. How can I change Navbar-Brand Image in Bootstrap after scrolling? I have this perfect working JS:

    $(window).scroll(function() {
    if ($(".navbar").offset().top > 50) {
        $('#custom-nav').addClass('affix');
        $(".navbar-fixed-top").addClass("top-nav-collapse");
    } else {
        $('#custom-nav').removeClass('affix');
        $(".navbar-fixed-top").removeClass("top-nav-collapse");
    }   
});

But I can't understand how my brand image can change after scrolling? Ty!

I am just newbie at Bootstrap and Web Developing, so my question could be really simple and funny for experts. How can I change Navbar-Brand Image in Bootstrap after scrolling? I have this perfect working JS:

    $(window).scroll(function() {
    if ($(".navbar").offset().top > 50) {
        $('#custom-nav').addClass('affix');
        $(".navbar-fixed-top").addClass("top-nav-collapse");
    } else {
        $('#custom-nav').removeClass('affix');
        $(".navbar-fixed-top").removeClass("top-nav-collapse");
    }   
});

But I can't understand how my brand image can change after scrolling? Ty!

Share Improve this question asked May 4, 2016 at 9:46 MorgariMorgari 5552 gold badges9 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Simple, Just change the src of the image based on the scrolling condition

$(window).scroll(function() {
    if ($(".navbar").offset().top > 50) {
        $('#custom-nav').addClass('affix');
        $(".navbar-fixed-top").addClass("top-nav-collapse");
        $('.navbar-brand img').attr('src','newImage.jpg'); //change src
    } else {
        $('#custom-nav').removeClass('affix');
        $(".navbar-fixed-top").removeClass("top-nav-collapse");
        $('.navbar-brand img').attr('src','OldImage.jpg')
    }   
});

.nav-brand here is by default class name given to anchor tag which contains img tag inside it. $('.navbar-brand img') gets the img tag which is inside .nav-brand element.

发布评论

评论列表(0)

  1. 暂无评论