作者:默识,来自原文地址功能描述
演示
文件介绍
hSwiper.js //插件的核心文件
hSwiper.wxml //插件的dom结构
hSwiper.wxss //插件的css
hSwiperTemplate.wxml //插件每个元素的模版
使用方法下载本插件,在需要使用的page的相关位置导入对应的hSwiper.js,hSwiper.wxss以及hSwiper.wxml即可,具体使用如下 index.js
const hSwiper=require("../../component/hSwiper/hSwiper.js");
var option={
data:{
//swiper插件变量
hSwiperVar:{}
},
onLoad:function(){
},
onReady:function(){
//实例化插件
var swiper=new hSwiper({reduceDistance:60,varStr:"hSwiperVar",list:[1,2,3,4,5]});
swiper.onFirstView=function(data,index){
console.log("当前是第"+(index+1)+"视图","数据是:"+data);
};
swiper.onLastView=function(data,index){
console.log("当前是第"+(index+1)+"视图","数据是:"+data);
};
swiper.afterViewChange=function(data,index){
console.log("当前是第"+(index+1)+"视图","数据是:"+data);
};
swiper.beforeViewChange=function(data,index){
console.log("当前是第"+(index+1)+"视图","数据是:"+data);
};
//更新数据
setTimeout(()=>{
console.log("3 s 后更新列表数据");
//3 s 后更新列表数据
this.setData({
"hSwiperVar.list[0]":"修改"
});
}, 3000);
setTimeout(()=>{
console.log("5s后更新数据 并且更新视图");
//5s后更新数据 并且更新视图
var oldList=swiper.getList();
swiper.updateList(oldList.concat([11,23,45]));
}, 5000);
}
};
Page(option);
index.wxml
<import src="../../component/hSwiper/hSwiper.wxml"/>
<view id="mainContainer">
<template is="hSwiper" data="{{...hSwiperVar}}"></template>
</view>
index.wxss
@import "../../component/hSwiper/hSwiper.wxss";
/*滚动图*/
#mainContainer{
height: 100%;
width: 100%;
}
.itemSelf{
height: 100%;
position: absolute;
box-sizing:border-box;
margin:10rpx;
overflow: hidden;
padding: 10rpx;
box-shadow: 0 0 1px 1px rgba(0,0,0,0.1);
height: 95%;
width: 96%;
background-color: gray;
color: white;
}
hSwiperTemplate.wxml (hswiper视图元素模版)每个视图的内容的wxml都写在该文件里面,使用 template标签 ,并且命名 ,当调用这里面的模版时,会自动注入 item以及 index数据,index表示是当前元素的元素索引 ,item则表示当前元素 数据。(相当于list[index]=item,但是 list不会注入到模版里面)
<template name="hSwiperItem">
<view class="itemSelf">
{{item}}
</view>
</template>
<template name="templateb">
{{item}}
</template>
hSwiper入口参数解释
var swiper=new hSwiper({
reduceDistance:60,
varStr:"hSwiperVar",
list:[1,2,3,4,5]
});
接口
事件item,index为当前视图的数据,以及索引
具体使用 可查看example文件夹下的例子,有注释说明。欢迎提问!!!
github地址:https://github.com/hlerenow/hSwiper
|