【问题标题】:Trying to build a grid of divs with opacity changing on hover试图建立一个不透明度在悬停时改变的div网格
【发布时间】:2012-11-25 08:35:47
【问题描述】:

请多多包涵,因为这是我尝试从头开始构建的第一个脚本,所以它很糟糕,我知道这一点。

我要创建的是:9 个 div 的网格,当鼠标悬停在 div 上时,其他 8 个淡入淡出为 0.25 不透明度。然后只要鼠标停留在网格上,“1”不透明度级别就会跟随鼠标。无论鼠标在哪里,您都有 1(实际上是 0.999)不透明度,在其他地方您有 0.25。

当鼠标完全退出网格区域时,所有 div 切换回 1 不透明度。

我知道这很复杂,所以我在这里创建了一个 jsfiddle: http://jsfiddle.net/Cooperdale/AKuKx/15/

如果你缓慢移动它实际上可以工作,但是脚本太不可靠:如果你移动鼠标更快,你会得到不可预知的结果,例如一组 div 为“on (1)”而其他 div 为“off ( 0.25)"。

这是我写的脚本:

    $(function() {

    $('#jazzmen').mouseleave(function() {
        $('#sq1').css({ opacity: 1 });
        $('#sq2').css({ opacity: 1 });
        $('#sq3').css({ opacity: 1 });
        $('#sq4').css({ opacity: 1 });
        $('#sq5').css({ opacity: 1 });
        $('#sq6').css({ opacity: 1 });
        $('#sq7').css({ opacity: 1 });
        $('#sq8').css({ opacity: 1 });
        $('#sq9').css({ opacity: 1 });
    }
    );


  $('.music9').hover(function() {
    if ($(this).css('opacity') == 1) {
        $(this).css({ opacity: 0.999 });
        if (this.id !== 'sq1') {
            $('#sq1').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq2') {
            $('#sq2').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq3') {
            $('#sq3').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq4') {
            $('#sq4').animate({opacity: 0.25}, 500);
        }        
        if (this.id !== 'sq5') {
            $('#sq5').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq6') {
            $('#sq6').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq7') {
            $('#sq7').animate({opacity: 0.25}, 500);
        }            
        if (this.id !== 'sq8') {
            $('#sq8').animate({opacity: 0.25}, 500);
        }
        if (this.id !== 'sq9') {
            $('#sq9').animate({opacity: 0.25}, 500);
        }
     }

     if ($(this).css('opacity') == 0.25) {
         $(this).animate({opacity: 0.999}, 200);

        if (this.id !== 'sq1') {
            $('#sq1').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq2') {
            $('#sq2').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq3') {
            $('#sq3').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq4') {
            $('#sq4').animate({opacity: 0.25}, 10);
        }        
        if (this.id !== 'sq5') {
            $('#sq5').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq6') {
            $('#sq6').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq7') {
            $('#sq7').animate({opacity: 0.25}, 10);
        }            
        if (this.id !== 'sq8') {
            $('#sq8').animate({opacity: 0.25}, 10);
        }
        if (this.id !== 'sq9') {
            $('#sq9').animate({opacity: 0.25}, 10);
        }
    }

  }

  );
});

我想脚本可以通过使用向量或其他东西变得更好。我希望有人可以帮助我使它更可靠,谢谢大家。

【问题讨论】:

  • 您会遇到错误,因为 animate 不会立即设置不透明度,而是会在 500 毫秒内消失。因此,您的代码中会出现节点为 .25 或 1 的情况。
  • 当鼠标离开网格时,真正的问题就出现了。其他功能很好,但有时当鼠标离开网格时,一个或多个 div 不会返回到 1 不透明度。如您所见,当鼠标离开网格时,效果是即时的,并且不使用动画。但这才是真正的问题开始的地方,所以我不认为动画是这里真正的问题。

标签: jquery opacity


【解决方案1】:

这样试试

$('.music9')
    .on('mouseover', function() {
        $(this).stop().animate({ opacity: 0.999 })
               .siblings().stop().animate({ opacity: 0.25 });
    })
    .on('mouseleave', function() {
        $('.music9').stop().animate({ opacity: 0.999 });
    });

DEMO

【讨论】:

  • 它有效,而且比我的短 95%!谢谢!现在我将查看文档并弄清楚它的作用:)。在您的示例中,不透明度可以设置为 1 而不是 .999 没有问题,我使用 .999 来避免一开始遇到的问题。
猜你喜欢
  • 2014-03-04
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2013-09-11
  • 2012-12-16
  • 2013-06-30
  • 2012-08-15
相关资源
最近更新 更多