Giter VIP home page Giter VIP logo

tiny-router's Introduction

Tiny Router

This is a small library in ES6 to do basic routing for sites and apps. No for use with other frameworks such as angular or view but it may work. This is a standalone routing library and will work with other libraries.

It will automatically change routes based on the hash and you can add views directly into the HTML.

It's around 1kb in file size and very simple to use.

How to use

  1. Include the tiny-router library into your html file
<script src="tiny-router.min.js"></script>
  1. In the HTML add a div with the data attribute call data-router and provide your router with an id so you can access it.
<div data-router id="main-router">
  1. In your js code wait till the page has loaded and create a new instance of the Router and provide the router element as the parameter.
let router = new Router(document.querySelector('#main-router'));
  1. Within the router element you can add your routes/views by specifiying data-route data attribute with the route (for example data-route="#/"). Those elements will be hidden but you should add some css to ensure they do not show on load. Use visibility style to hide them as this is what this library uses at present.
<div data-router id="main-router">
 <section data-route="/">
   ...
 </section>
 <section data-route="/home">
   ...
 </section>
</div>

Router methods

routeTo(string route)

This method will allow you to navigate to another route.

let router = new Router(document.querySelector('#main-router'));
router.routeTo('#/home');

Change Transition Animation Style

If you wish to change the tranistion style the you can do this by adding in a string of the tranisition style as the second parameter in the Router class.

let router = new Router(document.querySelector('#main-router'), 'visibility 0s, opacity 0.5s linear');

tiny-router's People

Contributors

fahimc avatar

Watchers

 avatar  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.