gpt4 book ai didi

html - 对象跟随响应式背景图像

转载 作者:行者123 更新时间:2023-11-28 16:29:01 26 4
gpt4 key购买 nike

我正在尝试制作一个包含“兴趣点”的横幅图片,您可以将鼠标悬停在上面以查看更多信息。这些点需要相对于背景图像有一个固定的位置。如果 background-image 具有静态大小,这当然很容易,但这需要响应 background-size: cover 的效果。

我试过用像素和百分比来定位它们,但是由于 background-size: cover 改变了图像的宽度和高度,这变得很困难。

有人对此有任何解决方案吗?

我将包含一些 Jsfiddle,以便您明白我的意思:

https://jsfiddle.net/jynf21kw/1/

body{
margin:auto;
text-align: centeR;
}
.background{

}
.bg{
background-size:cover;
height:500px;
position: relative;
overflow: hidden;
}
.poi{
height:50px;
width:50px;
position:absolute;
}
.poi:after{
font-family: FontAwesome;
content: "\f067";
color:green;
font-size: 30px;
}
<!doctype html>
<html lang="no">

<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<style>

</style>
</head>

<body>

<header id="header" class="header">
<h1>Header</h1>
</header>


<div class="background">
<div class="bg" style="background-image: url('http://i.imgur.com/HmjQ2tj.jpg');">
<div class="poi-container">
<div class="poi" style="left:33%;top:33%;"></div>
<div class="poi" style="left:564px;top:497px;"></div>
<div class="poi" style="left:1132px;top:36px;"></div>
<div class="poi" style="left:1332px;top:336px;"></div>
</div>

</div>
</div>

<section class="other">
<h2>Some other content</h2>
</section>

</body>

</html>

最佳答案

我发现使用背景图像是理想的,而是使用顶部有图钉的内联图像。

然后图像以通常的方式响应,并且可以相对于图像包装器绝对定位图钉/标记。

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.map {
margin: 10px;
position: relative;
}
.map img {
max-width: 100%;
display: block;
}
.box {
width: 5%;
height: 5%;
background-image: url(http://www.clker.com/cliparts/W/0/g/a/W/E/map-pin-red.svg);
background-position: top center;
background-repeat: no-repeat;
background-size: contain;
position: absolute;
}
#pin-1 {
top: 25%;
left: 36%;
}
.box:hover > .pin-text {
display: block;
}
.pin-text {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 75%;
white-space: nowrap;
display: none;
}
.pin-text h3 {
color: white;
text-shadow: 1px 1px 1px #000;
}
<div class="map">
<img src="http://connect.homes.com/wp-content/uploads/2012/05/200392710-0012.jpg" alt="" />
<div id="pin-1" class="box">
<div class="pin-text">
<h3>My House</h3>
</div>
</div>
</div>

关于html - 对象跟随响应式背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com