2016-05-18 12 views
0

jqueryの世界にちょっと新しくて、私は疑問に思っていましたが、私のjqueryを1つの矛盾の下に入れ子にすることができますか? ..(私は今それを持っているように)。ネストされたノンコンフリクトJquery

Wordpressを使用しているので、競合しないjqueryコードを上にする必要があります。

jQuery(document).ready(function($) { 
    //Modal contact pop-up 
    $(".modal-pop").click(function() { 
     $("#myModal").modal(); 
    }); 
    $(".email").click(function() { 
     $("#myModal").modal(); 
    }); 
}); 

//Navbar fixed top on scroll 
jQuery(document).ready(function($) { 
    $(document).ready(function() { 
     var menu = $('#site-nav'); 
     var origOffsetY = menu.offset().top; 

     function scroll() { 
      if ($(window).scrollTop() >= origOffsetY) { 
       $('#site-nav').addClass('navbar-fixed-top'); 
       $('.main-content').addClass('menu-padding'); 
      } else { 
       $('#site-nav').removeClass('navbar-fixed-top'); 
       $('.main-content').removeClass('menu-padding'); 
      } 
     } 

     document.onscroll = scroll; 
    }); 
}); 

//Image Overlay for Images in Portfolio section 
jQuery(document).ready(function($) { 
    $(function() { 
     // OPACITY OF BUTTON SET TO 0% 
     $(".roll").css("opacity", "0"); 
     // ON MOUSE OVER 
     $(".roll").hover(function() { 
       // SET OPACITY TO 70% 
       $(this).stop().animate({ 
        opacity: .7 
       }, "slow"); 
      }, 
      // ON MOUSE OUT 
      function() { 
       // SET OPACITY BACK TO 50% 
       $(this).stop().animate({ 
        opacity: 0 
       }, "slow"); 
      }); 
    }); 
}); 

//Smooth scrool to section 
jQuery(document).ready(function($) { 
    $(function() { 
     $('a[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 
        }, 1000); 
        return false; 
       } 
      } 
     }); 
    }); 
}); 
+0

あなたのページに複数のバージョンのjqueryを使用しようとしていますか? – dreamweiver

+0

いいえただ一つ..... ..... –

答えて

1

はい、あなたはおそらく、すべてを単一のready eventにラップする必要があります。それらをすべて自分のものにまとめるのは無駄です。明らかに、これは、これらをすべて1つのファイルに入れている場合にのみ発生します。 the scopeが変更されましたので注意してください!

jQuery(document).ready(function($) { 
    //Modal contact pop-up 
    $(".modal-pop").click(function() { 
     $("#myModal").modal(); 
    }); 
    $(".email").click(function() { 
     $("#myModal").modal(); 
    }); 

    //Navbar fixed top on scroll  
    function scroll() { 
     var menu = $('#site-nav'), 
      origOffsetY = menu.offset().top; 
     if ($(window).scrollTop() >= origOffsetY) { 
      menu.addClass('navbar-fixed-top'); 
      $('.main-content').addClass('menu-padding'); 
     } else { 
      menu.removeClass('navbar-fixed-top'); 
      $('.main-content').removeClass('menu-padding'); 
     } 
    } 

    document.onscroll = scroll; 

    //Image Overlay for Images in Portfolio section 
    // OPACITY OF BUTTON SET TO 0% 
    $(".roll").css("opacity", "0"); 
    // ON MOUSE OVER 
    $(".roll").hover(function() { 
      // SET OPACITY TO 70% 
      $(this).stop().animate({ 
       opacity: .7 
      }, "slow"); 
     }, 
     // ON MOUSE OUT 
     function() { 
      // SET OPACITY BACK TO 50% 
      $(this).stop().animate({ 
       opacity: 0 
      }, "slow"); 
     }); 

    //Smooth scrool to section 
    $('a[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 
       }, 1000); 
       return false; 
      } 
     } 
    }); 
}); 
+0

おかげで。ありがとう –

1

はい、すべてを1つにまとめることができます。

jQuery(document).ready(function($) { 

    //do a thing 

    //do another thing 

}); 
+0

いいですね。ありがとう –

関連する問題