初识better-scroll:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. body{
    10. margin: 0;
    11. }
    12. body,html{
    13. height: 100%;
    14. }
    15. ul,ol{
    16. list-style: none;
    17. padding: 0;
    18. margin: 0;
    19. }
    20. .con{
    21. width: 100vw;
    22. height: 100vh;
    23. /* 外框一定要设置高度 如果不设置 将没有效果 */
    24. /* background: green; */
    25. overflow: hidden;
    26. }
    27. /* 横向滑屏 */
    28. ul {
    29. /* width: 150vw; */
    30. }
    31. ul>li{
    32. width: 100%;
    33. height: 40px;
    34. line-height: 40px;
    35. text-indent: 20px;
    36. border-bottom: 1px solid #666666;
    37. color: #333333;
    38. }
    39. </style>
    40. </head>
    41. <body>
    42. <div class="con">
    43. <ul class="list">
    44. </ul>
    45. </div>
    46. </body>
    47. <script type="text/javascript" src="https://unpkg.com/better-scroll@1.0.1/dist/bscroll.min.js"></script>
    48. <script>
    49. function getData(){
    50. var list = document.querySelector('.list');
    51. var html = '';
    52. for(var i=0;i<30;i++){
    53. html += '<li>我是第' + (i+1) + '个li</li>' ;
    54. }
    55. list.innerHTML = html;
    56. }
    57. window.onload = function(){
    58. getData();
    59. con();
    60. }
    61. function con(){
    62. var con = document.querySelector('.con');
    63. var bscroll = new BScroll(con, {
    64. scrollX: false,
    65. scrollY: true,
    66. momentum: true
    67. });
    68. }
    69. </script>
    70. </html>

    轮播图:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. body{
    10. margin: 0;
    11. }
    12. body,html{
    13. height: 100%;
    14. }
    15. ul{
    16. list-style: none;
    17. margin: 0;
    18. padding: 0;
    19. }
    20. .con{
    21. width: 100vw;
    22. /* height: 100vh; */
    23. overflow: hidden;
    24. position: relative;
    25. }
    26. .list{
    27. width: 400vw;
    28. overflow: hidden;
    29. position: relative;
    30. }
    31. .list li{
    32. width: 100vw;
    33. height: 200px;
    34. float: left;
    35. text-align: center;
    36. box-sizing: border-box;
    37. font:24px/200px "微软雅黑";
    38. background: chocolate;
    39. color: white;
    40. /* border:1px solid green; */
    41. }
    42. .nav{
    43. position: absolute;
    44. bottom: 10px;
    45. left: 0;
    46. width: 100vw;
    47. text-align: center;
    48. }
    49. .nav a{
    50. display: inline-block;
    51. width: 12px;
    52. height: 12px;
    53. line-height: 12px;
    54. background: white;
    55. }
    56. .nav a.active{
    57. background: orange;
    58. }
    59. </style>
    60. </head>
    61. <body>
    62. <div class="con">
    63. <ul class="list">
    64. <li>我是li1</li>
    65. <li>我是li2</li>
    66. <li>我是li3</li>
    67. <li>我是li4</li>
    68. </ul>
    69. <nav class="nav">
    70. <a class="active"></a>
    71. <a></a>
    72. <a></a>
    73. <a></a>
    74. </nav>
    75. <!-- 布局后如何切换点 -->
    76. </div>
    77. <script type="text/javascript" src="https://unpkg.com/better-scroll@1.0.1/dist/bscroll.min.js"></script>
    78. <script>
    79. window.onload = ()=>{
    80. var con = document.querySelector(".con");
    81. var list = document.querySelector(".list");
    82. var navs = document.querySelectorAll(".nav a");
    83. var bscroll = new BScroll(con, {
    84. scrollX: true,
    85. scrollY: false,
    86. momentum: false,
    87. snap:{
    88. // 滑动的距离为屏幕的本分之及之后,换下一张
    89. threshold: 0.2
    90. // loop: true
    91. }
    92. });
    93. bscroll.on('scrollEnd', (e)=>{
    94. // DOM刷新不及时的情况
    95. bscroll.refresh();
    96. console.log(bscroll.getCurrentPage());
    97. var index = bscroll.getCurrentPage().pageX;
    98. navs.forEach((nav) => {
    99. nav.classList.remove("active");
    100. });
    101. navs[index].classList.add('active');
    102. });
    103. };
    104. </script>
    105. </body>
    106. </html>

    上滑加载:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    6. <title>Document</title>
    7. <style>
    8. </style>
    9. <style>
    10. body{
    11. margin: 0;
    12. }
    13. body,html{
    14. height: 100%;
    15. }
    16. ul,ol{
    17. list-style: none;
    18. padding: 0;
    19. margin: 0;
    20. }
    21. .con{
    22. width: 100vw;
    23. height: 100vh;
    24. overflow: hidden;
    25. }
    26. /* 横向滑屏 */
    27. ul{
    28. /* width: 150vw; */
    29. position: relative;
    30. }
    31. ul>li{
    32. width: 100%;
    33. height: 40px;
    34. line-height: 40px;
    35. text-indent: 20px;
    36. border-bottom: 1px solid #666666;
    37. color: #333333;
    38. }
    39. </style>
    40. </head>
    41. <body>
    42. <div class="con">
    43. <ul class="list">
    44. </ul>
    45. </div>
    46. </body>
    47. <script type="text/javascript" src="./bscroll.js"></script>
    48. <script>
    49. var list = document.querySelector(".list");
    50. //数据加载
    51. function getData(){
    52. var html = "";
    53. for(var i = 0; i < 20; i++){
    54. html += "<li>我是第"+(i+1)+"个li</li>"
    55. }
    56. list.innerHTML = html;
    57. }
    58. //数据加载
    59. function getNewData(type){
    60. var html = "";
    61. for(var i = 0; i < 10; i++){
    62. html += "<li>我是新加载第"+(i+1)+"个li</li>"
    63. }
    64. if(type === "up"){
    65. list.innerHTML += html;
    66. }else{
    67. list.innerHTML = html + list.innerHTML;
    68. }
    69. }
    70. window.onload = () =>{
    71. getData();
    72. intCon();
    73. }
    74. function intCon(){
    75. var con = document.querySelector(".con");
    76. var bscroll = new BScroll(con, {
    77. scrollX: false,
    78. scrollY: true,
    79. momentum: true,
    80. //上划加载
    81. pullUpLoad:{
    82. // 底部还剩50px的时候,进行这个操作
    83. threshold: 50
    84. },
    85. pullDownRefresh:{
    86. threshold: -50,
    87. stop: 30
    88. }
    89. });
    90. bscroll.on('pullingUp', ()=>{
    91. setTimeout(()=>{
    92. getNewData('up');
    93. bscroll.finishPullUp();
    94. bscroll.refresh();
    95. }, 1000);
    96. });
    97. bscroll.on('pullingDown', ()=>{
    98. console.log(222);
    99. setTimeout(()=>{
    100. getNewData('down');
    101. //下来动作结束
    102. bscroll.finishPullDown();
    103. bscroll.refresh();
    104. }, 1000);
    105. });
    106. }
    107. </script>
    108. </html>

    城市列表示例:

    1. (function(){
    2. function $getClass(classes){
    3. return document.querySelector(classes);
    4. }
    5. // 获取容器
    6. var list = $getClass('.list-wrapper');
    7. // 右侧导航
    8. var indexListNav = $getClass('.index-list-nav');
    9. // 导航ul下所有的li
    10. var indexListNavs = indexListNav.querySelectorAll('li');
    11. // 顶部索引
    12. var indexListfixed = $getClass('.index-list-fixed');
    13. var indexListContent = $getClass('.index-list-content');
    14. // 内容ul下所有的li
    15. var indexLists = indexListContent.children[1].children;
    16. var indexList = new BScroll(list,{
    17. // 事件派发
    18. probeType:3
    19. });
    20. indexList.on('scroll',(e)=>{
    21. var y= -e.y;
    22. // 排除定位城市,未超过第一项就不显示顶部
    23. if (y < indexLists[0].offsetTop){
    24. setNav(0);
    25. indexListfixed.style.display = 'none';
    26. return;
    27. }
    28. // 过了第一项,显示它
    29. indexListfixed.style.display = 'block';
    30. for(var i=0; i < indexLists.length-1; i++){
    31. // 滚动距离是否大于当前项,并且小于下一项,如果是,则说明是这项
    32. if (y >= indexLists[i].offsetTop && y < indexLists[i+1].offsetTop) {
    33. setNav(i);
    34. indexListfixed.innerHTML= indexLists[i].children[0].innerHTML;
    35. return;
    36. }
    37. }
    38. //最后一项不满足i+1的条件
    39. setNav(indexLists.length-1);
    40. indexListfixed.innerHTML= indexLists[indexLists.length-1].children[0].innerHTML;
    41. });
    42. indexListNav.addEventListener('touchstart',(e)=>{
    43. setIndex(e.changedTouches[0].clientY);
    44. });
    45. indexListNav.addEventListener('touchmove',(e)=>{
    46. setIndex(e.changedTouches[0].clientY);
    47. });
    48. // 设置坐标
    49. function setIndex(y){
    50. let index = getIndex(y);
    51. if (index < 0 || index > 8){
    52. return;
    53. }
    54. // scrollToElement 为插件自带的API,滚动到哪里(那个元素)
    55. indexList.scrollToElement(indexLists[index],1000);
    56. // 加上样式
    57. setNav(index);
    58. }
    59. //获取坐标
    60. function getIndex(y){
    61. var navTop = indexListNav.getBoundingClientRect().top;
    62. // li的高度
    63. var h = 18;
    64. var index = parseInt((y - navTop)/18);
    65. return index;
    66. }
    67. // li active
    68. function setNav(index){
    69. indexListNavs.forEach((li) => {
    70. li.classList.remove('active');
    71. });
    72. indexListNavs[index].classList.add('active');
    73. }
    74. })(window);
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>城市列表</title>
    8. <style type="text/css">
    9. ul {
    10. margin: 0;
    11. padding: 0;
    12. list-style: none;
    13. }
    14. .list-wrapper{
    15. position: absolute;
    16. left: 0;
    17. top: 0;
    18. right: 0;
    19. bottom: 0;
    20. overflow: hidden;
    21. background: #fff;
    22. }
    23. .index-list-content{
    24. background: #fff;
    25. border-radius: 2px;
    26. }
    27. .index-list-title{
    28. padding: 14px 16px;
    29. font-size: 14px;
    30. line-height: 1.6;
    31. color: #333;
    32. }
    33. .index-list-anchor{
    34. padding: 16px 16px 10px 16px;
    35. line-height: 1;
    36. font-size: 14px;
    37. color: #999;
    38. background: #f7f7f7;
    39. }
    40. .index-list-item {
    41. position: relative;
    42. height: 50px;
    43. line-height: 50px;
    44. padding: 0 16px;
    45. font-size: 14px;
    46. color: #333;
    47. }
    48. .index-list-item:last-child{
    49. border: none;
    50. }
    51. .index-list-item_active{
    52. background: #ddd;
    53. }
    54. .index-list-fixed{
    55. position: absolute;
    56. z-index: 1;
    57. top: 0;
    58. left: 0;
    59. width: 100%;
    60. padding: 16px 16px 10px 16px;
    61. box-sizing: border-box;
    62. font-size: 14px;
    63. line-height: 1;
    64. color: #999;
    65. background: #f7f7f7;
    66. }
    67. .index-list-nav {
    68. position: absolute;
    69. z-index: 30;
    70. right: 0;
    71. top: 50%;
    72. transform: translateY(-50%);
    73. font-family: Helvetica;
    74. }
    75. .index-list-nav ul {
    76. padding: 0;
    77. margin: 0;
    78. }
    79. .index-list-nav li{
    80. padding: 6px 16px 0 16px;
    81. line-height: 1;
    82. text-align: center;
    83. box-sizing: border-box;
    84. font-size: 12px;
    85. color: gray;
    86. }
    87. .index-list-nav .active {
    88. color: blue;
    89. }
    90. </style>
    91. </head>
    92. <body>
    93. <div class="list-wrapper">
    94. <div class="scroll-content">
    95. <div class="index-list-content">
    96. <div class="index-list-title">
    97. 定位城市:
    98. </div>
    99. <ul>
    100. <li>
    101. <h2 class="index-list-anchor">
    102. ★热门城市
    103. </h2>
    104. <ul>
    105. <li class="index-list-item border-bottom-1px">
    106. 北京市
    107. </li>
    108. <li class="index-list-item border-bottom-1px">
    109. 上海市
    110. </li>
    111. </ul>
    112. </li>
    113. <li>
    114. <h2 class="index-list-anchor">
    115. A
    116. </h2>
    117. <ul>
    118. <li class="index-list-item border-bottom-1px">
    119. 鞍山市
    120. </li>
    121. <li class="index-list-item border-bottom-1px">
    122. 安庆市
    123. </li>
    124. </ul>
    125. </li>
    126. <li>
    127. <h2 class="index-list-anchor">
    128. B
    129. </h2>
    130. <ul>
    131. <li class="index-list-item border-bottom-1px">
    132. 北京市
    133. </li>
    134. <li class="index-list-item border-bottom-1px">
    135. 巴音郭楞州
    136. </li>
    137. <li class="index-list-item border-bottom-1px">
    138. 博尔塔拉州
    139. </li>
    140. </ul>
    141. </li>
    142. <li>
    143. <h2 class="index-list-anchor">
    144. C
    145. </h2>
    146. <ul>
    147. <li class="index-list-item border-bottom-1px">
    148. 成都市
    149. </li>
    150. </ul>
    151. </li>
    152. <li>
    153. <h2 class="index-list-anchor">
    154. E
    155. </h2>
    156. <ul>
    157. <li class="index-list-item border-bottom-1px">
    158. 鄂尔多斯市
    159. </li>
    160. <li class="index-list-item border-bottom-1px">
    161. 鄂州市
    162. </li>
    163. <li class="index-list-item border-bottom-1px">
    164. 恩施州
    165. </li>
    166. </ul>
    167. </li>
    168. <li>
    169. <h2 class="index-list-anchor">
    170. F
    171. </h2>
    172. <ul>
    173. <li class="index-list-item border-bottom-1px">
    174. 福州市
    175. </li>
    176. <li class="index-list-item border-bottom-1px">
    177. 佛山市
    178. </li>
    179. <li class="index-list-item border-bottom-1px">
    180. 防城港市
    181. </li>
    182. </ul>
    183. </li>
    184. <li>
    185. <h2 class="index-list-anchor">
    186. G
    187. </h2>
    188. <ul>
    189. <li class="index-list-item border-bottom-1px">
    190. 广州市
    191. </li>
    192. <li class="index-list-item border-bottom-1px">
    193. 贵阳市
    194. </li>
    195. </ul>
    196. </li>
    197. <li>
    198. <h2 class="index-list-anchor">
    199. H
    200. </h2>
    201. <ul>
    202. <li class="index-list-item border-bottom-1px">
    203. 杭州市
    204. </li>
    205. <li class="index-list-item border-bottom-1px">
    206. 和田地区
    207. </li>
    208. </ul>
    209. </li>
    210. <li>
    211. <h2 class="index-list-anchor">
    212. Z
    213. </h2>
    214. <ul>
    215. <li class="index-list-item border-bottom-1px">
    216. 郑州市
    217. </li>
    218. <li class="index-list-item border-bottom-1px">
    219. 张家口市
    220. </li>
    221. <li class="index-list-item border-bottom-1px">
    222. 张家界市
    223. </li>
    224. <li class="index-list-item border-bottom-1px">
    225. 珠海市
    226. </li>
    227. <li class="index-list-item border-bottom-1px">
    228. 中山市
    229. </li>
    230. <li class="index-list-item border-bottom-1px">
    231. 自贡市
    232. </li>
    233. <li class="index-list-item border-bottom-1px">
    234. 资阳市
    235. </li>
    236. <li class="index-list-item border-bottom-1px">
    237. 枣庄市
    238. </li>
    239. <li class="index-list-item border-bottom-1px">
    240. 舟山
    241. </li>
    242. <li class="index-list-item border-bottom-1px">
    243. 遵义市
    244. </li>
    245. <li class="index-list-item border-bottom-1px">
    246. 淄博市
    247. </li>
    248. <li class="index-list-item border-bottom-1px">
    249. 株洲市
    250. </li>
    251. <li class="index-list-item border-bottom-1px">
    252. 中卫市
    253. </li>
    254. </ul>
    255. </li>
    256. </ul>
    257. </div>
    258. </div>
    259. </div>
    260. <div class="index-list-nav">
    261. <ul>
    262. <li data-index="0" class="active">
    263. </li>
    264. <li data-index="1">
    265. A
    266. </li>
    267. <li data-index="2" class="">
    268. B
    269. </li>
    270. <li data-index="3" class="">
    271. C
    272. </li>
    273. <li data-index="4" class="">
    274. E
    275. </li>
    276. <li data-index="5" class="">
    277. F
    278. </li>
    279. <li data-index="6" class="">
    280. G
    281. </li>
    282. <li data-index="7" class="">
    283. H
    284. </li>
    285. <li data-index="8" class="">
    286. Z
    287. </li>
    288. </ul>
    289. </div>
    290. <div class="index-list-fixed" style="display: none;">
    291. A
    292. </div>
    293. <script type="text/javascript" src="./bscroll.js"></script>
    294. <script type="text/javascript" src="./city.js"></script>
    295. </body>
    296. </html>