0

I've added jQuery function that shows and hides navbar when scrolled. I also want to hide a div when the button 'start' is clicked. After i add new function none of them works longer. I have tried to add it to various places, but I still get no result at all. So how do I add multiple functions and make them to work?

Heres my code:

<script>
    // function that hides text on click 
    (function($) {
            $('#start').click(function() {
                    $('.lead').hide());
            });
    });
    // my original function that stopped working after i added new one
    (function($) {
        $(document).ready(function() {
            $(".masthead").css("background-color", "inherit");
            // fade in .navbar
            $(function() {
                $(window).scroll(function() {
                    // set distance user needs to scroll before we fadeIn navbar
                    if($(this).scrollTop() > 600) {
                        $('.masthead').fadeIn();
                        $(".masthead").css("background-color", "black");
                    } else if($(this).scrollTop() === 0) {
                        $('.masthead').fadeIn();
                        $(".masthead").css("background-color", "inherit");
                    } else {
                        $('.masthead').fadeOut();
                    }
                });
            });
        });
    }(jQuery));
</script>
5
  • I think this post will be useful for you. Have a look at the link Commented Aug 10, 2015 at 8:44
  • $('.lead').hide()); one extra closing bracket is there. Commented Aug 10, 2015 at 8:46
  • Your first function won't run, because you don't call it. Commented Aug 10, 2015 at 8:46
  • pastebin.com/DiepDQB3 full code here Commented Aug 10, 2015 at 8:47
  • Both functions won't run when i added first one. Commented Aug 10, 2015 at 8:48

3 Answers 3

0

I think the problem is in the closing tag of the function you're adding.

Try like this:

(function ($){
    $('#start').click(function(){
    $('.lead').hide();
  });
}(jQuery));
// my original function that stopped working after i added new one
(function ($) {

  $(document).ready(function(){

  $(".masthead").css("background-color","inherit");
  // fade in .navbar
  $(function () {
    $(window).scroll(function () {
            // set distance user needs to scroll before we fadeIn navbar
      if ($(this).scrollTop() > 600) {
        $('.masthead').fadeIn();
        $(".masthead").css("background-color","black");
      } else if($(this).scrollTop() === 0){
                $('.masthead').fadeIn();
                  $(".masthead").css("background-color","inherit");

      } else {
        $('.masthead').fadeOut();
      }
    });
  });
}); 
  }(jQuery));
Sign up to request clarification or add additional context in comments.

Comments

0

At least, closing bracket is wrong here. $('.lead').hide()); Tip: always check developer console first. Have fun coding.

Comments

0

Edit : Note that you have an extra closing bracket in the first function $('.lead').hide());.

You can use the following. Both functions can be combined in to a single block. And you can use $(document).ready() directly. If there is a problem with name conflict for $ use jQuery.noConflict();

<script>
    $(document).ready(function() {
        $(".masthead").css("background-color", "inherit");
        // fade in .navbar
        $('#start').click(function() {
            $('.lead').hide();
        });
        $(window).scroll(function() {
            // set distance user needs to scroll before we fadeIn navbar
            if ($(this).scrollTop() > 600) {
                $('.masthead').fadeIn();
                $(".masthead").css("background-color", "black");
            } else if ($(this).scrollTop() === 0) {
                $('.masthead').fadeIn();
                $(".masthead").css("background-color", "inherit");
            } else {
                $('.masthead').fadeOut();
            }
        });
    });
</script>

Comments

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.