GithubHelp home page GithubHelp logo

tomhodgins / slinky Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 1.0 10 KB

Add JavaScript tests to <link> tags to toggle stylesheets

Home Page: https://tomhodgins.github.io/slinky

License: MIT License

HTML 79.61% JavaScript 19.03% CSS 1.36%
css-reprocessor element-queries container-queries media-queries responsive rwd css

slinky's Introduction

slinky

Add JavaScript tests to <link> tags to toggle stylesheets

Similar to how you can add media queries to <link> tags with the media="" attribute, Slinky lets you define a custom data-selector="" and data-test="" attributes to any <link> tag to define an element to watch, and a JavaScript test to evaluate from the context of the matching element.

A simple usage might toggle the display of a stylesheet when the #sidebar element is wider than 500px:

<link
  rel="stylesheet"
  href="sidebar.css"
  media="none"
  data-selector="#sidebar"
  data-test="this.offsetWidth > 500"
>

By default Slinky will add event listeners for window.load, window.resize, window.input and window.click, but you can override this by optionally specifying a data-event="" attribute as well. This stylesheet would listen to window.scroll alone:

<link
  rel="stylesheet"
  href="scroll.css"
  media="none"
  data-selector="body"
  data-test="this.scrollTop > innerHeight"
  data-event="scroll"
>

Note that in these examples I've included media="none" so the stylesheets default to being hidden - for progressive enhancement if you wish to default to the stylesheets applying unless they need to be hidden by JS, you can leave the media="none" off and the stylesheet will load by default, instead of remain hidden by default.

Links

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.