初识better-scroll:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body{
margin: 0;
}
body,html{
height: 100%;
}
ul,ol{
list-style: none;
padding: 0;
margin: 0;
}
.con{
width: 100vw;
height: 100vh;
/* 外框一定要设置高度 如果不设置 将没有效果 */
/* background: green; */
overflow: hidden;
}
/* 横向滑屏 */
ul {
/* width: 150vw; */
}
ul>li{
width: 100%;
height: 40px;
line-height: 40px;
text-indent: 20px;
border-bottom: 1px solid #666666;
color: #333333;
}
</style>
</head>
<body>
<div class="con">
<ul class="list">
</ul>
</div>
</body>
<script type="text/javascript" src="https://unpkg.com/better-scroll@1.0.1/dist/bscroll.min.js"></script>
<script>
function getData(){
var list = document.querySelector('.list');
var html = '';
for(var i=0;i<30;i++){
html += '<li>我是第' + (i+1) + '个li</li>' ;
}
list.innerHTML = html;
}
window.onload = function(){
getData();
con();
}
function con(){
var con = document.querySelector('.con');
var bscroll = new BScroll(con, {
scrollX: false,
scrollY: true,
momentum: true
});
}
</script>
</html>
轮播图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body{
margin: 0;
}
body,html{
height: 100%;
}
ul{
list-style: none;
margin: 0;
padding: 0;
}
.con{
width: 100vw;
/* height: 100vh; */
overflow: hidden;
position: relative;
}
.list{
width: 400vw;
overflow: hidden;
position: relative;
}
.list li{
width: 100vw;
height: 200px;
float: left;
text-align: center;
box-sizing: border-box;
font:24px/200px "微软雅黑";
background: chocolate;
color: white;
/* border:1px solid green; */
}
.nav{
position: absolute;
bottom: 10px;
left: 0;
width: 100vw;
text-align: center;
}
.nav a{
display: inline-block;
width: 12px;
height: 12px;
line-height: 12px;
background: white;
}
.nav a.active{
background: orange;
}
</style>
</head>
<body>
<div class="con">
<ul class="list">
<li>我是li1</li>
<li>我是li2</li>
<li>我是li3</li>
<li>我是li4</li>
</ul>
<nav class="nav">
<a class="active"></a>
<a></a>
<a></a>
<a></a>
</nav>
<!-- 布局后如何切换点 -->
</div>
<script type="text/javascript" src="https://unpkg.com/better-scroll@1.0.1/dist/bscroll.min.js"></script>
<script>
window.onload = ()=>{
var con = document.querySelector(".con");
var list = document.querySelector(".list");
var navs = document.querySelectorAll(".nav a");
var bscroll = new BScroll(con, {
scrollX: true,
scrollY: false,
momentum: false,
snap:{
// 滑动的距离为屏幕的本分之及之后,换下一张
threshold: 0.2
// loop: true
}
});
bscroll.on('scrollEnd', (e)=>{
// DOM刷新不及时的情况
bscroll.refresh();
console.log(bscroll.getCurrentPage());
var index = bscroll.getCurrentPage().pageX;
navs.forEach((nav) => {
nav.classList.remove("active");
});
navs[index].classList.add('active');
});
};
</script>
</body>
</html>
上滑加载:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Document</title>
<style>
</style>
<style>
body{
margin: 0;
}
body,html{
height: 100%;
}
ul,ol{
list-style: none;
padding: 0;
margin: 0;
}
.con{
width: 100vw;
height: 100vh;
overflow: hidden;
}
/* 横向滑屏 */
ul{
/* width: 150vw; */
position: relative;
}
ul>li{
width: 100%;
height: 40px;
line-height: 40px;
text-indent: 20px;
border-bottom: 1px solid #666666;
color: #333333;
}
</style>
</head>
<body>
<div class="con">
<ul class="list">
</ul>
</div>
</body>
<script type="text/javascript" src="./bscroll.js"></script>
<script>
var list = document.querySelector(".list");
//数据加载
function getData(){
var html = "";
for(var i = 0; i < 20; i++){
html += "<li>我是第"+(i+1)+"个li</li>"
}
list.innerHTML = html;
}
//数据加载
function getNewData(type){
var html = "";
for(var i = 0; i < 10; i++){
html += "<li>我是新加载第"+(i+1)+"个li</li>"
}
if(type === "up"){
list.innerHTML += html;
}else{
list.innerHTML = html + list.innerHTML;
}
}
window.onload = () =>{
getData();
intCon();
}
function intCon(){
var con = document.querySelector(".con");
var bscroll = new BScroll(con, {
scrollX: false,
scrollY: true,
momentum: true,
//上划加载
pullUpLoad:{
// 底部还剩50px的时候,进行这个操作
threshold: 50
},
pullDownRefresh:{
threshold: -50,
stop: 30
}
});
bscroll.on('pullingUp', ()=>{
setTimeout(()=>{
getNewData('up');
bscroll.finishPullUp();
bscroll.refresh();
}, 1000);
});
bscroll.on('pullingDown', ()=>{
console.log(222);
setTimeout(()=>{
getNewData('down');
//下来动作结束
bscroll.finishPullDown();
bscroll.refresh();
}, 1000);
});
}
</script>
</html>
城市列表示例:
(function(){
function $getClass(classes){
return document.querySelector(classes);
}
// 获取容器
var list = $getClass('.list-wrapper');
// 右侧导航
var indexListNav = $getClass('.index-list-nav');
// 导航ul下所有的li
var indexListNavs = indexListNav.querySelectorAll('li');
// 顶部索引
var indexListfixed = $getClass('.index-list-fixed');
var indexListContent = $getClass('.index-list-content');
// 内容ul下所有的li
var indexLists = indexListContent.children[1].children;
var indexList = new BScroll(list,{
// 事件派发
probeType:3
});
indexList.on('scroll',(e)=>{
var y= -e.y;
// 排除定位城市,未超过第一项就不显示顶部
if (y < indexLists[0].offsetTop){
setNav(0);
indexListfixed.style.display = 'none';
return;
}
// 过了第一项,显示它
indexListfixed.style.display = 'block';
for(var i=0; i < indexLists.length-1; i++){
// 滚动距离是否大于当前项,并且小于下一项,如果是,则说明是这项
if (y >= indexLists[i].offsetTop && y < indexLists[i+1].offsetTop) {
setNav(i);
indexListfixed.innerHTML= indexLists[i].children[0].innerHTML;
return;
}
}
//最后一项不满足i+1的条件
setNav(indexLists.length-1);
indexListfixed.innerHTML= indexLists[indexLists.length-1].children[0].innerHTML;
});
indexListNav.addEventListener('touchstart',(e)=>{
setIndex(e.changedTouches[0].clientY);
});
indexListNav.addEventListener('touchmove',(e)=>{
setIndex(e.changedTouches[0].clientY);
});
// 设置坐标
function setIndex(y){
let index = getIndex(y);
if (index < 0 || index > 8){
return;
}
// scrollToElement 为插件自带的API,滚动到哪里(那个元素)
indexList.scrollToElement(indexLists[index],1000);
// 加上样式
setNav(index);
}
//获取坐标
function getIndex(y){
var navTop = indexListNav.getBoundingClientRect().top;
// li的高度
var h = 18;
var index = parseInt((y - navTop)/18);
return index;
}
// li active
function setNav(index){
indexListNavs.forEach((li) => {
li.classList.remove('active');
});
indexListNavs[index].classList.add('active');
}
})(window);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>城市列表</title>
<style type="text/css">
ul {
margin: 0;
padding: 0;
list-style: none;
}
.list-wrapper{
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
background: #fff;
}
.index-list-content{
background: #fff;
border-radius: 2px;
}
.index-list-title{
padding: 14px 16px;
font-size: 14px;
line-height: 1.6;
color: #333;
}
.index-list-anchor{
padding: 16px 16px 10px 16px;
line-height: 1;
font-size: 14px;
color: #999;
background: #f7f7f7;
}
.index-list-item {
position: relative;
height: 50px;
line-height: 50px;
padding: 0 16px;
font-size: 14px;
color: #333;
}
.index-list-item:last-child{
border: none;
}
.index-list-item_active{
background: #ddd;
}
.index-list-fixed{
position: absolute;
z-index: 1;
top: 0;
left: 0;
width: 100%;
padding: 16px 16px 10px 16px;
box-sizing: border-box;
font-size: 14px;
line-height: 1;
color: #999;
background: #f7f7f7;
}
.index-list-nav {
position: absolute;
z-index: 30;
right: 0;
top: 50%;
transform: translateY(-50%);
font-family: Helvetica;
}
.index-list-nav ul {
padding: 0;
margin: 0;
}
.index-list-nav li{
padding: 6px 16px 0 16px;
line-height: 1;
text-align: center;
box-sizing: border-box;
font-size: 12px;
color: gray;
}
.index-list-nav .active {
color: blue;
}
</style>
</head>
<body>
<div class="list-wrapper">
<div class="scroll-content">
<div class="index-list-content">
<div class="index-list-title">
定位城市:
</div>
<ul>
<li>
<h2 class="index-list-anchor">
★热门城市
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
北京市
</li>
<li class="index-list-item border-bottom-1px">
上海市
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
A
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
鞍山市
</li>
<li class="index-list-item border-bottom-1px">
安庆市
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
B
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
北京市
</li>
<li class="index-list-item border-bottom-1px">
巴音郭楞州
</li>
<li class="index-list-item border-bottom-1px">
博尔塔拉州
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
C
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
成都市
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
E
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
鄂尔多斯市
</li>
<li class="index-list-item border-bottom-1px">
鄂州市
</li>
<li class="index-list-item border-bottom-1px">
恩施州
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
F
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
福州市
</li>
<li class="index-list-item border-bottom-1px">
佛山市
</li>
<li class="index-list-item border-bottom-1px">
防城港市
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
G
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
广州市
</li>
<li class="index-list-item border-bottom-1px">
贵阳市
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
H
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
杭州市
</li>
<li class="index-list-item border-bottom-1px">
和田地区
</li>
</ul>
</li>
<li>
<h2 class="index-list-anchor">
Z
</h2>
<ul>
<li class="index-list-item border-bottom-1px">
郑州市
</li>
<li class="index-list-item border-bottom-1px">
张家口市
</li>
<li class="index-list-item border-bottom-1px">
张家界市
</li>
<li class="index-list-item border-bottom-1px">
珠海市
</li>
<li class="index-list-item border-bottom-1px">
中山市
</li>
<li class="index-list-item border-bottom-1px">
自贡市
</li>
<li class="index-list-item border-bottom-1px">
资阳市
</li>
<li class="index-list-item border-bottom-1px">
枣庄市
</li>
<li class="index-list-item border-bottom-1px">
舟山
</li>
<li class="index-list-item border-bottom-1px">
遵义市
</li>
<li class="index-list-item border-bottom-1px">
淄博市
</li>
<li class="index-list-item border-bottom-1px">
株洲市
</li>
<li class="index-list-item border-bottom-1px">
中卫市
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="index-list-nav">
<ul>
<li data-index="0" class="active">
★
</li>
<li data-index="1">
A
</li>
<li data-index="2" class="">
B
</li>
<li data-index="3" class="">
C
</li>
<li data-index="4" class="">
E
</li>
<li data-index="5" class="">
F
</li>
<li data-index="6" class="">
G
</li>
<li data-index="7" class="">
H
</li>
<li data-index="8" class="">
Z
</li>
</ul>
</div>
<div class="index-list-fixed" style="display: none;">
A
</div>
<script type="text/javascript" src="./bscroll.js"></script>
<script type="text/javascript" src="./city.js"></script>
</body>
</html>