悬停时如何将图像放在其父 div 之外

2022-01-22 00:00:00 hover html css

我有一个可滚动的 div,其中包含一些 img 元素.我为图像添加了 widthheight 的悬停效果.它工作正常,但图像卡在 #scroller div 中.我希望图像在鼠标悬停时脱离滚动条 div .我怎样才能做到这一点 ?

I have a scrollable div with some img elements in it. I have added a hover effect of width and height for images. It works fine, but image get stuck in #scroller div. I want that the image get out of scroller div when mouse over it. How can I do that ?

CSS:

#scroller {
    width:100%;
    border:1px solid black;
    white-space:nowrap;
    height:130px;
    display:inline-block;
    overflow-x:scroll;
    overflow-y:hidden;
}
img {
    width:128px;
    height:128px;
}
img:hover {
    width : 192px;
    height:192px;
}

HTML:

<div id="scroller">
    <img/>
    <img/>
    <img/>
    <img/> ...and some more images
</div>

我没有尝试任何东西,因为我不知道.

I didn't try anything because I have no idea.

推荐答案

如果您打算仅使用 CSS,一种方法是将悬停时图像的位置更改为绝对位置.

If you are planning to use CSS only, one approach could be to change the position of the image on hover to absolute.

看这个例子:http://jsfiddle.net/Vn8M6/3/

#scroller {
    width:100%;
    height:100%;
    border:1px solid black;
    white-space:nowrap;
    height:130px;
    display:inline-block;
    overflow-x:scroll;
    overflow-y:hidden;
}

img {
    width:128px;
    height:128px;
    transition: ease-in-out 0s;  
    -webkit-backface-visibility: hidden;
    -webkit-transform: translateX(0);
     z-index:1;
    background-color:grey;
}

img:nth-child(2) {
    background-color:yellow;
     z-index:2;
}

img:nth-child(3) {
    background-color:red;
    z-index:3;
}

img:nth-child(4) {
    background-color:blue;
     z-index:4;
}

img:hover {
    width : 192px;
    height:192px;
    position:absolute;
    top:0;    
}

否则你可以使用 javascript 来获得更好的结果.

Otherwise you could use javascript to have achieve better results.

相关文章