I am trying to change only the alpha channel in the background-color of a div element(using a slider). But I don't know how to do It. Here is my code:
$(function() {
$( "#slider" ).slider({
range: "min",
value: 50,
min: 0,
max: 100,
slide: function( event, ui ) {
// While sliding, update the value in the alpha div element
$('#idOfMyDiv').css('background', rgb(,,,ui.value) ); //I don't know what to do here
}
});
});
Like this:
var newAlpha=0.50;
var bg=$('#idOfMyDiv').css('backgroundColor');
var a=bg.slice(4).split(',');
var newBg='rgba('+a[0]+','+parseInt(a[1])+','+parseInt(a[2])+','+newAlpha+')';
$('#idOfMyDiv').css('backgroundColor',newBg);
Example code and a Demo:
var newAlpha=0.50;
var bg=$('body').css('backgroundColor');
var a=bg.slice(4).split(',');
var newBg='rgba('+a[0]+','+parseInt(a[1])+','+parseInt(a[2])+','+newAlpha+')';
$('body').css('backgroundColor',newBg);
body{ background-color: orange; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With