npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jq-cssvar

v1.0.1

Published

a jQuery plugin that allows the use of CSS variables in JS

Downloads

3

Readme

cssVar - A jQuery plugin for handling CSS variables in Javascript

CSS variables are new to CSS3 and have many advantages over their prepocessors counterparts, such as being accessible in Javascript.

However accessing those variables in pure javascript tend to be an open door to very repetitive boilerplate, which is why I decided to created this plugin for jQuery.

It is originally a plugin from my other library (lightquery, currently in-dev), I just ported it to comply with jQuery's plugin syntax.


How to install/use ?

Witout NPM

  <head>
    <!-- [...] -->

    <script src="path/to/jquery"></script>
    <script src="path/to/the/cssVar/plugin"></script>

    <!-- [...] -->
  </head>

Once that is done, you can use cssVar in any script loaded after the plugin.

With NPM

npm install jq-cssvar
//We'll suppose that jQuery is already loaded (script tag or require) as $
require("path/to/the/cssVar/plugin")($);
//now loaded

Accessing variables

:root{
  --variable-name: 42;
}

To access this variable, we simply use

jQuery.cssVar("--variable-name");  //returns 42
jQuery.cssVar("variable-name"); //equivalent, also returns 42

Variable name selector

Because all CSS variable must start with "--", I made it possible to use the variables' names without "--". Therefore,

jQuery.cssVar("--x");

and

jQuery.cssVar("x");

are strictly equivalent.

Modifying global variables

:root{
  --var: 404
}

Just like any other plugin of jQuery, cssVar adopts the getter/setter syntax, therefore we can modify this variable like so :

jQuery.cssVar("var"); //returns 404
jQuery.cssVar("var", 42); //returns jQuery for chaining
jQuery.cssVar("var"); //returns 42

Non-global (instance) variables

Even though using only global CSS variables is the best use case, sometimes you just need to have non-global variables/modify those variables locally. Therefore cssVar is also ready for this.

:root{
  --var1: 404;
}

.class{
  --var1: 42;
  --var2: 40;
}
jQuery(".class").cssVar("var1"); //returns 42
jQuery(".class").cssVar("var2"); //returns 40
jQuery.cssVar("var1"); //returns 404

jQuery(".class").eq(2).cssVar("var1", 41);
jQuery(".class").eq(0).cssVar("var1"); //returns 42
jQuery(".class").eq(2).cssVar("var1"); //returns 41

jQuery(".class").cssVar("var1", 440);
jQuery(".class").eq(0).cssVar("var1"); //returns 440
jQuery(".class").eq(2).cssVar("var1"); //returns 440

Specifications

  • The getter only retrieves the variable's value from the first element of the matched set
  • The setter sets the variable's value for each element of the matched set
  • CSS variables only accept numbers and strings, those are the only types of values that you can set those variables to
  • If an error occurs, it returns what the method was called on (the matched set if instance, jquery if global)
  • All modifications you can do to those variables follow the same rules as if they were done in CSS (cf. How to use CSS variables (MDN))