0

我有不同的“抽屉”,可以根据页面上的导航链接滑动打开和关闭。我正在使用一些 jQuery 来打开和关闭它们,并根据打开的“抽屉”更改页面背景和链接颜色。

我还使用一些 jQuery 来淡化链接上的悬停颜色。一切正常,除了让链接在鼠标移出时返回新的基色。它始终默认为原始 css 值。所以,我知道我必须进入以下内容并根据我希望链接在鼠标移出时返回的颜色来更改 .mouseout 颜色。

如果我只是像下面的例子那样设置一个变量,一切都很好,它工作得很好。

    var originalColor = "#123456";

    jQuery.fn.linkFader = function(settings) {
      settings = jQuery.extend({
        returnColor: originalColor,
        color: '#8dc63f',
        duration: 600
      }, settings);
      return this.each(function() {
        $(this).mouseover(function() { $(this).stop(true, true).animate({ color: settings.color },settings.duration); });
        $(this).mouseout(function() { $(this).stop(true, true).animate({ color: settings.returnColor },settings.duration); });
        $(this).click(function() { $(this).stop(true, true).animate({ color: settings.color },settings.duration); });
      });
    };

    $(document).ready(function() {
      $('.fader').linkFader({
      });
    });

BUT... If I try to assign the variable "originalColor" like the following, it fails. I need the script to figure out which "drawer" is open, and set the variable to the proper color. What am I doing wrong here?

      var originalColor='';
      if($('#drawerA').is(":visible")){
        var originalColor = "#123456";
      }

      if($('#drawerB').is(":visible")){
        var originalColor = "#456789";
      }
4

1 回答 1

2

Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout. - jQuery API

That's why your 2nd if always gets hit.

You have to hide it with display: none

FYI

Visible elements are elements that are not:

  • set to display:none
  • form elements with type="hidden"
  • Width and height set to 0
  • A hidden parent element (this also hides child elements)
于 2012-06-30T00:46:00.130 回答