Access css block resource from jquery

I have a site that is heavily based on javascript, but I still want to let it easily modify CSS.
Take this example ... there is a div that has its opacity changed when you hover over jQuery, but I would like the designer to be able to choose the opacity value.
I was thinking of creating a block in css just to declare these constants, for example:

.constants_someid{
  opacity: 0.5;
}

and then through jQuery I use this opacity like

 var opacity = jQuery('css:constants_someid').attr('opacity');

I donโ€™t know if this is a good way to make this declaration of constants, but thatโ€™s what it came to my mind right now.

What do you guys think?

Thanks
Joe

+3
source share
3 answers

Javascript:

var settings = {
    opacity: 0.5
};

, . settings.opacity.

$.extend, , , :

var defaults = {
    opacity: 0.9,
    speed: 500
}

settings = $.extend({}, defaults, settings);
// returns { opacity: 0.5, speed: 500 }
+2

@lonesomdays , . ;

function getConfig(className,property)
{
    var dummy = $('<span class="' + className + '" />');
    var value = dummy.appendTo('body').css(property);
    dummy.remove();
    return value;
}

, getConfig('constants_someid','opacity')

: http://jsfiddle.net/5GWHU/

+2

CSS , , , .

, CSS constants. / jQuery.

, CSS :

.faded-out {
   opacity: 0.5;
}

.highlight {
   background: yellow;
}

jQuery:

jQuery('.someElements').addClass('faded-out');
jQuery('.otherElements').removeClass('highlight');
0

Source: https://habr.com/ru/post/1787945/


All Articles