【问题标题】:multiple mousehover with jquery?多个鼠标悬停与jquery?
【发布时间】:2009-11-14 00:38:32
【问题描述】:

假设我有一个带有背景图像(我的徽标)的 div,我需要将此 div 与其他 4 个相同图像但颜色不同的 div 进行切换。每个图像都将使用 jquery 效果在鼠标悬停时调用,因此它可以平滑显示,顺序为 div1、2、3、4、5,然后重新启动。

一个更简单的解释是..一个具有 5 种不同颜色背景的徽标,当您将鼠标移到徽标上时,每种背景颜色都会显示,就像您第一次通过它时它会是绿色的,但如果您再次将鼠标移到徽标上,它将是蓝色的,依此类推。

现在作为 javascript 和 jquery 的新手...我该如何实现呢?有人可以通过一些教程或特定文章指导我,或者如果代码开始,可以给我一个片段吗? 我之前试过问这个问题,有人回答了一段代码,它可能是一个变量(确切地说是一个计数器),但我真的不明白它是如何工作的以及如何实现它......

【问题讨论】:

    标签: javascript jquery counter mousehover


    【解决方案1】:

    您可以简单地将您的徽标制作为具有透明度的 gif/png 并将其设置为背景,然后在这些值之间为 backgroundColour css 属性设置动画(基于之前的脚本):

    $(document).ready(function()
    {
        var colours = ['blue','green','red','orange'];
        var colIndex = 0;
    
        $('#logo').mouseover(function()
        {
            if(colIndex > colours.length)
                colIndex = 0;
    
            $(this).css('backgroundColour', colours[colIndex]);
            colIndex++;
        });
    });
    

    【讨论】:

      【解决方案2】:

      类似的东西:

      $(document).ready(function() {
          var colours = ['blue','green','red','orange'];
          var colIndex = 0;
      
          $('#logo').mouseover(function() {
              if(colIndex > colours.length) {
                  colIndex = 0;
              }
              $(this).attr('src', colours[colIndex] + '-logo.jpeg');
              colIndex++;
          });
      });
      
      • 初始化一个数组,将其元素设置为 各种字符串前缀 徽标图像文件。
      • 将计数器初始化为零,因此我们 无需进行任何数学运算即可使用它 作为数组偏移量。
      • 当鼠标移到徽标上时 图像,将图像的来源更改为 存储在的字符串 colours[colIndex]。在我的例子中,我 已连接的最后一部分 文件名,假设约定 [colour name]-logo.jpeg。你可以 也可以把整个文件名 在数组中,不遵循任何约定。

      【讨论】:

      • 如何添加其他 4 张图片?我假设“-logo.jpeg”是主图像,但我在哪里添加其他图像?我可以将“蓝色”、“绿色”更改为我的图像源,例如“images/blue_logo.png”、“images/green_logo.png”吗?
      • 该脚本将根据“颜色”数组自动将 src 值替换为正确的文件名。因此,如果您的文件的名称是:blue-logo.jpg、green-logo.jpg、red-logo.jpg、orange-logo.jpg、yellow-logo.jpg,则语句 'colours[colIndex]' 将采用该值(颜色名称)并将其添加到“-logo.jpg”中,然后将其写入 src 属性值。
      【解决方案3】:

      我相信您的愿望比发布的其他答案要复杂一些-您希望图像平滑淡化。此代码在 div 中创建一个 div,然后在鼠标悬停时显示内部(因此也是最顶层)div,将较低(最外层)div 的背景更改为循环中的下一个,然后淡出上面的图像.它还具有选择性锁定机制,因此如果用户疯狂使用鼠标,它不会使图像以难看的方式闪烁。我希望这会有所帮助,如果您想更深入地解释代码的功能(您应该能够阅读它,大部分情况下),请在 cmets 中发布。

      这段代码有你的部分。

      Javascript:

      var pos = 0, lockOut = 0;
      var fade = 600; // change this if you like (in MS)
      // fix these div names
      var top = '#my-transitional-div', bottom = '#my-permanent-div';
      var images = [ // fix these paths
          'http://example.com/1.png',
          'http://example.com/2.png',
          'http://example.com/3.png',
          'http://example.com/4.png',
          'http://example.com/5.png'
      ];
      $(document).ready(function() {
          $(top).hide();
          $(bottom).css("background-image",'url(' + images[pos] + ')');
          $(bottom).live("mouseover",changeOut);
          pos = images.length;
          changeOut();
      });
      
      function changeOut() {
          if (++lockOut > 1) {
              lockOut--;
              return;
          }
          $(top).css("background-image",'url('+images[pos-1]+')').show().fadeOut(fade);
          if (pos >= images.length) pos = 0;
          $(bottom).css("background-image",'url('+images[pos++]+')');
          setTimeout(function(){lockOut--;},fade-10);
      }
      

      CSS:

      /* fix these div names (also height, width) */
      #my-transitional-div {
          width: 500px;
          height: 250px;
          margin: 0;
          padding: 0;
          position: absolute;
          left: 0;
          top: 0;
      }
      #my-permanent-div {
          position: relative;
          width: 500px;
          height: 250px;
          margin: 0;
          padding: 0;
      }
      

      HTML:

      <div id="my-permanent-div">
          <!-- notice that the "transitional" div (var=top) is INSIDE the other -->
          <div id="my-transitional-div"></div>
      </div>
      

      【讨论】:

      • 对我没用 :( 我将图像路径更改为 'images/button_hover_blue.png' 等等,我已经将永久 div 设为绝对,否则它会将我的其他 div 向下推 = (
      • 我不确定如果它是绝对的它会起作用,因为它可能会改变内部的位置。尝试删除所有与定位相关的规则(左侧、顶部和位置字段),如果您没有收到任何 javascript 错误,请发布代码的 JS 部分。或者不,你的选择 =)
      【解决方案4】:

      嘿,解散。这是一个带有顶部和左侧字段的绝对 div =( 这是代码:

      Javascript logo.js:

      var pos = 0, lockOut = 0;
      var fade = 600; // change this if you like (in MS)
      // fix these div names
      var top = 'div#my-transitional-div', bottom = 'div#my-permanent-div';
      var images = [ // fix these paths
          'images/button_hover_blue.png',
          'images/button_hover_yellow.png',
          'images/button_hover_green.png',
          'images/button_hover_pink.png',
          'images/button_hover_orange.png'
      ];
      $(document).ready(function() {
          $(top).hide();
          $(bottom).css("background-image",'url(' + images[pos] + ')');
          $(bottom).live("mouseover",changeOut);
          pos = images.length;
          changeOut();
      });
      
      function changeOut() {
          if (++lockOut > 1) {
              lockOut--;
              return;
          }
          $(top).css("background-image",'url('+images[pos-1]+')').show().fadeOut(fade);
          if (pos >= images.length) pos = 0;
          $(bottom).css("background-image",'url('+images[pos++]+')');
          setTimeout(function(){lockOut--;},fade-10);
      }
      

      CSS main.css:

      html{
          height:100%;
      }
      body
      {
          height:100%
          margin: 0;
          padding: 0;
          text-align: center;
          background-image:url(../images/bg.png);
          background-repeat:repeat;
      
      }
      
      div#container 
      {
          background-image:url(../images/bg.png);
          text-align: left;
          margin: auto;
          width: 760px;
          height:100%;
          min-height:100%;
          background-repeat:repeat;
          position:relative;
      } 
      div#logo{
          background-image:url(../images/logo.png);
          background-repeat:no-repeat;
          width:216px;
          height:235px;
          position:absolute;
          right:45px;
          top:5px;
          z-index:12;
      }
      div#my-transitional-div {
          background-repeat:no-repeat;
          width:216px;
          height:235px;
          position:absolute;
          right:45px;
          top:5px;
      }
      div#my-permanent-div {
          background-repeat:no-repeat;
          width:216px;
          height:235px;
          position:absolute;
          right:45px;
          top:5px;
      }
      

      HTML 索引.html:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <link rel="stylesheet" type="text/css" href="css/main.css" />
      <title>om</title>
      <script type='text/javascript' src='js/effects/logo.js'></script>
      </head>
      
      <body>
          <div id="container">
              <div id="logo"></div>
              <div id="my-permanent-div">
            <div id="my-transitional-div"></div></div>
      </div>
      </body>
      </html>
      

      【讨论】:

      • 抱歉回复晚了,我没有看太多 S.O.在周末。无论如何,我注意到在您的源文件中您不包含 jQuery 库,这可能是问题吗?另外,您确定图像的路径正确吗?如果站点位于文档根目录中,请尝试使用绝对路径(如“/images/...”)而不是没有前面正斜杠的相对路径
      猜你喜欢
      • 1970-01-01
      • 2011-08-04
      • 2011-07-07
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多