jquery - 如何淡出一个 div 并淡入另一个?

标签 jquery html css

我的目标是:

  • 鼠标悬停时,淡出“foo”并淡入“bar”
  • 鼠标移开时,淡出“bar”并淡入“foo”

我可以在鼠标悬停时淡出“foo”,但是当我将鼠标移到“bar”div 之外时,我无法淡入“bar”然后返回正常状态。

我有以下代码:

html

<ul>
  <li>
    <div class="foo"></div>
    <div class="bar"></div>
  </li>
  <li>
    <div class="foo"></div>
    <div class="bar"></div>
  </li>
</ul>

CSS

li .bar { display: none; }

js

// On mouse over, fade out 'foo' and fade in 'bar'
jQuery('.foo').mouseover(function() {
  jQuery(this).fadeOut(function() {
    jQuery('.bar').fadeIn(); <-- This is wrong. I need to get the bar inside this li
});

// On mouse out, fade out 'bar' and fade in 'foo'
jQuery('.bar').blur(function() {
  jQuery(this).fadeOut(function() {
    jQuery('.foo').fadeIn();
});

这是我糟糕的 jsFiddle 尝试:http://jsfiddle.net/TvCT5/2/

感谢向正确方向的插入 :)

最佳答案

这就是您想要的效果吗?

jsFiddle example

jQuery:

jQuery('.foo').mouseover(function(){
    jQuery(this).fadeOut(function(){jQuery(this).siblings().fadeIn();});
});
jQuery('.bar').mouseout(function(){
    jQuery(this).fadeOut(function(){jQuery(this).siblings().fadeIn();});
});

关于jquery - 如何淡出一个 div 并淡入另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550967/

相关文章:

javascript - 在 JQuery 中,.html(var) 与 .html(var.html())

jquery - 使用 HTML5/jQuery 从 iPhone 获取用户号码

html - 如何以文本为背景制作新行?

javascript - 如何防止加载查询的 css 应用于整个页面?

jquery - bxslider slideWidth 的宽度问题

jquery - 如何将用户 ID 传递到 django ajax post 中?

javascript - 无法访问 HTML 中的嵌套 JSON 对象

html - 自动调整图像大小以适应浏览器 (css Html)

jquery - 在悬停时显示 div 但使其点击通过

php - 用css设计wordpress插件