2017-04-06 21 views
1

我正在嘗試使我的div的背景顏色基於它包含的圖像發生變化。我已經看到類似的效果谷歌,Pinterest等等顏色顯示在圖像之前。優選多於一種顏色,可能爲梯度。有任何想法嗎?謝謝!基於圖像的頁面顏色背景

+0

你可以嘗試只使用圖片上的模糊濾鏡,並使用它模糊的背景,以及。 – nixkuroi

+1

您是否考慮過搜索效果並查看其背後的代碼? –

+1

一個快速的Google Seach會給你一個JavaScript插件列表來提取圖像的主色。然後你可以用它來爲你的div着色 – FrenchMajesty

回答

0

這可以通過圖像模糊來完成。

.container { 
 
    position:fixed; 
 
    width:200px; 
 
    height:150px; 
 
    border: 1px solid black; 
 
    border-radius: 2px; 
 
    overflow:hidden; 
 
} 
 

 
.background { 
 
    width: calc(100% + 40px); 
 
    margin-left: -40px; 
 
    margin-top: -40px 
 
    height:calc(100% + 40px); 
 
    position:absolute; 
 
    z-index:-1; 
 
    filter: blur(40px); 
 
} 
 

 
.foreground { 
 
    border:solid 1px black; 
 
    max-width:150px; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
}
<div class="container"> 
 
    <img class="background" src="http://neil.computer/stack/bg.jpg" /> 
 
    <img class="foreground" src="http://neil.computer/stack/bg.jpg" /> 
 
</div>

+0

完美工作。謝謝! – Maren