【问题标题】:Hover effect on multiple boxes多个框的悬停效果
【发布时间】:2013-06-30 21:13:34
【问题描述】:

我有多个框,我想对每个框应用悬停效果,当我悬停一个框时,所有其他框应该看起来更透明,从而产生更集中的效果。 我需要通过某种缓慢的过渡效果来完成它。

我尝试过如下,但似乎效果不佳

http://jsfiddle.net/ahmed2222/rSNS5/2/

<div class="container">
    <div class="thumb"> </div>
    <div class="thumb"> </div>
    <div class="thumb"> </div>
    <div class="thumb"> </div>
    <div class="thumb"> </div>  
    <div class="thumb"> </div>
    <div class="thumb"> </div>
    <div class="thumb"> </div>
    <div class="thumb"> </div>
 <div>

.container {
    width: 400px;
    overflow: auto;
}

.thumb {
    width:100px;
    height:100px;
    background: #000;  
    display: inline-table;
    margin: 10px;
}

$(function() {
    $('.thumb').hover(function () {
      $(this).css({"opacity":"1"});
      $('.thumb').not(this).animate({"opacity":"0.3"}, 1000);
    },
    function () {
      $('.thumb').css({"opacity":"1"});
    });
});

知道吗,为什么我的代码不起作用?

【问题讨论】:

  • 你的问题是?
  • 我认为您提供的代码没有任何问题...假设您正在尝试执行您正在尝试执行的操作。
  • 对不起,我已经更新了 jsfiddle 链接,上面的代码不能正常工作
  • 你的小提琴没有 js 或 css。这里:jsfiddle.net/KfARr

标签: javascript jquery css animation jquery-animate


【解决方案1】:

我注意到您原始解决方案的唯一真正问题是,当您快速从一个项目移动到另一个项目时,有时正在运行的动画会导致问题。这是您想要解决的问题吗?如果是这样,您可以输入stop()s 来停止动画。

试试这个:http://jsfiddle.net/rSNS5/4/

$(function() {
    $('.thumb').hover(function () {
      $('.thumb').stop();
      $(this).css({"opacity":"1"});
      $('.thumb').not(this).animate({"opacity":"0.3"}, 1000);
    },
    function () {
      $('.thumb').stop().css({"opacity":"1"});
    });
});

【讨论】:

【解决方案2】:

不需要javascript来实现,纯css就可以了:

.container:hover .thumb {
    opacity: .5;
}

.container:hover .thumb:hover {
    opacity: 1;
}

还有一个用于演示的小提琴:http://jsfiddle.net/pPWgq/

编辑:
并带有过渡:http://jsfiddle.net/pPWgq/1/

刚刚添加了这个:

.thumb {
   ...
   transition: opacity 1s;
}

【讨论】:

  • OP 说:“我需要通过某种缓慢的过渡效果来完成它。”
  • @PeterVR 干得好!请注意,当您悬停容器而不悬停任何拇指时,它们都会变得透明。我相信这是不希望的
  • 定位容器的好主意!我仍然希望它与 JS 一起在旧浏览器上工作。我会尝试玩弄这个想法。谢谢
猜你喜欢
  • 2014-01-20
  • 2014-10-13
  • 1970-01-01
  • 2016-09-21
  • 2021-11-12
  • 2012-07-14
  • 1970-01-01
  • 2016-02-24
  • 2010-11-30
相关资源
最近更新 更多