简体   繁体   English

将鼠标悬停在子菜单项上后更改背景图像

[英]Change background-image after hover on submenu item

Hello I got question regarding setting a background-image whenever I hover on an element of my submmenu. 您好,当我将鼠标悬停在submmenu的某个元素上时,我会遇到有关设置背景图像的问题。 I have the following HTML: 我有以下HTML:

<div class="navbar navbar-default main-nav">
    <div class="container">
        <div class="navbar-header" tabindex="-1">
            <button type="button" class="navbar-toggle collapsed">
            </button>
        </div>
        <div class="collapse navbar-collapse" tabindex="-1">
            <div class="nav navbar-nav" tabindex="-1">
                <div class="menuitem_wrapper" tabindex="-1">
                    <a class="home_button not_active" href="#"></a>
                    <div class="dropdown-menu" tabindex="-1">
                        <a href="#">My data</a>
                    </div>
                </div>
                <div class="menuitem_wrapper" tabindex="-1">
                    <a href="#">Menu 1</a>
                    <div class="dropdown-menu" tabindex="-1">
                        <a href="#">Submenu1</a>
                        <a href="#">Submenu2</a>
                    </div>
                </div>
                <div class="menuitem_wrapper" tabindex="-1">
                    <a class="active" href="#">Menu 2</a>
                    <div class="dropdown-menu" tabindex="-1">
                        <a href="#">submenu1</a>
                        <a href="#">submenu2</a>
                    </div>
                </div>
                <a href="#">FAQ</a>
                <a href="#">Contact</a>
                <a href="#">Logout</a></div>
        </div>
    </div>
</div>

I know this is not the proper way to generate a menu with submenu items but despite this, how could I get my grey home button (which becomes visible after hovering on the home button) to stay visible whenever I hover on the " my data" button? 我知道这不是生成带有子菜单项的菜单的正确方法,但是尽管如此,每当我将鼠标悬停在“ 我的数据”上时,如何使我的灰色主页按钮(在悬停在主页按钮上之后变为可见)仍然可见按钮? Is this possible with pure CSS? 纯CSS可以做到吗?

I have a demo here: JSFIDDLE 我在这里有一个演示: JSFIDDLE

Change this: 更改此:

.not_active:hover {
    background-image: url(https://jira.transvar.org/secure/attachmentzip/unzip/14790/12882%5B1%5D/Home_Icon_for_IGB_pack/home_icon_grey_128x128_more_space_16x16.pn);
}

to this: 对此:

    .not_active:hover, .menuitem_wrapper:hover .not_active {
        background-image: url(https://jira.transvar.org/secure/attachmentzip/unzip/14790/12882%5B1%5D/Home_Icon_for_IGB_pack/home_icon_grey_128x128_more_space_16x16.pn);
}

I just added .menuitem_wrapper:hover .not_active to the same rule. 我只是将.menuitem_wrapper:hover .not_active添加到同一规则。

Use jQuery mouseover() and mouseout() 使用jQuery mouseover()mouseout()

$(document).ready(function(){
     $(".dropdown-menu a").mouseover(function(){
         $("a.home_button.not_active").css("background-image", "url('https://jira.transvar.org/secure/attachmentzip/unzip/14790/12882%5B1%5D/Home_Icon_for_IGB_pack/home_icon_grey_128x128_more_space_16x16.png')");
     });


$( ".dropdown-menu a" ).mouseout(function() {
$("a.home_button.not_active").css("background-image", "url('https://upload.wikimedia.org/wikipedia/commons/3/34/Home-icon.svg')");
     });
});

Also make changes to CSS 同时对CSS进行更改

.not_active:hover {
    background-image: url(https://jira.transvar.org/secure/attachmentzip/unzip/14790/12882%5B1%5D/Home_Icon_for_IGB_pack/home_icon_grey_128x128_more_space_16x16.png)! important;
}

Working Example JSFIDDLE 工作示例JSFIDDLE

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

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