vue + better-scroll 封装成组件,横向滚动
2018-03-22 19:46
2561 查看
项目需要,需要个横向滚动条,封装成组件,信息从父页面传递,搞了一下午,贴代码:
组件为last-play:
<template> <div> <svg class="icon" aria-hidden="true" slot="icon" > <use xlink:href="#icon-zuijinlaifang"></use> </svg> <span >最近在玩</span> <div class="tab" ref="tab"> <div class="tab_content" ref="tabcontent"> <game_block class="tab_item" v-for="(item, index) in lastGameList" ref="tabitem" :gameinfo = 'item'> </game_block> </div> </div>
<foot-guide></foot-guide> </div></template>
<script>import { mapState } from "vuex";import footGuide from 'src/components/footGuide';import BScroll from 'better-scroll';import game_block from 'src/components/gameBlock';export default { name:'last_play', data() { return { selected:'game', activeIndex: 'game', }; }, components: { footGuide, game_block }, created () { this.$nextTick(()=>{ this.InitTabScroll(); });//必须等dom元素挂载完下一帧才能渠道$refs }, props: ["lastGameList"], methods: { goSpecial(index){ console.log('jump to index:'+index); }, InitTabScroll(){ console.log(this.lastGameList); let width= this.lastGameList.length * 80; this.$refs.tabcontent.style.width=width+'px'; console.log(width); this.$nextTick(()=>{ if (!this.scroll) { this.scroll=new BScroll(this.$refs.tab, { startX:0, click:true, scrollX:true, scrollY:false, eventPassthrough:'vertical' }) }else{ this.scroll.refresh(); } }); } }
}</script>
<style lang="scss" > .tab{ display: inline-block; width: 100%; height: 130px; overflow: hidden; } .tab_content{ text-align: 'center'; height: 130px; } .tab_item{ display: inline-block;
} .icon { display: inline-block; width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; margin-left: 10px; margin-right: 10px; } span{ font-size: 0.4rem; }</style>
其中game_block 是封装的另一个组件,lastGameList 是父页面到子组件传递的数据
父页面引用:
<div style="lastPlaydiv "> <last_play :lastGameList = 'gameinfo'></last_play> </div>
有个坑就是:lastGameList 必须是数组,不能是json!!
组件为last-play:
<template> <div> <svg class="icon" aria-hidden="true" slot="icon" > <use xlink:href="#icon-zuijinlaifang"></use> </svg> <span >最近在玩</span> <div class="tab" ref="tab"> <div class="tab_content" ref="tabcontent"> <game_block class="tab_item" v-for="(item, index) in lastGameList" ref="tabitem" :gameinfo = 'item'> </game_block> </div> </div>
<foot-guide></foot-guide> </div></template>
<script>import { mapState } from "vuex";import footGuide from 'src/components/footGuide';import BScroll from 'better-scroll';import game_block from 'src/components/gameBlock';export default { name:'last_play', data() { return { selected:'game', activeIndex: 'game', }; }, components: { footGuide, game_block }, created () { this.$nextTick(()=>{ this.InitTabScroll(); });//必须等dom元素挂载完下一帧才能渠道$refs }, props: ["lastGameList"], methods: { goSpecial(index){ console.log('jump to index:'+index); }, InitTabScroll(){ console.log(this.lastGameList); let width= this.lastGameList.length * 80; this.$refs.tabcontent.style.width=width+'px'; console.log(width); this.$nextTick(()=>{ if (!this.scroll) { this.scroll=new BScroll(this.$refs.tab, { startX:0, click:true, scrollX:true, scrollY:false, eventPassthrough:'vertical' }) }else{ this.scroll.refresh(); } }); } }
}</script>
<style lang="scss" > .tab{ display: inline-block; width: 100%; height: 130px; overflow: hidden; } .tab_content{ text-align: 'center'; height: 130px; } .tab_item{ display: inline-block;
} .icon { display: inline-block; width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; margin-left: 10px; margin-right: 10px; } span{ font-size: 0.4rem; }</style>
其中game_block 是封装的另一个组件,lastGameList 是父页面到子组件传递的数据
父页面引用:
<div style="lastPlaydiv "> <last_play :lastGameList = 'gameinfo'></last_play> </div>
有个坑就是:lastGameList 必须是数组,不能是json!!
相关文章推荐
- vue中使用better-scroll做横向滚动注意事项
- vue实现横向滚动效果(better-scroll)
- MUI滚动组件-scroll 横向滚动图片一半解决办法
- better-scroll上拉加载 和下拉刷新 的使用(基于vue写成组件的形式)
- vue的无缝滚动组件vue-seamless-scroll实例
- vue better scroll 无法滚动的解决方法
- Vue组件Better-Scroll使用
- 详解vue之better-scroll实现轮播图和页面滚动
- vue 中使用better-scroll插件时无法滚动问题
- 一个封装好的iOS无限滚动组件HXInfiniteScrollView
- vue滚动轴插件better-scroll使用详解
- vue利用better-scroll实现轮播图与页面滚动详解
- 小程序横向scroll-view组件自动滚动到某个view
- vue使用Better-Scroll实现纵向滚动
- 微信小程序顶部用 scroll-view 组件横向滚动,类似tab选项卡的效果,
- vue三级界面使用better-scroll滚动
- better-scroll在vue中实现原生滚动和上拉/下拉加载的效果
- vue之滚动轴插件better-scroll
- vue2.0 自定义组件的方法(vue组件的封装)
- 微信小程序scroll-view实现横向滚动和上拉加载示例