Smooth Scrolling of html pages using iScroll.js - BEHIND JAVA

Smooth Scrolling of html pages using iScroll.js

Share This

iScroll is a high performance, small footprint, dependency free, multi-platform javascript scroller. It can handle any element that needs to be moved with user interaction. It adds scrolling, zooming, panning, infinite scrolling, parallax scrolling, carousels to your projects and manages to do that in just 4kb. Give it a broom and it will also clean up your office.

Programing with iScroll.js

To implement the iScroll magic in your html page, you must initialize the IScroll javascript class in you html page. IScroll is a class that needs to be initiated for each scrolling area. There's no limit to the number of iScrolls you can have in each page if not that imposed by the device CPU/Memory.

The iScroll needs to be initiated when the DOM is ready. The safest bet is to start it on window onload event. DOMContentLoaded or inline initialization are also fine but remember that the script needs to know the height/width of the scrolling area. If you have images that don't have explicit width/height declaration, iScroll will most likely end up with a wrong scroller size. Here follows a very simple example of iScroll.js

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">

<title>iScroll Behindjava Demo</title>

<script type="text/javascript" src="iscroll.js"></script><!-- Here you need to specify the iscroll.js location -->
<script type="text/javascript">

var myScroll;
function loaded () {
 myScroll = new IScroll('#wrapper');
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

</script>

<style>
body {
 /* On modern browsers, prevent the whole page to bounce */
 overflow: hidden;
}

#wrapper {
 position: relative;
 width: 300px;
 height: 300px;
 overflow: hidden;

 /* Prevent native touch events on Windows */
 -ms-touch-action: none;

 /* Prevent the callout on tap-hold and text selection */
 -webkit-touch-callout: none;
 -webkit-user-select: none;
 -moz-user-select: none;
 -ms-user-select: none;
 user-select: none;

 /* Prevent text resize on orientation change, useful for web-apps */
 -webkit-text-size-adjust: none;
 -moz-text-size-adjust: none;
 -ms-text-size-adjust: none;
 -o-text-size-adjust: none;
 text-size-adjust: none;
}

#scroller {
 position: absolute;

 /* Prevent elements to be highlighted on tap */
 -webkit-tap-highlight-color: rgba(0,0,0,0);

 /* Put the scroller into the HW Compositing layer right from the start */
 -webkit-transform: translateZ(0);
 -moz-transform: translateZ(0);
 -ms-transform: translateZ(0);
 -o-transform: translateZ(0);
 transform: translateZ(0);
}
</style>

</head>
<body onload="loaded()">

<div id="wrapper">
 <div id="scroller">
  <p><strong>This demo shows the minimum CSS/HTML/JS configuration you need to run the iScroll. Look at the source code for details.</strong></p>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</div>

</body>
</html>

No comments:

Post a Comment

Pages