博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iPhone Scrollbars with iScroll
阅读量:7052 次
发布时间:2019-06-28

本文共 7402 字,大约阅读时间需要 24 分钟。

Since we've had web browsers and JavaScript, we've been intent on replacing native browser functionalities, and for many reasons. Whether it be that the native look or functionality is ugly, doesn't work the same across browsers, or isn't as feature-rich as it should be, we've always pushed the browser's limits to do better. One functionality we've tried desperately to emulate is scrollbars. , a fairly new scrolling lib, has done an exceptional job in emulating scrolling both within desktop and mobile browsers. iScroll also allows for scrolling of elements with overflow on older versions of Mobile Safari. Let's have a look at iScroll!

The HTML

iScroll requires a two-DIV pattern for declaring where iScroll will be used. The first DIV is the wrapper, the second DIV is the scrollable area:

 

 

iScroll will create and inject the scrollbar within the wrapper DIV. The content is held within the scroller DIV.

The CSS

The CSS is where iScroll can get a bit fuzzy. For iScroll to work optimally, both the wrapper and scroller DIVs need to be positioned absolutely and be styled to widths of 100%:

#wrapper {position:absolute;z-index:1;top:0; bottom:0; left:0;width:100%;overflow:auto;}#scroller {position:absolute; z-index:1;/*    -webkit-touch-callout:none;*/-webkit-tap-highlight-color:rgba(0,0,0,0);width:100%;padding:0;}

 

 

As a result, the third DIV of the structure needs the contain enough padding-right to keep the text and scrollbar far enough way from each other. Be sure to position those elements properly or iScroll wont work at all (as I found out the hard way) !

The JavaScript

The most obvious piece of using iScroll is including its .js file:

 

 

With iScroll now available within the page, the next step is creating the iScroll instance that suits the needs of your desired usage. The most simple of uses provides only the wrapper ID:

var scroller = new iScroll('wrapper');

 

Awesome; the page's native scrollbar disappears and is replaced by an iOS-style scrollbar! But like every good JavaScript lib, iScroll provides a host of features that allow you to customize your scrollbar. Options include setting flags for bounce, momentum, fade and hide settings, and whether scrollbars should be allowed both vertically and horizontally. Here's another example of how you can create a pull-to-refresh scrollbar:

var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset,generatedCount = 0;function pullDownAction () {setTimeout(function () {    // <-- Simulate network congestion, remove setTimeout from production!var el, li, i;el = document.getElementById('thelist');for (i=0; i<3; i++) {li = document.createElement('li');li.innerText = 'Generated row ' + (++generatedCount);el.insertBefore(li, el.childNodes[0]);}myScroll.refresh();        // Remember to refresh when contents are loaded (ie: on ajax completion)}, 1000);    // <-- Simulate network congestion, remove setTimeout from production!}function pullUpAction () {setTimeout(function () {    // <-- Simulate network congestion, remove setTimeout from production!var el, li, i;el = document.getElementById('thelist');for (i=0; i<3; i++) {li = document.createElement('li');li.innerText = 'Generated row ' + (++generatedCount);el.appendChild(li, el.childNodes[0]);}myScroll.refresh();        // Remember to refresh when contents are loaded (ie: on ajax completion)}, 1000);    // <-- Simulate network congestion, remove setTimeout from production!}function loaded() {pullDownEl = document.getElementById('pullDown');pullDownOffset = pullDownEl.offsetHeight;pullUpEl = document.getElementById('pullUp');    pullUpOffset = pullUpEl.offsetHeight;myScroll = new iScroll('wrapper', {useTransition: true,topOffset: pullDownOffset,onRefresh: function () {if (pullDownEl.className.match('loading')) {pullDownEl.className = '';pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';} else if (pullUpEl.className.match('loading')) {pullUpEl.className = '';pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';}},onScrollMove: function () {if (this.y > 5 && !pullDownEl.className.match('flip')) {pullDownEl.className = 'flip';pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';this.minScrollY = 0;} else if (this.y < 5 && pullDownEl.className.match('flip')) {pullDownEl.className = '';pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';this.minScrollY = -pullDownOffset;} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {pullUpEl.className = 'flip';pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';this.maxScrollY = this.maxScrollY;} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {pullUpEl.className = '';pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';this.maxScrollY = pullUpOffset;}},onScrollEnd: function () {if (pullDownEl.className.match('flip')) {pullDownEl.className = 'loading';pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';                pullDownAction();    // Execute custom function (ajax call?)} else if (pullUpEl.className.match('flip')) {pullUpEl.className = 'loading';pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';                pullUpAction();    // Execute custom function (ajax call?)}}});setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);}

 

Since we live in the world of AJAX-driven websites that allow content to come and go, calling the refresh method is all you need to do for iScroll to reevaluate the scrollbar position and size:

// When the AJAX is done, refresh the scrollbar sizing and positioning...scroller.refresh();

 

It's also important to point out that iScroll allows zooming and pinching, as well as snapping to elements:

var myScroll = new iScroll('wrapper', {/* snap: true, */         // Would snap logicallysnap: "p",                // Snaps to each "P" tagmomentum: false,hScrollbar: false,vScrollbar: false });

 

Lastly, iScroll-Lite is available for those looking to support only mobile browsers (iScroll allows for desktop support as well). The mischievous part of me would prefer iOS-style scrollbars instead of native browser scrollbars!

Possibly my favorite part of iScroll is that it's a standalone library; no external JavaScript library is required. iScroll has many configuration parameters so I encourage you to to check out everything you can do. Matteo Spinelli's iScroll is an outstanding piece of work; grab iScroll and start controlling your scrollbars today!

 

 

原文 

     this is a demo .

 

   
上拉下拉动态加载例子
    
        
            
Pull down to refresh...         
        
                 
            
Pull up to refresh...         
    
    
            
                    
  • 首页
  •                                  
        

 

 

 

demo  下载

 

转载地址:http://bhsol.baihongyu.com/

你可能感兴趣的文章
leetcode 443. String Compression
查看>>
在没联网环境下,启动tomcat出错
查看>>
关于Git bash-127.0.0.7:8888拒绝访问的小问题--环境变量
查看>>
有价值的数据
查看>>
LayUi超级好用的前端工具
查看>>
[Ubuntu] ubuntu的tty下挂载移动硬盘拷贝数据
查看>>
犯了个低级错误
查看>>
Outlook Anywhere 客户端配置详解
查看>>
IOS在Xcode 4.x以上如何 创建 和 添加 静态库
查看>>
Repeater控件数据导出Excel
查看>>
下载Android源码出现的问题
查看>>
远程桌面如何复制本地文件 远程桌面拷贝电脑上的文件方法
查看>>
[转]解决JS浮点数(小数)计算加减乘除的BUG
查看>>
ASP.NET MVC应用程序的安全性介绍总括(高级编程)
查看>>
记录下,我们平时开发当中不得不知道的HTTP状态码
查看>>
HDU-1045 Fire NetFire Net 最大团
查看>>
Eclipse自动部署项目到Tomcat的webapps下的有效方法
查看>>
Extjs4快速上手三——实现主界面
查看>>
如何替代Flex?
查看>>
控制属性修改时间,控制时间,联合主键
查看>>