如何将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上方时,它会向下推动它下面的元素,以便工具提示可以放在页面上,当我移动鼠标时,可以将其移动到&#39; unhover&#39;它,元素向上定位。我想要一种方式,当我悬停时,一个消息框被带到前面,下面的所有元素都不会移动。就像你在这个页面上悬停任何链接一样,它们会弹出一个小对话框,只有在悬停时才会在页面上重新定位其他元素。
答案 0 :(得分:2)
您需要指定z-index
和position
属性;例如:
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
答案 1 :(得分:1)
我为你创造了一个jsfiddle。单击以下链接查看示例: http://jsfiddle.net/xL7j4k6g/
请参阅上面的链接,但这里也是代码:
.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;
}
&#13;
<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>
&#13;
(这看起来并没有#34;酷&#34;但是有效。我建议将CSS3过渡到一些不错的转换触摸 - 例如在悬停时淡入工具提示。)
谢谢, 大卫
答案 2 :(得分:0)
您可以使用绝对定位和jQuery。这不是完美的,而是一个简单的例子。
$(document).ready(function() {
$('.box1').hover(function() {
$('.box2').toggleClass('active')
})
})
&#13;
.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;
}
&#13;
<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>
&#13;