Display image on hover, centered in the background(在悬停时显示图像,在背景中居中)
问题描述
我在 Stackoverflow 上遇到了许多关于在悬停时显示图像的问题,但找不到解决我的问题的方法.
I went through a number of questions regarding displaying image on hover here on Stackoverflow, but couldn't find a solution for my problem.
现在,当我将鼠标悬停在段落的背景上时,我设法在该段落的背景中显示了一张图片.查看 JSFiddle.
Right now I managed to display an image in the background of the paragraph once I hover on it. See on JSFiddle.
理想情况下,我想实现什么:一旦我将鼠标悬停在段落内的 Example1 上,我想在背景中显示 Image1,而不是在段落下方,而是在背景中居中,在所有元素下方.如图所示.
Ideally what would I like to do achieve: once I hover over an Example1 inside of a paragraph I would like to display Image1 in the background, not below the paragraph, but centered in the background, below all of the elements. As pictured here.
将鼠标悬停在 Example2 上会显示 Image2,Example3 会显示 Image3.
Hovering on Example2 would ideally display Image2, and Example3 would display Image3.
HTML
<div id="imgbg">
<p>Portfolio:</p>
<p>I worked with:<br />
Example1, Example2, Example3, Example4, Example5
</p>
</div>
CSS
#imgbg {
height: 100%;
width: 100%;
}
#imgbg:hover {
background-image: url('http://i.imgur.com/9bbjE1Mb.jpg');
}
有人有解决办法吗?非常感谢.
Does anyone have a solution? Many thanks.
推荐答案
希望对你有帮助.
#imgbg {
position: relative;
width: 100%;
height: 265px;
}
#imgbg .Example:hover:after {
display: block;
}
#imgbg .Example:after {
content: '';
display: none;
width: 160px;
height: 160px;
background: url('http://i.imgur.com/9bbjE1Mb.jpg');
background-size: contain;
position: absolute;
top: 50%;
left: 0;
margin-top: -80px;
margin-left: 145px;
z-index: -1;
}
<div id="imgbg">
<p>Portfolio:</p>
<p>I worked with:<br />
<span class="Example">Example1</span>,
<span class="Example">Example2</span>,
<span class="Example">Example3</span>,
<span class="Example">Example4</span>,
<span class="Example">Example5</span>
</p>
</div>
这篇关于在悬停时显示图像,在背景中居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在悬停时显示图像,在背景中居中
基础教程推荐
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在for循环中使用setTimeout 2022-01-01