我已经找到了使用div创建此事件的解决方案(请参见此处http://jsfiddle.net/zN983/1/),但是使用“提交”按钮使其发生似乎很棘手。

当悬停在“登录”按钮上时,我试图将内容背后的整个背景转换为黑色。

也可以在此过程中使页面上的所有黑色文本变为白色。

这是我与http://jsfiddle.net/63d5R/8/合作的内容

HTML:

    div id="login">



 <body bgcolor="#ffffff" link="#000000" vlink="#FF0000">    <font style="font-size: 70;" face="Courier new">Freddy&nbsp;<font style="font-size: 30;" face="courier new"> ARCHIVE</center></font></font>
</div>
<div class="archiveLogin">
    <center>
        <form name="loginform" id="loginform" action="php/processLogin.php" method="post">
            <table width="250" border="0" style="text-align: center;">
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><font face="FontName, Arial" size="1">USER</font>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="background: black; color: white; " type="text" name="username" id="username" class="input" value="" size="20" />
                    </td>
                </tr>
                <tr>
                    <td><font face="FontName, Arial" size="1">PASSWORD</font>
                    </td>
                </tr>
                <tr>
                    <div id="hover">
                        <td>
                            <input style="background: black; color: white;" type="password" name="password" id="password" class="input" value="" size="20" />
                        </td>
                    </div>
                </tr>
                <tr>
                    <td>
                        <input type="submit" class="loginButtonBodyBG" id="buttonText" value="Log In" />
                    </td>
                </tr>
            </table>
        </form>
    </center>
</div>
</body>
</div>


/ * CSS * /

    #login {
    text-align: center;
    font-size: 75;
    z-index: 2;
}

 .archiveLogin {
    padding-left: 0px;
}
input, textarea, select, a {
    outline: none;
    padding-top: 3px;
}
input:-webkit-autofill {
    background-color: black;
    color: white;
}
::-webkit-input-placeholder {
    font-family: courier new;
    font-size: x-small;
}
:-moz-placeholder {
    /* Firefox 18- */
    font-family: courier new;
}
::-moz-placeholder {
    /* Firefox 19+ */
    font-family: courier new;
}
:-ms-input-placeholder {
    font-family: courier new;
}

 input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px black inset;
    -webkit-text-fill-color: white;
}
#password {
    margin-bottom: 20px;
}
#buttonText {
    clear: both;
    background-color: black;
    border: 0 none;
    border-radius: 3px;
    cursor: pointer;
    display: inline-block;
    font-size: 12px;
    padding-left: 0px;
    height: 23px;
    line-height: 12px;
    margin: 0 5px 10px 0;
    padding-right: 0px;
    white-space: nowrap;
    width: 123px;
    color: white;
    opacity:1;
    text-align: center;
    margin-left: 3px;
}
#buttonText:hover {
    background-color: rgba(165, 122, 255, 0.7);
    border: 0 none;
}

最佳答案

如果我对您的理解正确,则可以这样做:

var button = document.getElementById('buttonText');
button.onmouseover = function(e) {
  document.body.style.background = "#000000";
  document.body.style.color = "#FFFFFF";
};
button.onmouseout = function(e) {
  document.body.style.background = "#FFFFFF";
  document.body.style.color = "#000000";
};


更新:升级以获得平滑效果

lerp = function(a,b,u) {
    return (1-u) * a + u * b;
};

fade = function(element, property, start, end, duration) {
    var interval = 10;
    var steps = duration/interval;
    var step_u = 1.0/steps;
    var u = 0.0;
    var theInterval = setInterval(function(){
        if (u >= 1.0){ clearInterval(theInterval) }
        var r = parseInt(lerp(start.r, end.r, u));
        var g = parseInt(lerp(start.g, end.g, u));
        var b = parseInt(lerp(start.b, end.b, u));
        var colorname = 'rgb('+r+','+g+','+b+')';
        element.style.setProperty(property, colorname);
        u += step_u;
    }, interval);
};

var black_col = {r:  0, g:  0, b:  0};
var white_col   = {r:255, g:255, b:255};
var button = document.getElementById('buttonText');

button.onmouseover = function(e) {
  fade(document.body,'background',white_col,black_col,2000);
  fade(document.body,'color',black_col,white_col,2000);
};
button.onmouseout = function(e) {
  fade(document.body,'background',black_col,white_col,2000);
  fade(document.body,'color',white_col,black_col,2000);

};

关于javascript - 将鼠标悬停在提交按钮上时,如何更改全身背景颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23984109/

10-14 17:30
查看更多