为啥在父元素中加了position: relative;就可以鼠标放上去的时候右移的同时消失,如果不加的话,就只能右移,但是不会消失
<html>
<head>
<link rel="stylesheet" href="./css/reset.css">
<style>
div{
width: 100px;
height: 100px;
overflow: hidden;
background-color: chartreuse;
position: relative;
}
img{
width: 100px;
height: 100px;
position: absolute;
left: 0px;
transition: 1s;
}
div:hover img{
left: 100px;
}
</style>
</head>
<body>
<div>
<img src="./source/1.jpg" >
</div>
</body>
</html>
img position是absolute,它的位置是相对最近的position非static的祖先元素,div 加position:absolute就是相对div,没有加就是相对body。因为div的overflow是hidden,img.left为100时就跑到div外了,被div裁剪了。如果img相对body定位就没div什么事了
position: relative;是相对定位,和hover一起用,就形成了移动的hover效果了