[英]Shrink container to image width
我目前正在開發一個網站 ,用戶應該可以在其中水平滾動瀏覽具有可點擊信息點的景觀。 網站需要完全響應,並確保我想將我的風景圖像和信息點放在具有完全相同大小的圖像的容器中。
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工作完全正常,直到您調整瀏覽器窗口的高度,然后信息點移動到錯誤的位置..但是當您刷新調整大小的網站時它再次正常工作。
試一試: 小提琴 (調整輸出大小,然后再點擊“運行”。)
在我的真實項目中,我使用javascript將容器的寬度設置為圖像的寬度,但我希望有一個干凈的CSS解決方案。
我知道周圍有類似的問題,但沒有一個建議的解決方案適合我。
先感謝您!
使用vw insted of%調整下面的代碼:
<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;
}
注意:請參閱本網站的響應式編碼標准:
首先,永遠不要使用位置:固定; 在CSS上除非你想讓div或容器保持在同一個地方,即使你滾動。 其次,我建議使用jquery進行流暢的布局。 流暢的布局是什么,即使您調整網站大小,它也會將頁面內容保持在同一位置。 如果您不想浪費時間編寫長腳本文件,我個人建議您使用Dreamweaver。 Dreamweaver更新了他們的腳本,只需單擊一些按鈕,即可讓用戶輕松地制作流暢的布局。 它會自動為您生成腳本文件和css。 如果您是像我這樣的手動編碼器,那么只需使用Dreamweaver生成流暢的布局並手動開始編碼。
希望這可以幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.