缩小容器以适应图像宽度

3
我目前正在开发一个网站,用户应该能够在其中水平滚动浏览一个带有可点击信息点的景观。为了确保网站完全响应式,我想把我的景观图片和信息点放在一个与图片大小完全相等的容器中。

HTML:

<div id="container-main">
    <div id="landscape">
        <img class="background" src="image.jpg" />

        <div class="point" style="top: 24%; left: 7.5%;"></div>
        <div class="point" style="top: 29%; left: 17.7%;"></div>
        <div class="point" style="top: 77%; left: 39%;"></div>
        <div class="point" style="top: 26%; left: 68%;"></div>
        <div class="point" style="top: 70%; left: 80%;"></div>
    </div>
</div>

CSS:

html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    height: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}

我的CSS在浏览器窗口高度改变时完全正常,然而信息点会移动到错误的位置...但是当您刷新调整大小后的网站时,它又能正常工作了。
试一下:Fiddle(调整输出,然后再次点击“运行”)。
在我的真实项目中,我使用JavaScript将容器的宽度设置为图像的宽度,但我很想有一个干净的CSS解决方案。
我知道有类似的问题,但是没有一个建议的解决方案适合我。
提前感谢您!
2个回答

1

使用vw代替%

调整以下代码:
<div id="container-main">
    <div id="landscape">
        <img class="background" src="image.jpg" />

        <div class="point" style="top: 24vw; left: 7.5vw;"></div>
        <div class="point" style="top: 29vw; left: 17.7vw;"></div>
        <div class="point" style="top: 77vw; left: 39vw;"></div>
        <div class="point" style="top: 26vw; left: 68vw;"></div>
        <div class="point" style="top: 70vw; left: 80vw;"></div>
    </div>
</div>
<style>
html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    height: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}
</style>

.....................另一种解决方案.........................................................

更改CSS:

html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    width: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}

注意:请查看 网站 的响应式编码标准:


我喜欢你的解决方案,但我有一点担心浏览器支持,因为Safari(桌面和移动版)在这个单位上存在一些问题。 - Rico Ocepek
请查看另一种解决方案。 - Asif Iqbal

0
首先,除非您希望div或容器在滚动时保持在同一位置,否则永远不要在css中使用position:fixed;。 其次,我建议使用jquery来制作流体布局。流体布局的作用是,在调整网站大小时,它会使页面内容保持在同一位置。如果您不想浪费时间编写长脚本文件,我个人建议您使用Dreamweaver。 Dreamweaver更新了他们的脚本,为用户提供了通过单击某些按钮轻松制作流体布局的方法。它将自动生成脚本文件和css文件。如果您像我一样是手动编码器,那么只需使用Dreamweaver生成流体布局并开始手动编码即可。
希望这可以帮助您 :)

1
顺便说一句,它确实有帮助。 - Rainb
很高兴能帮助到你 :) - PICKAB00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接