【问题标题】:Tooltips work in Firefox but not in Chrome工具提示在 Firefox 中有效,但在 Chrome 中无效
【发布时间】:2020-11-24 22:08:26
【问题描述】:

在我的web-app 中,我在带有图像的按钮上实现了一些工具提示。在 Firefox 中,它们按预期工作,也就是说,当您将鼠标悬停在该按钮上时,它们会出现在该按钮的正下方。但是,在 Chrome 中,它们出现在按钮的最左侧。

CSS:

      .tooltip {
        display: none;
      }
      button:hover .tooltip {
        display: block;
        position: absolute;
        background: #ffffaa;
        z-index: 10;
        padding: 2px;
        font-style: italic;
        font-family: Times;
      }

HTML:

      <div id="simulationButtons">
        <button id="playPauseButton">
          <img
            src="play.svg"
            alt="play"
            id="playImage"
            style="display: inline"
          />
          <img
            src="pause.svg"
            alt="pause"
            id="pauseImage"
            style="display: none"
          />
          <span class="tooltip">Play/Pause</span>
          <!--Didn't know modern browsers don't display alts automatically
              when you hover over an image.-->
        </button>

这在 Firefox 中按预期工作(这是适用于 Android 的 Firefox,但在桌面上看起来很相似):

但是,由于某种原因,在 Chrome 中,工具提示被移动到按钮的左侧(这是 Android 上的 Chrome,我还没有设法在我的 Linux 上安装 Chrome,但我认为它在 Chrome 上看起来会很相似桌面):

那么,Chrome 中发生了什么?我该如何解决?

【问题讨论】:

  • 在 chrome 中似乎可以正常工作。问题可能出在其他地方。你能发布一个工作代码示例吗?

标签: javascript html css google-chrome


【解决方案1】:

您需要在&lt;button id="playPauseButton"&gt;&lt;div id="simulationButtons"&gt;中设置position:relative

【讨论】:

  • 好吧,不,当我这台电脑上没有 Chrome 的时候,尝试起来并不容易。
  • 为什么不安装chrome?
  • 如何在 Oracle Linux 上安装 Chrome? Google 上提供的 RPM 适用于 Fedora,尝试在 Oracle Linux 上安装它会给我大量错误消息。
  • @FlatAssembler a quick google search
【解决方案2】:

您正在使用position:absolute 定位工具提示,因此父“按钮”应该有position:relative

将此代码添加到您的 css 中,它会正常工作。

#playPauseButton, #fastForwardButton, #singleStepButton, #stopButton {
    position: relative;
}

每当您制作此类新按钮时,请务必将position:relative 也添加到该按钮。

【讨论】:

  • 您的答案与我的答案相差 10 分钟,并且使用相同的解决方案。为什么不投票而不是重复答案?
  • 你的回答也是对的,但缺少一些东西,所以我只是提到了这些。如果您感觉不好,我很抱歉。
猜你喜欢
  • 2016-02-19
  • 2022-01-23
  • 1970-01-01
  • 2020-04-04
  • 2011-03-16
  • 2015-06-11
  • 2018-07-05
  • 2014-08-18
  • 2016-07-01
相关资源
最近更新 更多