【问题标题】:Jquery - background fade.Jquery - 背景淡化。
【发布时间】:2012-05-10 13:10:19
【问题描述】:

当您将鼠标悬停在框上时,我正在尝试使背景淡入。

Box1 是我鼠标悬停的那个盒子,hover1 是进来的新背景。这实际上工作得很好。但是,它会加载 acript,这意味着,如果我将鼠标悬停在盒子上而发疯,即使我的鼠标静止不动,褪色也会无休止地继续下去。我需要添加某种停止功能.. 内容是鼠标悬停时在内容框中更改的文本。这很好用。

$("#box1").mouseover(function(){
    $("#background").switchClass("nohover", "hover1", 500);
    $("#content").html(box1);

});

$("#box1").mouseout(function(){
    $("#background").switchClass("hover1", "nohover", 150);
    $("#content").html(content);

});

我也尝试过使用 var,但我仍然遇到同样的问题。如果我将鼠标快速悬停,则褪色会继续运行。

    var goeft = 0;
$("#box1").mouseover(function(){
 if(goeft == 0) {
   $("#background").switchClass("nohover", "hover1", 500);
  $("#content").html(box1);
   goeft = 1;
 }
});

$("#box1").mouseout(function(){
    $("#background").switchClass("hover1", "nohover", 150);
   $("#content").html(content);

   goeft = 0;
});

css代码-v-

      /* CSS Document */
       body
      {
      background-color:#B1EB78;
      }

     #wrapper
     {
     border:5px white solid;
     border-radius:15px;
     background-image:url(../images/mill.jpg);
     }

     #header
     {
     height:120px;
     background-image:url(../images/logo.png);
     background-repeat:no-repeat;
     }

     #content
     {
     height:250px;
     background-image:url(../images/trans_white.png);
     border:1px black solid;
     border-radius:5px;
     }

     #space
     {
     height:40px;
     }

     #space2
     {
     height: 10px;
     }

     #box1
     {
     height:250px;
     background-image:url(../images/trans_green.png);
     }




     #background
     {
     width:100%;
     height:100%;
     border-radius:9px;
     }


    .hover1
    {
    background-color:red;

    }

   .nohover
   {

   }

【问题讨论】:

  • 亲爱的提问者;您能否在JSFiddle 中重新创建您的问题?它允许人们观察和使用您的代码,从而更容易找到解决方案。此外,通常当我要问关于 SO 的问题时,我首先在 JSFiddle 中重新创建我的问题,然后自己及时发现并解决问题。
  • 我不能把它放在 JSFiddle 中,因为我的很多文件都是本地的。我外汇。不要链接到在线 jquery .. 所以它不会在 jsfiddle 中工作

标签: javascript jquery jquery-ui mouseover fade


【解决方案1】:
 var goeft = 0;
$("#box1").mouseenter(function(){
 if(goeft == 0) {
   $("#background").switchClass("nohover", "hover1", 500);
  $("#content").html(box1);
   goeft = 1;
 }
});

$("#box1").mouseleave(function(){
    $("#background").switchClass("hover1", "nohover", 150);
   $("#content").html(content);

   goeft = 0;
});

我不知道这些类,但我认为 mouseenter 和 mouseleave 是 mouseout 和 mouseover 的更好选择

【讨论】:

  • 我已将整个 css 文件放在我原来的帖子中。
  • 不幸的消息:switchclass 有一些问题,当你在 mouseover 动画完成之前将鼠标移出光标时,mouseout 函数不会运行,因为它的类当时没有从 nohover 更改为 hover1。
  • 如果你愿意,我可以给你一个替代方案
  • 你只需发布你的html代码,我会给你想要的解决方案
  • 制作一个 div(background-color:#ff0000;) 并设置它的 z-index -1 和 #background div z-index 2 ,然后将新的 div 和 #background 放在同一位置,之后onmouseover 和 mouseout 为新 div 的 opacity 属性设置动画。
猜你喜欢
  • 2016-01-28
  • 2013-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多