简体   繁体   English

在滚动条上更改汉堡菜单的颜色

[英]Changing color of hamburger menu on scroll

I have built a site on selfhosted WordPress (with Divi).我在自托管的 WordPress(使用 Divi)上建立了一个站点。 The site uses two menu logos, one for primary menu bar and the second one for the fixed header.该站点使用两个菜单标志,一个用于主菜单栏,第二个用于固定 header。 The logo changes on scroll with this code:使用以下代码滚动时徽标会发生变化:

<script>
    
jQuery(window).scroll(function () {
    if (jQuery(window).scrollTop() > 50) {
        jQuery('#logo').attr('src','link_to_fixed_header_logo')
    } else {
         jQuery('#logo').attr('src','link_to_primary_top_menu_logo')
    }
});

</script>

I would like the site to change color of the hamgurger button on scroll in the mobile version.我希望该网站在移动版本中更改滚动时汉堡包按钮的颜色。 The inspect tool shows that the color is defined by these lines:检查工具显示颜色由以下几行定义:

body.et_divi_100_custom_hamburger_menu .et_divi_100_custom_hamburger_menu__icon div {
    background: #000!important;

I use this CSS to make the hamburger button black.我用这个 CSS 使汉堡按钮变黑。 Is it possible to change the CSS through the JQuery code, so that the button is white on top, and turns black on scroll?是否可以通过 JQuery 代码更改 CSS,使按钮顶部为白色,滚动时变为黑色?

Sure, you can modify your Javascript to also set the color of the hamburger menu using your existing scroll handler.当然,您可以修改 Javascript 以使用现有的滚动处理程序设置汉堡菜单的颜色。 You might be able to make the selector at the top there a bit nicer, but I don't know the structure of your page.您也许可以使顶部的选择器更好一些,但我不知道您页面的结构。

jQuery(window).scroll(function () {
    var hamburgerImage = jQuery('body.et_divi_100_custom_hamburger_menu .et_divi_100_custom_hamburger_menu__icon div');
    if (jQuery(window).scrollTop() > 50) {
        jQuery('#logo').attr('src','link_to_fixed_header_logo');
        hamburgerImage.css('background-color', '#FFF');
    } else {
        jQuery('#logo').attr('src','link_to_primary_top_menu_logo');
        hamburgerImage.css('background-color', '#000');
    }
});

This can be accomplished just with CSS, no need for JS.只需使用 CSS 即可完成,无需 JS。

DIVI, on scrolling, adds automatically a class to the header element, the class is .et-fixed-header . DIVI 在滚动时会自动将 class 添加到header元素中, class 是.et-fixed-header

However, to enable this you should enable the option "Fixed Navigation Bar" from Backend->DIVI->Theme Options但是,要启用此功能,您应该从Backend->DIVI->Theme Options启用选项“Fixed Navigation Bar”

Use this to change the color: body.et_divi_100_custom_hamburger_menu header.et-fixed-header.et_divi_100_custom_hamburger_menu__icon div{}使用它来更改颜色: body.et_divi_100_custom_hamburger_menu header.et-fixed-header.et_divi_100_custom_hamburger_menu__icon div{}

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

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