Java Script to change Nav Bar text to image on scroll









up vote
0
down vote

favorite












Hi I have the following code for a website which currently displays the text "Prince Resourcing" in the Nav Bar. When the user scrolls, the colours of the Nav Bar change, but what I really want is to replace the "Prince Resourcing" text with a thumbnail image I have created. I can't seem to get it to work, can anyone help? Thanks.



creative.js code



 (function($) {
"use strict"; // Start of use strict

// Smooth scrolling using jQuery easing
$('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function()
if (location.pathname.replace(/^//, '') == this.pathname.replace(/^//, '') && location.hostname == this.hostname)
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length)
$('html, body').animate(
scrollTop: (target.offset().top - 56)
, 1000, "easeInOutExpo");
return false;


);

// Closes responsive menu when a scroll trigger link is clicked
$('.js-scroll-trigger').click(function()
$('.navbar-collapse').collapse('hide');
);

// Activate scrollspy to add active class to navbar items on scroll
$('body').scrollspy(
target: '#mainNav',
offset: 57
);

// Collapse Navbar
var navbarCollapse = function()
if ($("#mainNav").offset().top > 100)
$("#mainNav").addClass("navbar-shrink");

else
$("#mainNav").removeClass("navbar-shrink");

;
// Collapse now if page is not at top
navbarCollapse();
// Collapse the navbar when page is scrolled
$(window).scroll(navbarCollapse);


index.html code



<body id="page-top">

<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand js-scroll-trigger" href="#page-top" >Prince Resourcing</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" >
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#portfolio">Current Roles</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="index.html#blog">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="gdpr.html">GDPR</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="team.html">The Team</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>









share|improve this question

























    up vote
    0
    down vote

    favorite












    Hi I have the following code for a website which currently displays the text "Prince Resourcing" in the Nav Bar. When the user scrolls, the colours of the Nav Bar change, but what I really want is to replace the "Prince Resourcing" text with a thumbnail image I have created. I can't seem to get it to work, can anyone help? Thanks.



    creative.js code



     (function($) {
    "use strict"; // Start of use strict

    // Smooth scrolling using jQuery easing
    $('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function()
    if (location.pathname.replace(/^//, '') == this.pathname.replace(/^//, '') && location.hostname == this.hostname)
    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    if (target.length)
    $('html, body').animate(
    scrollTop: (target.offset().top - 56)
    , 1000, "easeInOutExpo");
    return false;


    );

    // Closes responsive menu when a scroll trigger link is clicked
    $('.js-scroll-trigger').click(function()
    $('.navbar-collapse').collapse('hide');
    );

    // Activate scrollspy to add active class to navbar items on scroll
    $('body').scrollspy(
    target: '#mainNav',
    offset: 57
    );

    // Collapse Navbar
    var navbarCollapse = function()
    if ($("#mainNav").offset().top > 100)
    $("#mainNav").addClass("navbar-shrink");

    else
    $("#mainNav").removeClass("navbar-shrink");

    ;
    // Collapse now if page is not at top
    navbarCollapse();
    // Collapse the navbar when page is scrolled
    $(window).scroll(navbarCollapse);


    index.html code



    <body id="page-top">

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
    <a class="navbar-brand js-scroll-trigger" href="#page-top" >Prince Resourcing</a>
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" >
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
    <ul class="navbar-nav ml-auto">
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="#about">About</a>
    </li>
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="#portfolio">Current Roles</a>
    </li>
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="index.html#blog">Blog</a>
    </li>
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="gdpr.html">GDPR</a>
    </li>
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="team.html">The Team</a>
    </li>
    <li class="nav-item">
    <a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
    </li>
    </ul>
    </div>
    </div>
    </nav>









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Hi I have the following code for a website which currently displays the text "Prince Resourcing" in the Nav Bar. When the user scrolls, the colours of the Nav Bar change, but what I really want is to replace the "Prince Resourcing" text with a thumbnail image I have created. I can't seem to get it to work, can anyone help? Thanks.



      creative.js code



       (function($) {
      "use strict"; // Start of use strict

      // Smooth scrolling using jQuery easing
      $('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function()
      if (location.pathname.replace(/^//, '') == this.pathname.replace(/^//, '') && location.hostname == this.hostname)
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length)
      $('html, body').animate(
      scrollTop: (target.offset().top - 56)
      , 1000, "easeInOutExpo");
      return false;


      );

      // Closes responsive menu when a scroll trigger link is clicked
      $('.js-scroll-trigger').click(function()
      $('.navbar-collapse').collapse('hide');
      );

      // Activate scrollspy to add active class to navbar items on scroll
      $('body').scrollspy(
      target: '#mainNav',
      offset: 57
      );

      // Collapse Navbar
      var navbarCollapse = function()
      if ($("#mainNav").offset().top > 100)
      $("#mainNav").addClass("navbar-shrink");

      else
      $("#mainNav").removeClass("navbar-shrink");

      ;
      // Collapse now if page is not at top
      navbarCollapse();
      // Collapse the navbar when page is scrolled
      $(window).scroll(navbarCollapse);


      index.html code



      <body id="page-top">

      <!-- Navigation -->
      <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
      <div class="container">
      <a class="navbar-brand js-scroll-trigger" href="#page-top" >Prince Resourcing</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" >
      <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#about">About</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#portfolio">Current Roles</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="index.html#blog">Blog</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="gdpr.html">GDPR</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="team.html">The Team</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
      </li>
      </ul>
      </div>
      </div>
      </nav>









      share|improve this question













      Hi I have the following code for a website which currently displays the text "Prince Resourcing" in the Nav Bar. When the user scrolls, the colours of the Nav Bar change, but what I really want is to replace the "Prince Resourcing" text with a thumbnail image I have created. I can't seem to get it to work, can anyone help? Thanks.



      creative.js code



       (function($) {
      "use strict"; // Start of use strict

      // Smooth scrolling using jQuery easing
      $('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function()
      if (location.pathname.replace(/^//, '') == this.pathname.replace(/^//, '') && location.hostname == this.hostname)
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length)
      $('html, body').animate(
      scrollTop: (target.offset().top - 56)
      , 1000, "easeInOutExpo");
      return false;


      );

      // Closes responsive menu when a scroll trigger link is clicked
      $('.js-scroll-trigger').click(function()
      $('.navbar-collapse').collapse('hide');
      );

      // Activate scrollspy to add active class to navbar items on scroll
      $('body').scrollspy(
      target: '#mainNav',
      offset: 57
      );

      // Collapse Navbar
      var navbarCollapse = function()
      if ($("#mainNav").offset().top > 100)
      $("#mainNav").addClass("navbar-shrink");

      else
      $("#mainNav").removeClass("navbar-shrink");

      ;
      // Collapse now if page is not at top
      navbarCollapse();
      // Collapse the navbar when page is scrolled
      $(window).scroll(navbarCollapse);


      index.html code



      <body id="page-top">

      <!-- Navigation -->
      <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
      <div class="container">
      <a class="navbar-brand js-scroll-trigger" href="#page-top" >Prince Resourcing</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" >
      <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#about">About</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#portfolio">Current Roles</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="index.html#blog">Blog</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="gdpr.html">GDPR</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="team.html">The Team</a>
      </li>
      <li class="nav-item">
      <a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
      </li>
      </ul>
      </div>
      </div>
      </nav>






      javascript html html5 frontend






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 10:17









      SDROB

      408




      408






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can inline:



          var navbarCollapse = function() 
          if ($("#mainNav").offset().top > 100)
          $("#mainNav").addClass("navbar-shrink");
          $(".navbar-brand").html('<img src="/relative/path/to/image.png" />');
          else
          $("#mainNav").removeClass("navbar-shrink");
          $(".navbar-brand").html('Prince Resourcing');

          ;


          It would be preferential to use jQuery's addCLass however" $(".navbar-brand").addClass("YourImageClass");






          share|improve this answer




















          • This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
            – SDROB
            Nov 12 at 11:08











          • This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
            – Jamie_D
            Nov 12 at 11:14










          • Sorry scratch that, works good - thanks!
            – SDROB
            Nov 12 at 11:27










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237953%2fjava-script-to-change-nav-bar-text-to-image-on-scroll%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can inline:



          var navbarCollapse = function() 
          if ($("#mainNav").offset().top > 100)
          $("#mainNav").addClass("navbar-shrink");
          $(".navbar-brand").html('<img src="/relative/path/to/image.png" />');
          else
          $("#mainNav").removeClass("navbar-shrink");
          $(".navbar-brand").html('Prince Resourcing');

          ;


          It would be preferential to use jQuery's addCLass however" $(".navbar-brand").addClass("YourImageClass");






          share|improve this answer




















          • This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
            – SDROB
            Nov 12 at 11:08











          • This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
            – Jamie_D
            Nov 12 at 11:14










          • Sorry scratch that, works good - thanks!
            – SDROB
            Nov 12 at 11:27














          up vote
          0
          down vote



          accepted










          You can inline:



          var navbarCollapse = function() 
          if ($("#mainNav").offset().top > 100)
          $("#mainNav").addClass("navbar-shrink");
          $(".navbar-brand").html('<img src="/relative/path/to/image.png" />');
          else
          $("#mainNav").removeClass("navbar-shrink");
          $(".navbar-brand").html('Prince Resourcing');

          ;


          It would be preferential to use jQuery's addCLass however" $(".navbar-brand").addClass("YourImageClass");






          share|improve this answer




















          • This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
            – SDROB
            Nov 12 at 11:08











          • This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
            – Jamie_D
            Nov 12 at 11:14










          • Sorry scratch that, works good - thanks!
            – SDROB
            Nov 12 at 11:27












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can inline:



          var navbarCollapse = function() 
          if ($("#mainNav").offset().top > 100)
          $("#mainNav").addClass("navbar-shrink");
          $(".navbar-brand").html('<img src="/relative/path/to/image.png" />');
          else
          $("#mainNav").removeClass("navbar-shrink");
          $(".navbar-brand").html('Prince Resourcing');

          ;


          It would be preferential to use jQuery's addCLass however" $(".navbar-brand").addClass("YourImageClass");






          share|improve this answer












          You can inline:



          var navbarCollapse = function() 
          if ($("#mainNav").offset().top > 100)
          $("#mainNav").addClass("navbar-shrink");
          $(".navbar-brand").html('<img src="/relative/path/to/image.png" />');
          else
          $("#mainNav").removeClass("navbar-shrink");
          $(".navbar-brand").html('Prince Resourcing');

          ;


          It would be preferential to use jQuery's addCLass however" $(".navbar-brand").addClass("YourImageClass");







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:21









          Jamie_D

          39428




          39428











          • This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
            – SDROB
            Nov 12 at 11:08











          • This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
            – Jamie_D
            Nov 12 at 11:14










          • Sorry scratch that, works good - thanks!
            – SDROB
            Nov 12 at 11:27
















          • This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
            – SDROB
            Nov 12 at 11:08











          • This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
            – Jamie_D
            Nov 12 at 11:14










          • Sorry scratch that, works good - thanks!
            – SDROB
            Nov 12 at 11:27















          This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
          – SDROB
          Nov 12 at 11:08





          This solution doesn't seem to work.. even if I replace the line containing the image source with ` $(".navbar-brand").html('TEST');` the output does not seem to change. Any ideas?
          – SDROB
          Nov 12 at 11:08













          This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
          – Jamie_D
          Nov 12 at 11:14




          This solution depends on what the navbar-shrink class is up to. Please post a snippet of the CSS that contains both navbar-shrink and navbar-brand
          – Jamie_D
          Nov 12 at 11:14












          Sorry scratch that, works good - thanks!
          – SDROB
          Nov 12 at 11:27




          Sorry scratch that, works good - thanks!
          – SDROB
          Nov 12 at 11:27

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237953%2fjava-script-to-change-nav-bar-text-to-image-on-scroll%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20