I am trying to change the background opacity of a div using a jquery ui slider.
The user can change the background color of the div, so I need to know how I can only change the alpha parameter of the background without changing the color.
This is my code so far:
$('#opacity-slider').slider({ min: 0, max: 1, step: 0.1, value: 1 })
.bind("slidechange", function() {
//get the value of the slider with this call
var o = $(this).slider('value');
var e = $('.element-active');
var currentColor = $('.element-active').css('background-color');
$(e).css('background-color', 'rgba(255, 255, 255, '+o+')')
});
I need to some how change only the alpha part of the currentColor variable. I hope someone can help me! TIA
Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.
So how do you use RGBA? rgba (100%, 0%, 0%, 1); The fourth value denotes alpha and needs to be between 0.0 (absolute transparency) and 1.0 (absolute opacity). For example, 0.5 would be 50% opacity and 50% transparency.
Transparency using RGBA In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
There's no CSS property that you can use to change the opacity of only the background image. Unlike background colors, which allow you to adjust the alpha channel to control opacity, it simply doesn't exist for the background-image property.
Try this:
var alpha = $(this).slider('value');
var e = $('.element-active');
$(e).css('background-color', 'rgba(255,255,255,' + alpha + ')');
Updated Example Here
String manipulation might be the best way to go:
var e = $('.element-active');
var currentColor = $('.element-active').css('background-color');
var lastComma = currentColor.lastIndexOf(',');
var newColor = currentColor.slice(0, lastComma + 1) + o + ")";
$(e).css('background-color', newColor);
I managed to solve this by learning from and adapting the answer suggested by @Tom Smilack, using string manipulation.
I made a small change so that his answer would also work for divs that do not have alpha set originally, here is the final code that I am using:
$('#opacity-slider').slider({ min: 0, max: 1, step: 0.1, value: 1 })
.bind("slidechange", function() {
//get the value of the slider with this call
var o = $(this).slider('value');
var e = $('.element-active');
var currentColor = $('.element-active').css('background-color');
var lastComma = currentColor.lastIndexOf(')');
var newColor = currentColor.slice(0, lastComma - 1) + ", "+ o + ")";
$(e).css('background-color', newColor);
});
Thanks to everyone for the suggestions and I hope this helps people wanting the same funcitonality
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