Giter VIP home page Giter VIP logo

touchscroll's Introduction

touchscroll

A standalone and jQuery plugin to handle NATIVE scroll on NATIVE types of mobile web applications

Features

  • Locks your web page scrolling
  • Adds native scrolling to whatever element you want
  • Supports delegated scrolling
  • Prevents any dragging beyond boundaries
  • Superfast native scrolling!
  • Add touchable elements with delegation

What to be aware of

Attach touchscroll to elements you are going to keep around through the cycle of your application. Backbone view elements is an example of that. If you are changing DOM content, attach the touchscroll to a higher order DOM element, even the body could have the touchscroll, and then use delegation instead.

Sizing scrollable areas can be quite difficult. We recommend to use the CSS calc method. See examples below. We also recommend to use box-sizing: border-box, when adding borders and padding to elements that are part of the calculated areas. Also example below.

How to use

jQuery: $('#myEl').touchscroll(ARGS);

normal: touchscroll(document.getElementById('myEl'), ARGS);

Examples will be with jQuery.

Add script. Be sure to add it after jQuery, if you want to use jQuery

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script src="jquery.js"></script>
    <script src="touchscroll.js"></script>
  </body>
</html>

The DOM structure

The scroller needs two elements. A wrapper and the element that is the list. This is a typical DOM structure, with css:

<!DOCTYPE html>
<html>
  <head>
    <style>
      html, body {
        margin: 0;
        height: 100%;
        overflow: hidden;
      }
      #wrapper {
        height: 100%; /* Locks the wrapper to 100% height of body */
        overflow: hidden; /* Makes sure the list element is hidden inside wrapper */
      }
    </style>
  </head>
  <body>
    <div id="wrapper">
      <div class="list"></div>
    </div>
  </body>
</html>

Attach scroll to a DOM node directly

This will make the content of the DIV natively scrollable

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="wrapper">
      <div id="list"></div> 
    </div>
    <script src="jquery.js"></script>
    <script src="touchscroll.js"></script>
    <script>
      $('#list').touchscroll();
    </script>
  </body>
</html>

Attach scroll to DOM node with delegation

In this example it will be safe to empty the content of main and add a new list element inside it.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="wrapper">
      <!-- Using CLASS instead -->
      <div class="list"></div>
    </div>
    <script src="jquery.js"></script>
    <script src="touchscroll.js"></script>
    <script>
      $('#wrapper').touchscroll('.list');
    </script>
  </body>
</html>

Listen to touch on elements and trigger method. The second argument is the element that was touched

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="wrapper">
      <div class="list"></div>
    </div>
    <script src="jquery.js"></script>
    <script src="touchscroll.js"></script>
    <script>
      var openItem = function (event, item) {
        console.log('My item ' + item);
      };
    
      $('#wrapper').touchscroll('.list', {
        '.item': openItem
      });
    </script>
  </body>
</html>

How to use it on a Backbone view

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="wrapper">
      <div class="list"></div>
    </div>
    <script src="jquery.js"></script>
    <script src="touchscroll.js"></script>
    <script src="underscore.js"></script>
    <script src="backbone.js"></script>
    <script>
        var MyView = Backbone.View.extend({
          el: '#wrapper',
          initialize: function () {
            this.$el.touchscroll('.list', {
              '.item': this.openItem.bind(this)
            });
          },
          openItem: function (event, item) {
            // Do something
          }
        
        });
    </script>
  </body>
</html>

Sizing a scrollable area

Note that if you are using LESS, it will compile the calculations in the calc method, which means the final output will be "calc(0)" in this case. To avoid LESS to do the calculation, you can escape it like this: "height: ~"calc(100% - 100px)";

<!DOCTYPE html>
<html>
  <head>
    <style>
        html, body {
          height: 100%;
          margin: 0;
        }
        #header {
          height: 100px;
        }
        #wrapper {
          height: calc(100% - 100px);
          overflow: hidden;
        }
    </style>
  </head>
  <body>
    <div id="header">Header</div>
    <div id="wrapper">
      <div class="list"></div>
    </div>
  </body>
</html>

Never use border or padding on scroll elements themselves. Do that in a wrapper. This has to do with calculations of the scroll.

Handling border and padding on calculated elements

In this example we use the css property "box-sizing: border-box" to safely still use 100% -100px since both the border and the padding will be part of the headers total height.

<!DOCTYPE html>
<html>
  <head>
    <style>
        html, body {
          height: 100%;
          margin: 0;
        }
        #header {
          height: 100px;
          border: 2px solid #000;
          padding: 10px;
          box-sizing: border-box;
        }
        #wrapper {
          height: calc(100% - 100px);
          overflow: hidden;
        }
    </style>
  </head>
  <body>
    <div id="header">Header</div>
    <div id="wrapper">
      <div class="list"></div>
    </div>
  </body>
</html>

TIPS

Remove all user selection boxes

* {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}

META tags to display content correctly on mobile devices

<!DOCTYPE html>
<html>
  <head>
      <meta name="viewport" content="width=device-width, user-scalable=0, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
    <meta name="msapplication-tap-highlight" content="no"/>
    <meta name="apple-mobile-web-app-capable" content="yes">
  </head>
  <body>
  
  </body>
</html>

touchscroll's People

Contributors

christianalfoni avatar mokr avatar

Watchers

 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.