如何在菜单链接更改背景图像上进行onmouseover?

时间:2013-09-05 13:02:15

标签: javascript html css drop-down-menu background

当我的光标在链接1上时,我想将图像»main.png«更改为图像»background.png«,如下面的链接所示。对于菜单的每个链接(链接2,链接3和链接4),我有3个图像,一个打开的抽屉。

main

changed background

我已经有了工作菜单代码,当光标位于链接1或链接2或链接3或链接4上时,它会打开带有子链接的子菜单。此子菜单保持打开状态直到计时器运行。因此,我想将新代码集成到旧代码中,因此只要子菜单1打开并且新背景2保持在那里,只要子菜单2打开,等待其他两个子菜单,新背景1就会保留在那里。 我已经在互联网上搜索了解决方案,但我找不到任何东西。有很多类似的东西,但不仅仅是这样,我只是不知道如何整合它。我试了几个,但都没有用。

这是我的工作菜单代码:

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>

1 个答案:

答案 0 :(得分:1)

<强>已更新

如果我理解你想要什么,这就是解决方案:

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)';
}