Giter Site home page Giter Site logo

joaocunha / vunit Goto Github PK

View Code? Open in Web Editor NEW
104.0 7.0 65.0 305 KB

:triangular_ruler: A lightweight alternative to the buggy vh/vw CSS units.

Home Page: http://joaocunha.github.io/vunit/

License: MIT License

JavaScript 40.10% CSS 24.00% HTML 35.90%

vunit's Introduction

vUnit

vUnit is a vanilla JS microlib (~1000 bytes after gzip) that allows you to size elements based on the viewport dimensions, without relying on the buggy vh/vw/vmin/vmax CSS units. See a live example.

4x4 panel, 50% height and width: 4x4 panel, 50% height and width

How to use, in 3 steps

First: install using bower or npm:

bower install vunit or npm install vunit.js

Second: add the script to the <head> tag and instantiate vUnit passing a CSSMap object:

<head>
	<!-- Add vUnit.js to the head to avoid FOUC -->
	<script src="path/to/vunit.js"></script>

	<!-- Instantiate vUnit.js passing a CSSMap with properties you want to play with -->
	<script>
		new vUnit({
			CSSMap: {
				// The selector (VUnit will create rules ranging from .selector1 to .selector100)
				'.vh_height': {
					// The CSS property (any CSS property that accepts px as units)
					property: 'height',
					// What to base the value on (vh, vw, vmin or vmax)
					reference: 'vh'
				},
				// Wanted to have a font-size based on the viewport width? You got it.
				'.vw_font-size': {
					property: 'font-size',
					reference: 'vw'
				},
				// vmin and vmax can be used as well.
				'.vmin_margin-top': {
					property: 'margin-top',
					reference: 'vmin'
				}
			},
			onResize: function() {
				console.log('A screen resize just happened, yo.');
			}
		}).init(); // call the public init() method
	</script>
</head>
<body>
	<h1 class="vw_font-size15">This title font-size is 15% of the viewport width.</h1>
	<p class="vh_height50">This p's height is 50% of the viewport height.</p>
	<p class="vmin_margin-top5">This p has some margin-top<p>
</body>

Third: Add the generated classes to your HTML elements:

<h1 class="vw_font-size15">This title font-size is 15% of the viewport width.</h1>
<p class="vh_height50">This p's height is 50% of the viewport height.</p>
<p class="vmin_margin-top5">This p has some margin-top.<p>

You're done!

How it works

Viewport relative units are awesome, except they're not - they are buggy, unreliable and have inconsistent implementation across browsers. vUnit.js offers a lightweight, robust alternative for them and weighs ~600 bytes after gzip.

vUnit.js calculates the browser viewport dimensions and creates CSS rules ranging from 1% to 100% of its size. These rules are then inserted into a stylesheet which is injected on the fly to the <head> tag.

An observer running every 100ms checks if the viewport has been resized and regenerates the CSS rules accordingly. It's a cross-device, event-less solution to keep track of everything that would trigger a resize on the viewport, namely:

  • Window resizing on desktop;
  • Orientation change on mobile;
  • Scrollbars appearing/disappearing on desktop;
  • Navigation bars appearing/disappearing on mobile;
  • Zooming on mobile and desktop;
  • Download bar on desktop;
  • Password saving prompt on desktop;
  • Etc.

Pro tips

  • Load vUnit on the <head> tag to avoid FOUC.
  • Add a CSS transition on mobile, so it doesn't jitter as the address bar appears/disappears.
  • vUnit is pretty fast, but avoid bloating your CSSMap with properties you won't gonna use.
  • vUnit is not supposed to replace your grid, but to enhance your design.
  • Always consider non-JS users.

Browser support

So far, tested on:

  • IE8+
  • Chrome
  • Firefox
  • Safari
  • Chrome for Android
  • Android Browser 4.3

More to come.

TODO

  • The issues page is your friend.

vunit's People

Contributors

joaocunha avatar josepedrodias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vunit's Issues

*Feature* -- Adding support by Property to Chrome Bug

There is a known bug than chrome set a Margin.

The way to fix it is by setting inside body a margin:0;, so I think than this awsome library could implement a property inside .init() for set margin:0; auto inside body tag ; something like

chromeBugSupport:true.

Regards

Add support to decimal values / floats

Actual code does not provide a right way to set decimal vh or vw values.
Sometimes is required because content needs to be adjusted in not integer values.

I suggest something like this:

.vw_font-size15 -- As actual code does 15% of vw.
.vw_font-size15_35 -- As 15.35% of vw.

The fork button on your site is redirecting to a 404

Hey i found your amazing js lib but i tried to fork it from the fork button on your site and it redirects to a 404 maybe wanna check that out and fix that :D (ok edit if your not logged in to gihub) maybe add clone button or just the bower install thingy

Padding problem

Hey
If two elements arranged vertically, on top of each other, have a padding bottom and padding top respectively, it does not adjusts the height accordingly. The bottom element clips a little.

You can also confirm this by adding only padding-bottom to the top element only.

Thanks.

Performance Problem

I was testing and I feel a big impact in the performance in one of my apps and I think is because of the calculation of the measurements and attach it to the head.

I feel than that calcs needs to be on fly after document ready..

Font-size properties

Hey there--

Love vunit! But there is a pretty large feature that seems to be missing from it. Why can't I use font-size properties other than just vw? The ability to size text on vh, vmin or vmax would be extremely helpful!! Anytime I try to manually add the properties in my CSSMap, vunit won't load.

Are these features planned to be implemented? Is there just something I'm doing wrong?

Thanks for your help!

Can vunit overcome the problems that surround treating a page segment as a viewport?

There is an issue that I have been trying to overcome which is that VW/VH units become completely useless as soon as you are working with any kind of split screen/pane. I've been looking for a solution where one segment of this split can be treated as it's own viewport, but the only real solution is using iframes. See a question about this on Stack Overflow.

jsFiddle use the iFrames approach, which is how they able to have their screen area split 4 ways, but still be able to correctly display elements sized with vw and vh in the bottom right hand preview window see here.

Although the jsFiddle approach is precisely the behavior I want, splitting my application up into separate pages, so that they can be windowed separately within iFrames, just so I can use viewport measurements in each, seems rather extreme.. and so my question is...

Does vunit offer the flexibility to calculate it's units from a specific DIV container, or does it only calculate from the entire viewport?

Thanks

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.