javascript - Jquery效果-鼠标悬停时背景颜色变化;

标签 javascript jquery css mouseevent

我正在尝试创建一种效果,即当鼠标悬停在其上时,背景颜色从左向右更改。 我试过这个:

http://jsfiddle.net/WVWKE/

$('#div1').mouseover(function(){
$('#back').animate({width: '200px'}, 1000);
});
$('#div1').mouseout(function (){
$('#back').animate({width: '0px'}, 1000).stop();
});

#div1{
height:200px;
width:200px;
background-color:green;
float:left;
}

#back {
width:0px;
height:200px;
background-color:Gray;
display: block;
}


<div id="div1">
<div style="position:absolute">That was amazing!</div>
<div id="back">
</div>
</div>

但是,无法正常工作。如果我将鼠标多次放在 div 上,则会多次产生效果,但不会。 尝试多次放置鼠标并离开。这种效果会发生很多次。 有什么帮助吗?

最佳答案

您错误地使用了stop。像这样使用它:

$('#div1').mouseover(function (){
    $('#back').stop().animate({width: '200px'}, 1000);
});
$('#div1').mouseout(function (){
    $('#back').stop().animate({width: '0px'}, 1000);
});

如果你像英语一样思考就很容易理解:

$('#back')                         // take "back"
.stop()                            // and stop it
.animate({width: '200px'}, 1000);  // and animate it

<强> Fiddle

关于javascript - Jquery效果-鼠标悬停时背景颜色变化;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159467/

相关文章:

jquery - 使用 JQuery 和 3 个单选按钮显示/展开不同的 HTML

javascript - 以一种颜色显示的 LinearGradient

html - PSD to html 从上到下的大小转换成html

jquery - 关闭按钮在弹出窗口中不起作用

javascript - 在使用 iframe 显示 pdf 时在 firefox 中隐藏 pdf 工具栏

javascript - 如何将文本区域的内容复制到剪贴板?

javascript - 为什么 javascript 静态变量在不创建最少一个实例的情况下无法访问(使用类名)?

javascript - 无法使用模式为 'navigate' 和非空 RequestInit 的 Request 构造 Request

javascript - 鼠标悬停 Action 只运行一次(如何)

jquery - 居中 Bootstrap col-xs-* 类(居中 Bootstrap 固定宽度输入机器人)