javascript - 悬停后如何将内容放入框中?

标签 javascript jquery html css

我打算制作一种便签样式,并且我想在悬停未激活时显示框内的内容而不首先显示它。就像我的代码示例中的这里一样:请帮忙!

body {
  margin: 45px;
}

.box {
  display:inline-block;
  background-color: pink;
  height: 200px;
  width: 200px;
  transition: transform 300ms ease-in-out;
  pointer-events: none;
}

.container {
  width: 200px;
  height: 200px;
  border: 20px solid grey;
  background-color: violet;
}

.container:hover .box{
  transform: translate(200px, 150px) rotate(20deg);
}
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="CSSTransitions.css">
    <script src="jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>CSS Transitions</title>
  </head>
  <body>
    <div class="container">
      <p>Hello</p>
    <div class="box"></div>
  </div>
  </body>
</html>

fiddle

大家好,如果我对我想要实现的目标的解释不清楚,我深表歉意。没有悬停时有一个粉红色的盒子可见,悬停时里面有一个紫色的盒子。当粉红色框未悬停时,我希望隐藏紫色框内的段落。但是我的代码在这里显示,即使粉红色框没有悬停,紫色框内的段落也已经显示。

最佳答案

您必须设置内部 div 的 position: absolute.box 以将其放置在上方文本。请记住将 position: relative 设置为父 div,即 .container

Absolute positioned element is removed from the normal document flow; no space is created for the element in the page layout. Instead, it is positioned relative to its closest positioned ancestor if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left. This value creates a new stacking context when the value of z-index is not auto. Absolutely positioned boxes can have margins, and they do not collapse with any other margins.

More help on position property

body {
  margin: 45px;
}

.box {
  display: inline-block;
  background-color: pink;
  height: 200px;
  width: 200px;
  transition: transform 300ms ease-in-out;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
}

.container {
  position: relative;
  width: 200px;
  height: 200px;
  border: 20px solid grey;
  background-color: violet;
}

.container:hover .box {
  transform: translate(200px, 150px) rotate(20deg);
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="CSSTransitions.css">
  <script src="jquery-3.2.1.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <title>CSS Transitions</title>
</head>

<body>
  <div class="container">
    <p>Hello</p>
    <div class="box"></div>
  </div>
</body>

</html>

希望对您有所帮助。

关于javascript - 悬停后如何将内容放入框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184602/

相关文章:

javascript - 为什么 onchange 事件带我到新页面?

javascript - 无法将 Metro UI CSS 插入 SharePoint Web 部件

javascript - 在 IE11 上使用wheelDelta

javascript - 如何在Firefox中干净地停止网页?

javascript - 鼠标滚轮事件触发过于频繁/许多滚动会破坏动画

css - 修复了 Firefox 7.0.1 中的定位错误,一些溢出似乎导致固定 div 上的边距为 1px

javascript - jquery嵌套无法拖动到空列表

带边距的 jQuery Mobile Center 输入

php - 将 php 语法与 ajax 结合起来

javascript - Backbone.undo.js 不跟踪我的第二个跨度并且不保存起始值