Is there a way to achieve a gradient effect using pseudo-element :before on an image that can be customized by adjusting a slider input? I've been trying to implement this feature with the following code, but have not been successful so far.
var sliderValue = $('#range');
sliderValue.oninput = function(){
var val1 = this.value;
var val2 = 100-val1;
$('<style> #filter:before{linear-gradient(to top, rgba(255,255,255,1) '+val1+'%,rgba(0,0,0,0) '+val2+'%);}</style>').appendTo("#filter");
};
.slider
{
-webkit-appearance:none;
appearance:none;
width: 100%;
height: 10px;
max-width: 400px;
background-color: #ccc;
margin: 0;
padding: 0;
outline: none;
border-radius: 10px;
cursor: pointer;
}
#filter {
position:relative;
float: left;
max-height: 480px;
}
#filter:before {
content: "";
position:absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
width:320px;
height:100%;
background: linear-gradient(to top, rgba(255,255,255,1) 15%,rgba(0,0,0,0) 22%);
z-index: 1;
}
<div class="container">
<input type="range" min="0" max="100" value="0" class="slider" id="range">
<div id="filter">
<img id="previewImg" src="img/dummy_320x480_ffffff_d002e2.png" alt="Placeholder" style="height: 100%; width:320px;">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>