私が達成しようとしているのは、同じ画像を使用して、background-position
をjQueryと設定して、最も近い親と重複するようにbackground-image
です。私は何とかして、$element.property().left
は2
に近い何かを乗算する必要があると思っています(なぜそれが本当に理解できないのかまだ分かりませんが)。要素の位置に基づいて正確な背景位置を設定する方法は?
誰かが私に方程式を正確に伝えることができたら、それは大きな助けになるでしょう。私は、padding
とmargin
の要素と、関連するDOMツリー上のすべての要素があると思いますが、多くの組み合わせの後にはまだそこに到達できません。
希望の効果を得るには、background: transparent;
を設定するのが最善の方法だと思われるかもしれませんが、はそうではありません。;要素の背景画像にCSSプロパティのfilter
プロパティをさらに使用するために必要です。
テスト用にHTMLにインラインスタイルが追加されています。また、jsfiddleを作成しました。ここで
$.fn.filteredImg= function() {
var $this = $(this)
$this.each(function(index, card) {
var $card = $(card);
var img = $card.data("img");
var $parent = $card.parentsUntil("[data-img='" + img + "']").last().parent();
var $effect = $card.children(".filtered-effect");
var pos = $card.position();
$parent.css({
backgroundImage: "url(" + img + ")",
backgroundRepeat: "no-repeat"
});
$effect.css({
backgroundImage: "url(" + img + ")",
backgroundPosition: -2 * Math.ceil(pos.left) + "px " + -Math.round(pos.top) + "px"
});
})
}
$(".card-filtered-img").filteredImg();
.card-filtered-img {
width: 80%;
min-height: 500px;
margin-left: 77px;
margin-top: 17px;
position: relative;
}
.card-filtered-img > * {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.card-filtered-img .filtered-effect {
z-index: 99;
}
.card-filtered-img .card-content {
background: rgba(34, 34, 34, 0.35);
z-index: 100;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-img="http://www.planwallpaper.com/static/images/colorful-wallpaper2.jpg">
<div class="container" style="margin-top:30px">
<div class="row" style="padding:30px">
<div class="col">
<div class="card-filtered-img" data-img="http://www.planwallpaper.com/static/images/colorful-wallpaper2.jpg">
<div class="filtered-effect"></div>
<div class="card-content"></div>
</div>
<div class="card-filtered-img" data-img="http://www.planwallpaper.com/static/images/colorful-wallpaper2.jpg">
<div class="filtered-effect"></div>
<div class="card-content"></div>
</div>
</div>
</div>
</div>
なぜ画像を使用して?なぜあなたは '透明な'背景を使用し、 '不透明度 'で遊んでいないのですか? –
たとえば、透明な背景と不透明度を持つ 'filter:blur(5px)'を使用することはできません。 – wscourge
[this](http://jsfiddle.net/QvSng/192/)のようなものを試すこともできます。 –