简体   繁体   English

如何在jQuery中使用多个功能?

[英]How do I use multiple functions in jQuery?

I've added jQuery function that shows and hides navbar when scrolled. 我添加了jQuery函数,该函数在滚动时显示和隐藏导航栏。 I also want to hide a div when the button 'start' is clicked. 我也想在单击“开始”按钮时隐藏div。 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>

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));

Edit : Note that you have an extra closing bracket in the first function $('.lead').hide()); 编辑 :请注意,您在第一个函数$('.lead').hide());有一个额外的右括号$('.lead').hide()); .

You can use the following. 您可以使用以下内容。 Both functions can be combined in to a single block. 两种功能都可以组合成一个块。 And you can use $(document).ready() directly. 您可以直接使用$(document).ready() If there is a problem with name conflict for $ use jQuery.noConflict(); 如果$名称冲突,请使用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>

At least, closing bracket is wrong here. 至少,这里的右括号是错误的。 $('.lead').hide()); $('。lead')。hide()); Tip: always check developer console first. 提示:请务必先检查开发者控制台。 Have fun coding. 玩得开心。

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

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