简体   繁体   English

如何在悬停时使图像在div中向上移动

[英]How to go about making an image move up in the div on hover

I have a question on how to go about making a div that when you hover it, an image on the inside will move up into the div. 我有一个关于如何制作div的问题,当你将它悬停时,内部的图像会向上移动到div中。

This is my code so far: 到目前为止这是我的代码:

<div id="div1">
  <div id="div2">
    <img src="http://dummyimage.com/300x300/000/fff" width="300px" height="300px">
  </div>
</div>

css CSS

#div1 { 
    width: 300px;
    height: 300px;
    border: 2px black solid;
    position: absolute;
    top: 100px;
    left: 500px;
    overflow: hidden;
}

#div2 img {
    position: absolute;
    top:200px;
}

This image that sits in the div1 sits at the bottom of the div, the rest of the overflow is hidden. 位于div1中的这个图像位于div的底部,其余的溢出被隐藏。 I want it to move up when the div is hovered. 我希望它在div徘徊时向上移动。

Any idea on how to go about this? 关于如何解决这个问题的任何想法? I was going to use the transform css and translateY it but I'm not sure if there is a better way that this can be done through JQuery. 我打算使用转换css并翻译它,但我不确定是否有更好的方法可以通过JQuery完成。

Let me know what you think 让我知道你的想法

Here is pure css approach with transition on hover 这是纯粹的css方法,在hover进行过渡

 #div1 { width: 300px; height: 300px; border: 2px black solid; position: relative; overflow: hidden; } #div2 img { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); transition: all 0.3s ease-in; } #div1:hover img { top: 30%; } 
 <div id="div1"> <div id="div2"> <img src="http://placehold.it/350x150"> </div> </div> 

#div1:hover #div2 img{
    top: 100px; //change this number to get correct placement
} 

Use :hover on your CSS and specify a new location for the image when you hover. 使用:将鼠标悬停在CSS上,并在悬停时为图像指定新位置。 You can even animate it so it "glides" into position. 你甚至可以为它制作动画,使其“滑行”到位。

You have the image unnecessarily wrapped in a div. 你的图像被不必要地包裹在div中。 just assign the id to the image itself 只需将id分配给图像本身

  <div id="div1">
        <img id="div2" src="http://www.maceire.com/wp-content/uploads/2015/01/grey- bac  kground-1-wide-hd-background-and-wallpaper.jpg" width="300px" height="300px">
  </div>

Then you can manipulate to top css value 然后你可以操纵顶部的css值

var q = document.getElementById('div2');
q.style.top = '0px';

and

q.style.top = '200px';

to move up and down 上下移动

For performance and simplicity, use CSS if possible and resort to javascript when you need to support legacy browsers. 为了性能和简单性,尽可能使用CSS,并在需要支持旧版浏览器时使用javascript。 Your needs here could easily be solved using just CSS: 只需使用CSS就可以轻松解决您的需求:

#div1 { width: 300px;
    height: 300px;
    border: 2px black solid;
    position: absolute;
    top: 100px;
    left: 500px;
    overflow: hidden;
}

#div2 img {
    position: absolute;
    top: 80%;
    transition: all 2s ease-in-out;
    -webkit-transition: all 2s ease-in-out; /** Chrome & Safari **/
    -moz-transition: all 2s ease-in-out; /** Firefox **/
    -o-transition: all 2s ease-in-out; /** Opera **/
}

#div1:hover #div2 img {
    top: 0%;
}

This approach does not require you to know the dimensions of the image. 此方法不要求您了解图像的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM