【问题标题】:Icon to move when hovering over a menu button将鼠标悬停在菜单按钮上时要移动的图标
【发布时间】:2012-12-28 15:37:36
【问题描述】:

在你阅读这篇文章之前,请打开这个网站看看我想要做什么:

https://www.kris-willis.com

如您所见,菜单下方有一个红色箭头,我想要实现的是...当我将鼠标悬停在菜单按钮上时,箭头会移动到我悬停在同一个按钮上无需重新加载页面。

理想情况下,我希望箭头移回默认按钮。如果单击不同的菜单按钮,默认按钮也可以更改。

如果您知道任何指向示例等的链接...我将不胜感激!

感谢您的宝贵时间,

克里x

【问题讨论】:

  • 链接上的 SSL 错误...
  • @NuclearGhost 只要在浏览器中输入链接就可以了:)
  • @Snuffleupagus 我曾尝试使用 javascript 从头开始​​构建它,但一直失败,所以想知道是否有类似的东西可以学习。
  • 不要使用表格作为菜单。另请参阅this
  • 你想让箭头 /move/ 到按钮还是 /jump/ 到按钮?

标签: javascript jquery html css


【解决方案1】:

首先是您的 DOCTYPE 错误。

<!DOCTYPE html PUBLIC "">

这会使您的页面以怪异模式加载。改成

<!DOCTYPE html>

对于 HTML5 或使用完整的,包括 FSI 和 FPI。

其次,您使用&lt;table&gt; 进行导航。没有什么严重的问题,但人们倾向于使用ul

对于:hover,您可以简单地使用

#MenuPosition table tbody tr td:hover
{
background-image: url("/images/Arrow.jpg");
}

您可能需要使用paddingsmargins 或使用display: blockdisplay: inline-block 来正确定位箭头。

【解决方案2】:

制作“按钮”锚点。使用 css set 为:hover 创建一个规则来设置包含箭头的背景图片。

那里有很多 CSS 教程,NettutsWebdesigntuts 有很多导航文章。或者,如果您喜欢模仿他人,请找到您喜欢的网站并挑选来源,直到您弄清楚他们是如何做到的。

请记住,完成您正在做的事情根本不需要 javascript。除非你想要一些动画,即使这样 CSS 可以处理大部分工作,我认为纯 CSS 是更好的方法。

【讨论】:

    【解决方案3】:

    纯 CSS 解决方案

    检查这个答案。

    Is there any way to hover over one element and affect a different element?

    可能是这样的:

    #thething {
        margin: 0;
    }
    .classone:hover + #thething {
        margin-top: 10px;
        margin-left: 10px;
    }
    

    如果它们是父 div 中的相邻兄弟。

    【讨论】:

      【解决方案4】:

      只需将箭头相对于td 的左侧移动margin-left DEMO

      $("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});
      

      Tp 这样做将 jQuery 库添加到页面的头部

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      

      将此代码添加到外部 js 文件中,并将其添加到页面的 head 部分

      $(function(){
        $("#MenuPosition").on("hover","td",function(){
          $("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});
      
        });
      
      });
      

      编辑:用于恢复箭头原始位置

      $(function(){
        currentPos = $("#Arrow").css("margin-left");
      
        $("#MenuPosition").on("hover","td",function(){
          $("#Arrow").css({"margin-left":$(this).position().left});
      
        });
      
      
         $("#MenuPosition").on("mouseout","td",function(){
      
           $("#Arrow").css({"margin-left":currentPos});        
        });
      
      });
      

      注意:请查看计算部分并进行更正。

      PS:不能正确是因为它是我从办公室注销的时间;)。但我觉得你有这样做的逻辑

      【讨论】:

      • 有没有办法设置一个默认按钮,让箭头在鼠标离开按钮后移回?
      【解决方案5】:

      你可以这样做:

      使用 span 在 HTML 中的导航/菜单下方添加 bg 箭头:

      <ul class="nav">
          <li>
              <a href="#">Menu 1</a>
              <span class="arrow">&nbsp;&nbsp;</span>
          </li>
          <li>
              <a href="#">Menu 2</a>
              <span class="arrow">&nbsp;&nbsp;</span>
          </li>
      </ul>
      

      CSS:

      .nav {
          font-size: anypx;
          list-style: none outside none;
          margin: 0;
          padding: 0;
          position: relative;
      }
      
      .nav li {
          background: #whatev;
          display: block;
          float: left;
          height: anypx;
          line-height: anypx;
          padding: 0;
          text-align: center;
      }
      
      .nav li a {
          color: #any;
          display: block;
          padding: any;
          position: relative;
          text-decoration: none;
          width: auto;
      }
      .arrow {
          background: url("images/arrow.png") no-repeat scroll 0 9px transparent;
          display: none;
          height: anypx;
          text-indent: -9999px;
          width: whatevs;
          z-index: 9999;
      }
      

      最后是使它工作的 JS/Jquery:

      $(document).ready(function(){     
          Your_menu();
      });
      
      function Your_menu(){
      
          $(".nav li").hover(function(){
                  $(this).find('.arrow').css({visibility: "visible",display: "none"}).show();
              },function(){
                  $(this).find('.arrow').css({visibility: "hidden"});
          });
      
      }   
      

      这是一个显示这个的网站:)

      http://www.drexelmedicine.org/

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        • 2018-08-19
        • 2022-11-10
        相关资源
        最近更新 更多