iscroll.js 学习笔记

1.0 什么是iscroll.js?

就是一个Javascript 库,或者说是一个JQuery 插件,能在手机端实现下拉刷新,滚动翻页,双指放大缩小等特效。

可以模拟原生iOS应用里的滚动列表操作。

下载地址:https://github.com/cubiq/iscroll/

 

2.0为什么要用iscroll.js ?

css属性overflow:scroll在ios5之前是不支持的,所以可以通过这个插件来在所有浏览器上支持这种滚动。

 

3.0如何使用iscroll,js?

直接看demo:

https://github.com/cubiq/iscroll/tree/master/demos

 

简单来说就是

1.引入iscroll.js, 定义myScroll,内容里面div 父id写wrapper,子写scroller

 

<!DOCTYPE html>
<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 demo: scroll to element</title>

<script type="text/javascript" src="iscroll.js"></script>

<script type="text/javascript">
var myScroll;
function loaded () {
    myScroll = new IScroll(#wrapper, { mouseWheel: true, click: true });
}
document.addEventListener(touchmove, function (e) { e.preventDefault(); }, false);
</script>

<style type="text/css">
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
html {
    -ms-touch-action: none;
}
body,ul,li {
    padding: 0;
    margin: 0;
    border: 0;
}
body {
    font-size: 12px;
    font-family: ubuntu, helvetica, arial;
    overflow: hidden; /* this is important to prevent the whole page to bounce */
}
#header {
    position: absolute;
    z-index: 2;
    top: 0;
    left: 0;
    width: 100%;
    height: 45px;
    line-height: 45px;
    background: #CD235C;
    padding: 0;
    color: #eee;
    font-size: 20px;
    text-align: center;
    font-weight: bold;
}
#footer {
    position: absolute;
    z-index: 2;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 48px;
    background: #444;
    padding: 0;
    border-top: 1px solid #444;
}
#wrapper {
    position: absolute;
    z-index: 1;
    top: 45px;
    bottom: 48px;
    left: 0;
    width: 100%;
    background: #ccc;
    overflow: hidden;
}
#scroller {
    position: absolute;
    z-index: 1;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    width: 100%;
    -webkit-transform: translateZ(0);
    -moz-transform: translateZ(0);
    -ms-transform: translateZ(0);
    -o-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -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 ul {
    list-style: none;
    padding: 0;
    margin: 0;
    width: 100%;
    text-align: left;
}
#scroller li {
    padding: 0 10px;
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #fff;
    background-color: #fafafa;
    font-size: 14px;
}
</style>
</head>
<body onload="loaded()">
<div id="header">iScroll</div>

<div id="wrapper">
    <div id="scroller">
        <ul>
            <li><a href="javascript:myScroll.scrollToElement(document.querySelector(‘#scroller li:nth-child(10)‘))">Scroll to element 10</a></li>
            <li>Pretty row 2</li>
            <li>Pretty row 3</li>
            <li>Pretty row 4</li>
            <li>Pretty row 5</li>
            <li>Pretty row 6</li>
            <li>Pretty row 7</li>
            <li>Pretty row 8</li>
            <li>Pretty row 9</li>
            <li><a href="javascript:myScroll.scrollToElement(document.querySelector(‘#scroller li:nth-child(25)‘), null, null, true)">Center element 25 to screen</a></li>
            <li>Pretty row 11</li>
            <li>Pretty row 12</li>
            <li>Pretty row 13</li>
            <li>Pretty row 14</li>
            <li>Pretty row 15</li>
            <li>Pretty row 16</li>
            <li>Pretty row 17</li>
            <li>Pretty row 18</li>
            <li>Pretty row 19</li>
            <li>Pretty row 20</li>
            <li>Pretty row 21</li>
            <li>Pretty row 22</li>
            <li>Pretty row 23</li>
            <li>Pretty row 24</li>
            <li><a href="javascript:myScroll.scrollToElement(document.querySelector(‘#scroller li:nth-child(50)‘), 1200, null, null, IScroll.utils.ease.elastic)">Scroll to the last element with elastic easing</a></li>
            <li>Pretty row 26</li>
            <li>Pretty row 27</li>
            <li>Pretty row 28</li>
            <li>Pretty row 29</li>
            <li>Pretty row 30</li>
            <li>Pretty row 31</li>
            <li>Pretty row 32</li>
            <li>Pretty row 33</li>
            <li>Pretty row 34</li>
            <li>Pretty row 35</li>
            <li>Pretty row 36</li>
            <li>Pretty row 37</li>
            <li>Pretty row 38</li>
            <li>Pretty row 39</li>
            <li>Pretty row 40</li>
            <li>Pretty row 41</li>
            <li>Pretty row 42</li>
            <li>Pretty row 43</li>
            <li>Pretty row 44</li>
            <li>Pretty row 45</li>
            <li>Pretty row 46</li>
            <li>Pretty row 47</li>
            <li>Pretty row 48</li>
            <li>Pretty row 49</li>
            <li>Pretty row 50</li>
        </ul>
    </div>
</div>
<div id="footer"></div>
</body>
</html>

 

wrapper翻译是包装,理解为容器

scroller翻译是卷纸,理解为滚动条

 

position: absolute; 绝对定位 :

父级没有position,就以浏览器左上角为原始点进行

父级有position的话,就以父级的左上角来进行定位

 

position:relative; 相对定位 :

以父级的内容左上角来进行定位,

 

 

===========================================

每次写网页最好都在meta 标签里面添加一个  

<meta http-equiv="pragma" content="no-cache"/>

用来清理缓存

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。