如何在我的div的右侧制作悬停盒?

时间:2017-05-31 18:30:46

标签: html css

我试图修改this  代码,因此框将滑动到框的左侧。 我在想我应该在课堂上更改.overlay right: 0;right: 100%;,但它没有做任何事情。看起来应该是enter image description here 另外当盒子向右滑动并且我将鼠标悬停在盒子上时,我该怎么办?我的盒子一直停留,直到我移开鼠标,我该如何解决?

当我向右侧做动画时,这是css代码:

.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 100%;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 0;
  height: 100%;
  transition: .5s ease;
}

.container:hover .overlay {
  width: 100%;
}

.text {
  white-space: nowrap; 
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}

2 个答案:

答案 0 :(得分:1)

使用此代码在悬停时将叠加层移至左侧

.overlay {
  position: absolute;
  bottom: 0;
  left: 0; 
  background-color: #008CBA;
  overflow: hidden;
  width: 0;
  height: 100%;
  transition: .5s ease;
}

.container:hover .overlay {
 width: 100%;
 left: -100%;
}



.container {
  position: relative;
  width: 50%;
  float:right;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0; 
  background-color: #008CBA;
  overflow: hidden;
  width: 0;
  height: 100%;
  transition: .5s ease;
}

.container:hover .overlay {
  width: 100%;
  left: -100%;
}

.text {
  white-space: nowrap; 
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}

<h2>Slide in Overlay from the Right</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是你想要做的吗? https://codepen.io/anon/pen/MmNXmN

<强> HTML

<h2>Slide in Overlay from the Right</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
    <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
    <div class="overlay">
        <div class="text">Hello World</div>
    </div>
</div>

<强> CSS

.container {
  position: relative;
  width: 50%;
  float: right;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  right: 100%;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 0;
  height: 100%;
  transition: .5s ease;
}

.container:hover .overlay {
  width: 100%;
  right: 100%;
}

.text {
  white-space: nowrap; 
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}