简体   繁体   English

当特定类处于活动状态时更改背景颜色

[英]Change background color when specific class is active

I'm making a wordpress website, and the header menu has a background color when the page loads, but when I scroll it changes the header to sticky and changes the background color.我正在制作一个 wordpress 网站,页面加载时标题菜单具有背景颜色,但是当我滚动它时,它会将标题更改为粘性并更改背景颜色。 The problem is that the dropdown menu doesn't change color and stays the same as the initial header.问题是下拉菜单不会改变颜色并保持与初始标题相同。

What I wanted to do is when the header contains (stick) class to transform the background color of the dropdown menu.我想要做的是当标题包含 (stick) 类以转换下拉菜单的背景颜色时。

Initial menu:初始菜单:

<header class=" scroll_header_top_area dark stick scrolled_not_transparent page_header">

when it transforms:当它转换时:

<header class="scroll_header_top_area dark stick scrolled_not_transparent page_header sticky sticky_animate">

So is it possible to add javascript or jquery to order the dropdown menu to change the background color when the header has (.sticky) class?那么当标题有 (.sticky) 类时,是否可以添加 javascript 或 jquery 来订购下拉菜单以更改背景颜色?

Thanks.谢谢。

Edit: https://prnt.sc/o4xf4e https://prnt.sc/o4xf4e编辑: https : //prnt.sc/o4xf4e https://prnt.sc/o4xf4e

These are screenshots for better comprehension of the problem, sorry for not expressing in the best way.这些是更好地理解问题的屏幕截图,抱歉没有以最好的方式表达。

I had this css to change only the dropdown:我有这个 css 只更改下拉列表:

.drop_down .second .inner ul, .drop_down .second .inner ul li ul, .shopping_cart_dropdown, li.narrow .second .inner ul, .header_top .right #lang_sel ul ul, .drop_down .wide .second ul li.show_widget_area_in_popup .widget, .drop_down .wide.wide_background .second{
    background-color: #ffffff!important;
}

I added .scroll_header_top_area.sticky behind but nothing happened.我在后面添加了 .scroll_header_top_area.sticky 但什么也没发生。 Doing what you told me to do will only change the menu background and not the dropdown.做你告诉我做的只会改变菜单背景而不是下拉菜单。

Just use CSS只需使用 CSS

.scroll_header_top_area.sticky {
  background-color: <whateveryouwant>;
}

Although css would be the preferred approach.尽管 css 将是首选方法。 This is the solution to your question using jquery.这是使用 jquery 解决您的问题。

$(".stick").css("background-color", "#000");

'#000 represents the color black in hex. '#000 表示十六进制的黑色。

I overcomplicated, it was as you all said:我太复杂了,正如你们所说:

.drop_down .second .inner ul{
    background-color: rgba(0,0,0,0.3)!important;
}

header.sticky .drop_down .second .inner ul{
    background-color: #ffffff!important;
}

Thank you.谢谢你。

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

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