Giter Club home page Giter Club logo

mobile-viewport-control's Introduction

This library is under active development. Please email us if you’re ready to use it in a production environment.

Mobile Viewport Control

This JavaScript library attempts to solve the hard problem of creating a full-window modal experience for mobile browsers. The goal is to allow it to be dropped into any page regardless of its viewport settings. It does this by attempting to control the mobile viewport and by hiding all elements except the one you are trying to modalize.

--

[![scroll][scroll-gif]][scroll-gfy] [![zoom][zoom-gif]][zoom-gfy]
__Figure 1.__ Scrolling __Figure 2.__ Zooming + Scrolling

--

Background. The viewport is the visible part of the webpage on your screen (see quirksmode [1] [2]). It is controlled by two things: scroll and zoom. The user can swipe to scroll and pinch to zoom. A JavaScript developer can control the scroll programmatically, but there is no officially supported method for controlling the zoom programmatically. However, we do have a de facto standard for limiting and initializing the zoom:

<head>
  <meta name="viewport" content="initial-scale=1">
  <meta name="viewport" content="minimum-scale=0">
  <meta name="viewport" content="maximum-scale=10">
</head>

Workaround. There is an undocumented feature that allows us to modify the viewport state at runtime by adding and modifying viewport tags. There are caveats depending on the browser. The goal of this library is to identify and workaround these caveats until there is a standard for programmatically controlling zoom. We have researched and tested on multiple platforms, though we are currently focusing support for Mobile Safari and WebViews on iOS 7,8,9. (See Compatibility Testing)

Modals are the Use-Case. Viewport settings vary widely across different webpages, making it hard to create drop-in modal experiences that work everywhere (see Stripe Checkout and Auth0 Lock). For browsers that cannot create pop-up tabs and for pages where redirection is not an option, this library can help fill in the gaps.

UPDATE: See the upcoming Payment Request API for native mobile checkout modals for the web.

--

[![freeze][freeze-gif]][freeze-gfy] [![isolate][isolate-gif]][isolate-gfy]
__Figure 3.__ Freezing and Thawing __Figure 4.__ Isolating the Modal

--

The Abstraction. This library provides a limited abstraction for controlling zoom: freezing and thawing. (See Figure 3). Freezing will lock the zoom to a given scale. Thawing will restore the viewport to what it was before freezing, while also restoring the appropriate zoom limits (i.e. min and max). Thawing in this way is useful in the context of a modal.

Isolating the Modal. The freeze/thaw functions are designed to be used in conjunction with a third operation: isolation. (See Figure 4). This is done by passing an optional element ID to the freeze function, which you would presumably wish to be the full-window modal. This temporarily applies CSS rules to ensure with high confidence that all elements except the given element are collapsed (via display:none). You can then style your element to fill the window as a modal. Thawing will restore the page back to what it was before.

Usage

npm install mobile-viewport-control
const viewport = require('mobile-viewport-control');
// ... or use `window.mobileViewportControl` if not used with package manager


// Freeze the viewport at a desired scale.
viewport.freeze(1.0, () => console.log('notified when frozen!'));

// Restore the viewport to what it was before freezing.
viewport.thaw(() => console.log('notified when thawed!'));

Additionally, to freeze the scroll area to a given element, you can pass an element ID as the second argument. We do this by temporarily hiding everything else on the page. Just make sure your element is a direct child of document.body.

viewport.freeze(1.0, 'myElementID');

Testing

Please see Compatibility Testing.

License

ISC License

--

Source code for the animated figures above: mobile-viewport-control-viz

mobile-viewport-control's People

Contributors

shaun-stripe avatar krithix avatar romainhuet avatar

Watchers

James Drew 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.