JQuery - 在鼠标悬停时淡出颜色

时间:2022-03-16 18:57:04

I have the following JQuery code to fade the background color of a div to a different color when the mouse hovers over the div element. It works great but it requires jqueryui.js to work. My pages already use the jquery.js for other purposes, so I have to load both frameworks.

当鼠标悬停在div元素上时,我有以下JQuery代码将div的背景颜色淡化为不同的颜色。它工作得很好,但它需要jqueryui.js才能工作。我的页面已经将jquery.js用于其他目的,所以我必须加载两个框架。

Can this be done only with jquery instead of jqueryui?

这可以只用jquery而不是jqueryui来完成吗?

<!-- fade page onload -->
$(document).ready(function(){
    $('#page_effect').fadeIn(1000);
});
<!-- fade login form to color on hover -->
$(document).ready(function(){
$("#frmLogin").hover(
function() {
$(this).stop().animate({ backgroundColor: "#fff"}, 800);
},
function() {
$(this).stop().animate({ backgroundColor: "#e6e6e6" }, 800);
});
});

Thank you!

谢谢!

2 个解决方案

#1


4  

Little bit hacky, but its the best I could come up with...

有点hacky,但它是我能想到的最好的......

Working example: http://jsfiddle.net/Damien_at_SF/paDmg/

工作示例:http://jsfiddle.net/Damien_at_SF/paDmg/

Code:

码:

<div id="frmLogin">
    <div id="bg"></div>
    <div id="text">BLAH BLAH HAHAHAH</div>
</div>

Fade in the 'bg' div (which is the background colour) when hovering over the content...

将鼠标悬停在内容上时淡入'bg'div(这是背景颜色)...

$(document).ready(function(){
$("#text").hover(
function() {
$("#bg").stop().fadeOut();
},
function() {
$("#bg").stop().fadeIn();
});
});

CSS for positioning:

用于定位的CSS:

#frmLogin {

    position:relative;
    height:400px;
    width:800px;

}

#bg{

    position:absolute;
    width:100%;
    height:100%;
    border:1px solid black;
    background:#e6e6e6;

}

#text {
    background:transparent;
    position:absolute;
    width:100%;
    height:100%;
    border:1px solid black;
}

jQueryUI is an awesome tool, I'd definitely use it over my solution...

jQueryUI是一个很棒的工具,我肯定会在我的解决方案中使用它...

Hope that helps :)

希望有所帮助:)

#2


8  

If you are OK with Jquery UI, a better solution would be just this

如果你对Jquery UI没问题,那么更好的解决方案就是这样

$(document).ready(function(){
    $("#sample").mouseover(function() {
         $(this).animate({ backgroundColor:'#f00'},1000);
    }).mouseout(function() {
        $(this).animate({ backgroundColor:'#ccc'},1000);
    });       
});

DEMO: http://jsfiddle.net/Starx/KpEMc/1/

演示:http://jsfiddle.net/Starx/KpEMc/1/

#1


4  

Little bit hacky, but its the best I could come up with...

有点hacky,但它是我能想到的最好的......

Working example: http://jsfiddle.net/Damien_at_SF/paDmg/

工作示例:http://jsfiddle.net/Damien_at_SF/paDmg/

Code:

码:

<div id="frmLogin">
    <div id="bg"></div>
    <div id="text">BLAH BLAH HAHAHAH</div>
</div>

Fade in the 'bg' div (which is the background colour) when hovering over the content...

将鼠标悬停在内容上时淡入'bg'div(这是背景颜色)...

$(document).ready(function(){
$("#text").hover(
function() {
$("#bg").stop().fadeOut();
},
function() {
$("#bg").stop().fadeIn();
});
});

CSS for positioning:

用于定位的CSS:

#frmLogin {

    position:relative;
    height:400px;
    width:800px;

}

#bg{

    position:absolute;
    width:100%;
    height:100%;
    border:1px solid black;
    background:#e6e6e6;

}

#text {
    background:transparent;
    position:absolute;
    width:100%;
    height:100%;
    border:1px solid black;
}

jQueryUI is an awesome tool, I'd definitely use it over my solution...

jQueryUI是一个很棒的工具,我肯定会在我的解决方案中使用它...

Hope that helps :)

希望有所帮助:)

#2


8  

If you are OK with Jquery UI, a better solution would be just this

如果你对Jquery UI没问题,那么更好的解决方案就是这样

$(document).ready(function(){
    $("#sample").mouseover(function() {
         $(this).animate({ backgroundColor:'#f00'},1000);
    }).mouseout(function() {
        $(this).animate({ backgroundColor:'#ccc'},1000);
    });       
});

DEMO: http://jsfiddle.net/Starx/KpEMc/1/

演示:http://jsfiddle.net/Starx/KpEMc/1/