Giter Club home page Giter Club logo

csstemplate.js's Introduction

CSSTemplate.js

Inject and update dynamic CSS using simple js templating

Usage

This script is intended to be used mostly in the <head> to enhance the main stylesheet. Add a style tag with an id. This is a good ol' CSS stylesheet with the sugar of placeholder $variables. Then include the script.

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
    <style id="css-tpl">
        body {
            font-size: $baseFontSize;
        }
        .full-height {
            height: $windowHeight;
        }
    </style>
    <script src="CSSTemplate.js"></script>
    <script>
        // Setup here
    </script>
</head>

Now you set the placeholder values by creating a new CSSTemplate instance pointing to our style tag.

var tpl = new CSSTemplate('css-tpl');
tpl.set({
    baseFontSize: (window.innerHeight / 5) + 'px',
    windowHeight: window.innerHeight + 'px'
});

Best usage would be creating a "refresh" function and attach to an event handler:

var tpl = new CSSTemplate('css-tpl');
function handler() {
    tpl.set({
        baseFontSize: (window.innerHeight > 320 ? window.innerHeight / 5 : 64) + 'px',
        windowHeight: window.innerHeight + 'px'
    });
}
document.addEventListener('resize', handler);
handler();

The minified script is small enough to be placed directly in the <head> without noticeable overhead.

Copyright and Licensing

Copyright (c) 2015 Nicolás Arias, released under the MIT license.

csstemplate.js's People

Contributors

ezakto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.