0

I've made this header: https://www.youtube.com/watch?v=LOWjWRy_Kks and at the place of the logo, I've inserted a picture, which is black.

When I scroll down, I want this image to be white (since the header turns black), so I've made a white version of the same image.

However I have no idea where to insert the image. I've been thinking about inserting it in the javascript code, which is the following:

    $(window).on('scroll', function() {
    if($(window).scrollTop()) {
      $('nav').addClass('black');
    }
    else {
      $('nav').removeClass('black');
    }
  })

How can I swap in this white image using javascript?

3
  • In video, it is working fine. What is the issue here? Commented May 19, 2018 at 12:21
  • For a question to be well received on StackOverflow, you need to provide the mimimum complete code to see / recreate the situation. A video on YouTube to illustrate what you want is less than desirable. Commented May 19, 2018 at 13:15
  • Possible duplicate of Change image src using javascript Commented May 19, 2018 at 13:18

1 Answer 1

0

I have not executed this code but I saw the video and with that reference suggesting following code to satisfy your requirement.

<script type="text/javascript">
    $("#logoimageid").attr('src', 'blacklogo.jpg');//this will not required if default 'blacklogo.jpg' src alerady set in img tag 
     $(window).on('scroll', function() {
    if($(window).scrollTop()) {
      $("#logoimageid").attr('src', 'whitelogo.jpg');
    }
    else {
      $("#logoimageid").attr('src', 'blacklogo.jpg');
    }
  })
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Hey! Thank you for your comment. I've tried it and it didn't work unfortunately. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.