jQuery有条件的标头类交换



希望有人可以帮助我,因为我真的不知道JS,并且想在WP网站上构建现有功能...

我有两个站点标头的版本。在主页上,Alt(透明背景)版本加载,滚动后,它将其交换至Site Winder Wonny-Alt(White BG)版本。

我想添加一个条件,当菜单按钮"切换"时,标头也将标题交换到非alt版本(class =" main-Navigation toggled")。

这是现有功能。谢谢你的进步。

jQuery(document).ready(function ($) {
    console.log('ready');
      var header = $(".site-header");
      if( $("body.home").length ) {
        header.addClass("site-header-alt");
      }
        
      $(window).scroll(function() {    
          var scroll = $(window).scrollTop();
          
          if( $("body.home").length ) {
            if (scroll <= 100) {
                header.addClass("site-header-alt");
            } else {
                header.removeClass("site-header-alt");
            }
          }
      });
  });

简单地放置:

$(document).ready(function () {
    // check if .main-navigation contains toggled class
    if ( $(".main-navigation").hasClass("toggled") )
    {
        header.addClass("site-header-alt");
    }
    // Also listen to the click event of the element that does the toggling.
    $(".menu-toggle").click( function () {
        if ( $(".main-navigation").hasClass("toggled") )
        {
            header.addClass("site-header-alt");
        }
    });
    // other stuffs
});

希望有帮助

最新更新