Giter Site home page Giter Site logo

viewporter's Introduction

Zynga Viewporter v2.0

Viewporter is a open-source JavaScript project by Zynga to ease mobile viewport management. It specifically simplifies the part of setting up the right screen dimensions and removes the pain from handling the viewport meta tag manually.

What does it do?

When put into the header of a page and when running a mobile device, Viewporter will first try to scroll away any URL or debug bars to maximize the visible window, and then substracts the remaining chrome/UI height from the window, effectively removing ugly scrollbars along the way. It will also track orientationchange, thus, you will always have a maximized viewing experience.

How to use?

In v1, all you had to to was to put Viewporter into the head of the page. There's just a little bit more to do in v2, but it isn't painful:

Add the following meta viewport to the of your page:

<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0" />

Wrap your element with the viewporter wrapper div:

<body>
<div id="viewporter">
	...
</div>
</body>

That's it, really! Feel free to have a look at the demo pages if something doesn't work as expected.

What's wrong with doing it manually?

You could of course try to set the viewport meta tag yourselves, as suggested in various places, usually something like <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">. You will quickly recognize two apparent issues:

  • proportional device-height doesn't subtract the chrome height, so the window is always larger than the viewport when set, causing scrollbars even on empty pages
  • rotating the device will cause the page to zoom (as device-width isn't inverted on rotation)
  • even with a manually fixed viewport, there's a stupid gap at the bottom of the page (when using absolutely positioned elements)

Advantages of using Viewporter

So what's in it for you? There's a couple of automatic advantages for you when the Viewporter is running. Here's a list:

  • Maximized viewport (scrolling away unneeded UI)
  • Easy layouting

Easy layouting?

Yep. Take a <div>, position it absolutely, set its width and height to "50%", left and bottom to 0 and the background to any color. With Viewporter enabled, it will be placed at the bottom left corner of the window, and stretch to the middle of the window. Sounds obvious right? It isn't really, when you want a maximized window.

API

Viewporter is almost zero configuration. There's only one constant to check if Viewporter is in fact running, a convienience method to detect landscape orientation and a smart ready callback function. In addition, there's a couple of events you will likely want to use.

Constants

  • z.viewporter.ACTIVE

Methods

  • z.viewporter.isLandscape() - returns wether the device is rotated to landscape or not
  • z.viewporter.ready() - accepts a callback and fires it when the viewporter has been successfully executed

Events

All events fire as native events on the window object.

  • viewportready - fires as soon as the Viewporter has been executed for the first time
  • viewportchange - fires when the viewport changes, i.e. the device is rotated, and after Viewporter has been executed again

viewporter's People

Contributors

pbakaus avatar

Stargazers

Jeremy Sarda avatar

Watchers

Jeremy Sarda avatar James Cloos 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.