JAVASCRIPT
CSS Utilities and toolbox
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
Function name | Description |
---|---|
.addClassName(elm, className) | Adds a class to a given element |
.addRemoveClassName(elm, className, addRemState) | Adds of removes a class. |
.appendStyleTag(selector, style, options) | Injects style tags with rules to the page. |
.appendStylesheet(path, options) | Injects an external link tag. |
.appendStylesheetCb(cssURI, [callback]) | Injects an external link tag. |
.blinkClass(element, className, timeout, negate) | Blinks a class from an element |
.changeFontSize(selector, delta, [op], [minVal], [maxVal]) | Change the font size of elements. |
.decToHex(dec) | Converts decimal to hexadecimal values |
.getPropertyFromStylesheet(selector, property) | Get a single property from a stylesheet. |
.getStyle(elm, style) | Gets the value for an element's style attribute |
.hasClassName(elm, className, [all]) | Checks if an element has a class. |
.hexToDec(hex) | Converts hexadecimal values to decimal |
.hide(elm) | Hides an element. |
.removeClassName(elm, className) | Removes a class from a given element |
.setClassName(elm, className, [add]) | Alias to addRemoveClassName. |
.setOpacity(elm, value) | Sets the opacity of given element |
.setStyle(elm, style) | Adds CSS rules to an element's style attribute. |
.show(elm, [forceDisplayProperty]) | Shows an element. |
.showHide(elm, [show]) | Shows or hides an element. |
.toggle(elm, forceShow) | Toggles an element visibility. |
.toggleClassName(elm, className, [forceAdd]) | Toggles a class name from a given element |
Adds a class to a given element
elm
Element or element idclassName
Class or classes to add. Examples: 'my-class', ['my-class', 'other-class'], 'my-class other-class'Code
<div id="container">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('container');
InkCss.addClassName(elm, 'newclass');
InkCss.addClassName(elm, ['class1', 'class2']);
Ink.log(elm);
// <div id="container" class="newclass class1 class2">
});
</script>
Adds of removes a class.
Depending on addRemState, this method either adds a class if it's true or removes if if false.
elm
DOM element or element idclassName
class name to add or remove.addRemState
Whether to add or remove. `true` to add, `false` to remove.Code
<div id="elm1">
Element content
</div>
<div id="elm2" class="class2">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm1 = Ink.i('elm1');
var elm2 = Ink.i('elm2');
InkCss.addRemoveClassName(elm1, 'class1', true);
InkCss.addRemoveClassName(elm2, 'class2', false);
Ink.log(elm1);
// <div id="elm1" class="class1">
Ink.log(elm2);
// <div id="elm2" class="">
});
</script>
Injects style tags with rules to the page.
selector
The css selector for the rulestyle
The content of the style ruleoptions
Options for the tagoptions.type
'text/css'File typeoptions.force
falseIf true, the style tag will be appended to end of headCode
<!-- original markup -->
<html>
<head>
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
InkCss.appendStyleTag('#elm', 'border: 5px solid #000;');
});
</script>
<!-- result markup -->
<html>
<head>
<style type="text/css">
#elm {
border: 5px solid #000;
}
</style>
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
Injects an external link tag.
This method add a stylesheet to the head of a page
path
File pathoptions
Options for the tagoptions.media
'screen'Media typeoptions.type
'text/css'File typeoptions.force
falseIf true, tag will be appended to end of headCode
<!-- original markup -->
<html>
<head>
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
InkCss.appendStylesheet('https://example.com/css/file.css');
});
</script>
<!-- result markup -->
<html>
<head>
<link rel="stylesheet" href="https://example.com/css/file.css" type="text/css" media="screen">
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
Injects an external link tag.
Loads CSS via LINK element inclusion in HEAD (skips append if already there)
Works similarly to appendStylesheet but:
supports optional callback which gets invoked once the CSS has been applied
cssURI
URI of the CSS to load, if empty ignores and just calls back directlycallback
optional callback which will be called once the CSS is loadedCode
<!-- original markup -->
<html>
<head>
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
InkCss.appendStylesheetCb('https://example.com/css/file.css', function() {
Ink.log('CSS is ready...');
});
});
</script>
<!-- result markup -->
<html>
<head>
<link rel="stylesheet" href="https://example.com/css/file.css" type="text/css" media="screen">
</head>
<body>
<div id="elm">
Element content
</div>
</body>
</html>
Blinks a class from an element
Add and removes the class from the element with a timeout, so it blinks
element
DOM element or element idclassName
Class name(s) to blinktimeout
timeout in ms between adding and removing, default 100 msnegate
is true, class is removed then addedCode
<div id="elm1">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm1 = Ink.i('elm1');
InkCss.blinkClassName(elm1, 'newclass', 1000);
Ink.log(elm1);
// <div id="elm1" class="newclass">
// after 1 sec
// <div id="elm1">
});
</script>
Change the font size of elements.
Changes the font size of the elements which match the given CSS rule
For this function to work, the CSS file must be in the same domain than the host page, otherwise JS can't access it.
selector
CSS selector ruledelta
Number of pixels to change on font-sizeop
Supported operations are '+' and '*'. defaults to '+'minVal
If result gets smaller than minVal, change does not occurrmaxVal
If result gets bigger than maxVal, change does not occurrConverts decimal to hexadecimal values
Useful to convert colors to their hexadecimal representation.
dec
Either a single decimal value, an rgb(r, g, b) string or an Object with r, g and b propertiesCode
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var a = InkCss.decToHex(51);
Ink.log(a);
// 33
var b = InkCss.decToHex({r:255, g: 0, b: 100});
Ink.log(b);
// FF0064
});
Get a single property from a stylesheet.
Use this to obtain the value of a CSS property (searched from loaded CSS documents)
selector
a CSS rule. must be an exact matchproperty
a CSS propertyGets the value for an element's style attribute
elm
DOM element or element idstyle
Which css attribute to fetchCode
<style type="text/css">
.classrule {
color: #FF0000;
font-size:12px;
}
</style>
<div id="elm" class="classrule">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
var color = InkCss.getStyle(elm, 'color');
var fontSize = InkCss.getStyle(elm, 'font-size');
Ink.log(color); // rgb(255, 0, 0) )
Ink.log(fontSize); // 12px
});
</script>
Checks if an element has a class.
This method verifies if an element has ONE of a list of classes. If the last argument is flagged as true, instead checks if the element has ALL the classes
elm
DOM element or element idclassName
Class name(s) to testall
falseIrrelevant if only one `className` is passed. If `true`, check if the element contains ALL the CSS classes. If `false`, check whether the element contains ANY of the given classes.Code
<div id="container" class="classname">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('container');
InkCss.hasClassName(elm, 'newclass'); // false
InkCss.hasClassName(elm, 'classname'); // true
});
</script>
Converts hexadecimal values to decimal
Useful to use with CSS colors
hex
hexadecimal Value with 6, 3, 2 or 1 charactersCode
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var a = InkCss.hexToDec('#CCC');
Ink.log(a);
// {r: 204, g: 204, b: 204}
var b = InkCss.hexToDec('#FF0033');
Ink.log(b);
// {r: 255, g: 0, b: 51}
});
Hides an element.
elm
DOM element or element idCode
<div id="elm">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.hide(elm);
Ink.log(elm);
// <div id="elm" style="display: none;">
});
</script>
Removes a class from a given element
elm
DOM element or element idclassName
Class names to remove. You can either use a space separated string of classnames, comma-separated list or an arrayCode
<div id="container" class="oldclass">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('container');
InkCss.removeClassName(elm, 'oldclass');
Ink.log(elm);
// <div id="container">
});
</script>
Alias to addRemoveClassName.
Utility function, saves many if/elses.
elm
DOM element or element idclassName
Class names to add\remove. Comma separated, space separated or simply an Arrayadd
falseFlag to switch behavior from removal to addition. true to add, false to removeSets the opacity of given element
elm
DOM element or element idvalue
allows 0 to 1(default mode decimal) or percentage (warning using 0 or 1 will reset to default mode)Code
<div id="elm">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.setOpacity(elm, 0.5);
Ink.log(elm);
// <div id="elm" style="opacity:0.5;">
});
</script>
Adds CSS rules to an element's style attribute.
elm
DOM element or element idstyle
Which css attribute to setCode
<div id="elm">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.setStyle(elm, 'color:#000; font-size:20px;');
Ink.log(elm);
// <div id="elm" style="color: rgb(0, 0, 0); font-size: 20px;">
});
</script>
Shows an element.
Internally it unsets the display property of an element. You can force a specific display property using forceDisplayProperty
elm
DOM element or element idforceDisplayProperty
Css display property to apply on showCode
<div id="elm" style="display:none;">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.show(elm);
Ink.log(elm);
// <div id="elm" style="">
InkCss.show(elm, 'block');
Ink.log(elm);
// <div id="elm" style="display:block;">
});
</script>
Shows or hides an element.
If the show parameter is true, it shows the element. Otherwise, hides it.
elm
DOM element or element idshow
falseWhether to show or hide `elm`.Code
<div id="elm">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.showHide(elm, true);
Ink.log(elm);
// <div id="elm" style="">
InkCss.showHide(elm, false);
Ink.log(elm);
// <div id="elm" style="display: none;">
});
</script>
Toggles an element visibility.
elm
DOM element or element idforceShow
Forces showing if element is hiddenCode
<div id="elm">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm = Ink.i('elm');
InkCss.toggle(elm);
Ink.log(elm);
// <div id="elm" style="display: none;">
InkCss.toggle(elm);
Ink.log(elm);
// <div id="elm" style="">
});
</script>
Toggles a class name from a given element
elm
DOM element or element idclassName
Class nameforceAdd
Flag to force adding the the classe names if they don't exist yet.Code
<div id="elm1">
Element content
</div>
<script>
Ink.requireModules(['Ink.Dom.Css_1'], function(InkCss){
var elm1 = Ink.i('elm1');
InkCss.toggleClassName(elm1, 'newclass');
Ink.log(elm1);
// <div id="elm1" class="newclass">
InkCss.toggleClassName(elm1, 'newclass');
Ink.log(elm1);
// <div id="elm1" class="">
});
</script>