html 元素悬停在另一个元素上?

html element to hover over another element?

如何将一个 html 元素放在另一个元素的前面,以便当一个特定元素悬停在新元素上时,新元素将出现在它的前面。 这是我的代码:

<div class="third">
        <label> Enter Password: </label>
        <input type="text" name="pword1" class="iBox" id="pword1" onmouseout="HideToolTip()" onmouseover="ShowToolTip()" onkeyup="allFunctions()" placeholder="choose a password" autocomplete="off">
        <p id="tooltipbox" style="visibility:hidden">Password must be between 8-16 characters, contain an uppercase, lowercase, number and special character</p>
    </div>

我有一个工具提示,目前可以使用。但是当我将鼠标悬停在 textarea 上时,它会将其下方的元素向下推,以便工具提示可以适合页面,当我将鼠标移动到 'unhover' 它时,元素会重新向上定位。我想要一种方法,当我悬停时,一个消息框被带到前面,下面的所有元素都不会移动。就像当您将鼠标悬停在该页面上的任何链接时,它们会弹出一个小对话框,该对话框仅在悬停时出现并且不会重新定位页面上的其他元素。

您需要指定z-indexposition属性;例如:

p#tooltipbox{
   z-index:1000;
   position:absolute;
   top:0;//move the element to the top of div.third of div.third
   left:0;//if you want to move the element to the left;
}
div.third{
   position:relative;
}

这里有更多信息
z-index property
position propery

我为你创建了一个 jsfiddle。点击下面的link看例子: http://jsfiddle.net/xL7j4k6g/

参考上面的link,这里也是代码:

.fieldarea {
    position: relative;
    border: 1px solid red;
    width: 50%;
}
.fieldarea label {
    width: 35%;
    display: inline-block;
}
.fieldarea input {
    width: 50%;
    display: inline-block;
}
.tooltipbox {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 1000;
    max-width: 200px;
    border: 1px solid gray;
    background-color: yellow;
    opacity: 0;
    transition: opacity .25s ease-in-out;
    -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
}
.fieldarea:hover .tooltipbox {
    opacity: 1;
}
<div class="fieldarea">
        <label for="pword1">Enter Password:</label>
        <input type="text" name="pword1" placeholder="choose a password" autocomplete="off">
        <div class="tooltipbox">Password must be between 8-16 characters, contain an uppercase, lowercase, number and special character</div>
    </div>
<div class="fieldarea">
        <label for="pword1">Enter Password:</label>
        <input type="text" name="pword1" placeholder="choose a password" autocomplete="off">
        <div class="tooltipbox">Password must be between 8-16 characters, contain an uppercase, lowercase, number and special character</div>
    </div>  

(这看起来 "cool" 但有效。我建议研究 CSS3 过渡到一些不错的转换触摸 - 例如,在悬停时淡入工具提示。)

谢谢, 大卫

您可以使用绝对定位和 jQuery。这并不完美,只是一个简单的例子。

$(document).ready(function() {
  $('.box1').hover(function() {
    $('.box2').toggleClass('active')
  })
})
.wrapper {
  position: relative;
  width: 100px;
  height: 100px;
  overflow: hidden;
  border: 2px solid black;
  background-color: white;
}
.box1 {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 0;
  left: 0;
  background-color: blue;
}
.box2 {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 0;
  left: 100;
  background-color: red;
}
.active {
  left: 0;
}
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="wrapper">
    <div class="box1"></div>
    <div class="box2"></div>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="script.js"></script>
</body>

</html>