3

我正在尝试将输入字段设置为 opacity:0.8; 如果鼠标悬停在输入字段上,并且输入处于焦点状态并且输入中有数据,则渐变为 1.0。

如果上述任何条件不成立,则让输入衰减回 0.8。

我已经将焦点功能与 mouseover/mouseout 和 change 功能混合使用,该功能验证输入的 val() 但我无法获得所需的效果。

任何人都可以帮忙吗?

HTML

<form id="ajax-contact-form" class="contactForm">
    <label>Simply type your email.</label>
        <input class="textbox" name="email" type="text" value="">
        <input class="sendMessage" name="submit" value="Send Email" type="submit">
</form>

CSS

    .contactForm .textbox {
    width: 564px;
    height: 46px;
    margin: 0;
    padding: 0 0 0 15px;
    font-size: 16px;
    color: #182A76;
    outline: 0;
    border: 0;
    background: url('../images/form.png') left 97% no-repeat;
    opacity:0.8;
    filter:alpha(opacity=80)
    }

查询

$(document).ready(function(){
    $("input.textbox").mouseover(function() {
    $('.textbox').fadeTo("slow", 1);
}).mouseout(function() {
    $('.textbox').fadeTo("slow", 0.8);
})

if($("input.textbox").val() === "") {
    $('.textbox').css({ opacity: 0.8});
} else {
    $('.textbox').css({ opacity: 1});

}

$('input.textbox').focus(function() {
        $(this).stop().fadeTo("slow", 1);
    }).blur(function() {
        $(this).stop().fadeTo("slow", 0.8);
    });

});

4

1 回答 1

4

试试这个 -演示

$("input")
    .on("mouseover focus",  function() { 
        $(this).animate({ opacity: 1 }); 
    })
    .on("mouseleave blur", function() {
        if ( $(this).val() == '' && !$(this).is(":focus") ) {
            $(this).animate({ opacity: .4 });
        }
    });
于 2012-07-30T18:30:57.167 回答