0
https://codepen.io/carcleo/pen/JjbYQWa
In the code below (and also in the codepen above) I have 2 div’s where I’m doing an effect Parallax.
But I need to make the effect happen with background opaque. 30% opacity and I can’t get that directly into the div without also reaching the content.
So I’m doing in the ::before of div. But the background doesn’t appear at all.
Could you please help me understand?
$('.prlx::before').each(function() {
var obj = $(this);
obj.css('background-position', '50% 0');
obj.css('background-attachment', 'fixed');
$(window).scroll(function() {
var offset = obj.offset();
var yPos = -($(window).scrollTop() - offset.top) / 10;
var bgpos = obj.css('background-position-x') + ' ' + yPos + 'px';
obj.css('background-position', bgpos);
});
});
#detalhes {
position: relative;
width: 100vw;
height: 600px;
}
#contato {
position: relative;
width: 100vw;
height: 800px;
}
#detalhes::before {
content:'';
width: 100%;
height: 100%;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: .2;
background-image: url("https://i.stack.imgur.com/vxxg4.jpg") ;
background-size: cover;
}
#contato::before {
content:'';
width: 100%;
height: 100%;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: .2;
background-image: url("https://i.stack.imgur.com/D0Mls.jpg") ;
background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="detalhes" class="prlx"></div>
<div id="contato" class="prlx"></div>
I don’t think it’s possible to select this type of element
:before
,:after
your selector =>'.prlx::before'
, you will have to manipulate in another way– Icaro Martins
Related link in stackoverflow en - Selecting and manipulating CSS pseudo-Elements such as :before and ::after using javascript (or jQuery)
– Icaro Martins