简体   繁体   中英

How to make onmouseover on menu link change background image?

I want to change image »main.png« into image »background.png « when my cursor is on link 1 as you can see on the links below. And I have 3 images more, for each link of the menu (link 2, link 3 and link 4) one open drawer.

main

changed background

I've got working menu code already, which opens submenus with sublinks, when the cursor is on link 1 or link 2 or link 3 or link 4. This submenus stay open until timer runs off. So I want to integrate the new code into the old one, so the new background 1 stays there as long as the submenu 1 is open and the new background 2 stays there as long as submenu 2 is open and so on with other two submenus. I have searched for the solution all over the Internet, but I couldn't find anything. There are a lot of similar stuff as this, but not just like it, and I'm just not good enough to know how to integrate it. I tried a few, but none worked.

Here is my working menu code:

CSS

body
{
background-image:url(Slike/Ozadja/main.png);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:center;
background-size: cover;
background-size:contain;}


#ddm
{   margin: 0;
    padding: 0;
    z-index: 30}

#ddm li
{   margin: 0; 
    padding: 0;
    list-style: none;
    float: left;
    font: bold 14px arial}

#ddm li a
{   display: block;
    margin: 0 1px 0 0;
    padding: 4px 10px;
    width: 120px;
    background: green;
    color: #FFF;
    text-align: center;
    text-decoration: none;}

#ddm li a:hover
{   background: transparent;
    color: #392865}

#ddm div
{   position: absolute;
    visibility: hidden;
    margin: 0;
    padding: 30px;
    background: transparent}

    #ddm div a
    {   position: static;
        display: block;
        margin: 0;
        padding: 5px 10px;
        width: auto;
        white-space: nowrap;
        text-align: center;
        text-decoration: none;
        background: yellow;
        color: #000;
        font: 12px arial}

    #ddm div a:hover
    {   background: transparent;
        color: #392865}

HTML

<html>
<head>
<title>Drop-Down Menu</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta http-equiv="Content-Type"
    content="text/html;charset=UTF-16">
    <link rel="stylesheet" type="text/css" href="default.css">

<script type="text/javascript">

var timeout         = 500;
var closetimer      = 0;
var ddmenuitem      = 0;

function mopen(id)
{   
    mcancelclosetime();

    if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';    
}

function mclose()
{
    if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

function mclosetime()
{
    closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
    if(closetimer)
    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = mclose;     

</script>

</head>
<body>

<ul id="ddm">
    <li><a class="prvi" href="#" onmouseover="mopen('m1')" onmouseout="mclosetime()">link1</a>
        <div id="m1" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">
        <a href="#">sublink11</a>
        <a href="#">sublink12</a>
        </div>
    </li>
    <li><a class="drugi" href="#" onmouseover="mopen('m2')" onmouseout="mclosetime()">link2</a>
        <div id="m2" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">
        <a href="#">sublink21</a>
        <a href="#">sublink22</a>
        </div>
    </li>
    <li><a class="tretji" href="#" onmouseover="mopen('m3')" onmouseout="mclosetime()">link3</a>
        <div id="m3" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">
        <a href="#">sublink31</a>
        <a href="#">sublink32</a>
        </div>
    </li>
    <li><a class="cetrti" href="#" onmouseover="mopen('m4')" onmouseout="mclosetime()">link4</a>
        <div id="m4" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">
        <a href="#">sublink41</a>
        <a href="#">sublink42</a>
        </div>
    </li>
</ul>
<div style="clear:both"></div>

<div style="clear:both"></div>

</body>
</html>

UPDATED

If I understand what do you want, this is the solution:

var myImages = {};
myImage['m1'] = 'image1.png';
myImage['m2'] = 'image2.png';
myImage['m3'] = 'image3.png';
myImage['m4'] = 'image4.png';

function mopen(id)
{   
    mcancelclosetime();

    if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';    
    document.body.style.backgroundImage = 'url(Slike/Ozadja/' + myImage[id] + ')';
}

function mclose()
{
    if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    document.body.style.backgroundImage = 'url(Slike/Ozadja/main.png)';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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