vue的无缝滚动组件vue-seamless-scroll实例
程序员文章站
2022-04-03 17:06:58
installation
npm
npm install vue-seamless-scroll --save
usage
es6
以...
installation
npm
npm install vue-seamless-scroll --save
usage
es6
以下es6用法需要webpack环境编译.
<template> <div id="app"> <div class="fixed top-0 w-100 z-1 flex-none flex flex-row items-center pv3 ph4 bg-blue white"> <div class="flex-none"><a href="https://github.com/chenxuan0000/vue-seamless-scroll" rel="external nofollow" rel="external nofollow" rel="noopener" target="_blank" title="view on github"> <svg xmlns="http://www.w3.org/2000/svg" fill="white" width="32" height="32" viewbox="0 0 16 16"> <path d="m8 .198c-4.418 0-8 3.582-8 8 0 3.535 2.292 6.533 5.47 7.59.4.075.548-.173.548-.384 0-.19-.008-.82-.01-1.49-2.227.485-2.696-.943-2.696-.943-.364-.924-.888-1.17-.888-1.17-.726-.497.055-.486.055-.486.802.056 1.225.824 1.225.824.714 1.223 1.872.87 2.328.665.072-.517.28-.87.508-1.07-1.776-.202-3.644-.888-3.644-3.954 0-.874.313-1.588.824-2.148-.083-.202-.357-1.015.077-2.117 0 0 .672-.215 2.2.82.64-.177 1.323-.266 2.003-.27.68.004 1.365.093 2.004.27 1.527-1.035 2.198-.82 2.198-.82.435 1.102.162 1.916.08 2.117.512.56.822 1.274.822 2.147 0 3.072-1.872 3.748-3.653 3.946.288.248.544.735.544 1.48 0 1.07-.01 1.933-.01 2.196 0 .213.145.462.55.384 3.178-1.06 5.467-4.057 5.467-7.59 0-4.418-3.58-8-8-8z"></path> </svg> </a> </div> <div class="flex-none" style="margin-left: 10px;"><a href="https://github.com/chenxuan0000/vue-seamless-scroll" rel="external nofollow" rel="external nofollow" class="white no-underline underline-hover">vue-seamless-scroll</a></div> </div> <p class="pt5 f2" style="text-align: center;">无缝滚动demo</p> <div class="flex wd800"> <div class="options" style="color:#357edd;"> <p><b>demo1</b> 向上无缝滚动,hover可停止</p> var option = {<br/> step: 0.5,<br/> limitmovenum: 5<br/> } </div> <my-class :data="listdata" :class-option="classoption" @copy-data="listdata = listdata.concat(listdata)" class="warp"> <ul class="item"> <li v-for="item in listdata"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span> </li> </ul> </my-class> </div> <div class="flex wd800"> <div class="options" style="color:#357edd;"> <p><b>demo2</b> limitmovenum过大不滚动 开启了openwatch</p> <p>3s后data增加到9条</p> var option = {<br/> limitmovenum: 7<br/> } </div> <my-class :data="listdata1" :class-option="classoption1" @copy-data="listdata1 = listdata1.concat(listdata1)" class="warp"> <ul class="item"> <li v-for="item in listdata1"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span></li> </ul> </my-class> </div> <div class="flex wd800"> <div class="options" style="color:#357edd;"> <p><b>demo3</b> limitmovenum过大不滚动 关闭了openwatch</p> <p>3s后data增加到9条</p> var option = {<br/> limitmovenum: 7<br/> openwatch: false<br/> } </div> <my-class :data="listdata2" :class-option="classoption2" @copy-data="listdata2 = listdata2.concat(listdata2)" class="warp"> <ul class="item"> <li v-for="item in listdata2"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span></li> </ul> </my-class> </div> <div class="flex wd800"> <div class="options" style="color:#357edd;"> <p><b>demo4</b> 向下滚动,禁止了hover悬停</p> var option = {<br/> limitmovenum: 5,<br/> direction: 0,<br/> hoverstop: false,<br/> } </div> <my-class :data="listdata3" :class-option="classoption3" @copy-data="listdata3 = listdata3.concat(listdata3)" class="warp"> <ul class="item"> <li v-for="item in listdata3"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span></li> </ul> </my-class> </div> <div class="flex wd800"> <div class="options" style="color:#357edd;"> <p><b>demo5</b> 向上无缝滚动,单条停止一段时间</p> var option = {<br/> step: 0.5,<br/> limitmovenum: 5<br/> singleheight: 30,<br/> waittime: 1000<br/> } </div> <my-class :data="listdata4" :class-option="classoption4" @copy-data="listdata4 = listdata4.concat(listdata4)" class="warp"> <ul class="item"> <li v-for="item in listdata4"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span> </li> </ul> </my-class> </div> </div> </template> <script> import myclass from '../src/components/myclass.vue' export default { name: 'app', data () { return { listdata: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }, { 'title': '无缝滚动第七行无缝滚动第七行', 'date': '2017-12-16' }, { 'title': '无缝滚动第八行无缝滚动第八行', 'date': '2017-12-16' }, { 'title': '无缝滚动第九行无缝滚动第九行', 'date': '2017-12-16' }], listdata1: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }], listdata2: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }], listdata3: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }, { 'title': '无缝滚动第七行无缝滚动第七行', 'date': '2017-12-16' }, { 'title': '无缝滚动第八行无缝滚动第八行', 'date': '2017-12-16' }], listdata4: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }, { 'title': '无缝滚动第七行无缝滚动第七行', 'date': '2017-12-16' }, { 'title': '无缝滚动第八行无缝滚动第八行', 'date': '2017-12-16' }, { 'title': '无缝滚动第九行无缝滚动第九行', 'date': '2017-12-16' }], } }, computed: { classoption () { return { step: 0.5, limitmovenum: 5 } }, classoption1 () { return { limitmovenum: 7 } }, classoption2 () { return { limitmovenum: 7, openwatch: false } }, classoption3 () { return { limitmovenum: 5, direction: 0, hoverstop: false } }, classoption4 () { return { step: 0.5, limitmovenum: 5, singleheight: 30, waittime: 1000 } } }, components: { myclass }, created () { settimeout(() => { this.listdata1 = this.listdata2 = [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }, { 'title': '无缝滚动第七行无缝滚动第七行', 'date': '2017-12-16' }, { 'title': '无缝滚动第八行无缝滚动第八行', 'date': '2017-12-16' }, { 'title': '无缝滚动第九行无缝滚动第九行', 'date': '2017-12-16' }] }, 3000) } } </script> <style lang="scss"> #app { padding-bottom: 100px; } .flex-fill { -ms-flex: 1 1; flex: 1 1; } .options { width: 300px; font-size: 15px; margin-right: 60px; p { color: #000; margin-bottom: 30px; b { font-size: 16px; font-style: italic; } } } .wd800 { width: 800px; margin: 30px auto; } .warp { height: 260px; width: 360px; overflow: hidden; ul { list-style: none; padding: 0; margin: 0 auto; li { height: 30px; line-height: 30px; display: flex; justify-content: space-between; font-size: 15px; } } } @media screen and (max-width: 770px) { .warp { width: 90%; margin: 0 auto; } body { background-color: lightblue; } .wd800 { width: 100%; } .flex.wd800 { display: block; } .options { width: 90%; margin: 20px auto; p { margin-bottom:0; } } } </style>
import vue from 'vue' import vueseamlessscroll from 'vue-seamless-scroll' new vue({ components: { vueseamlessscroll } })
普通模式 (script tag)
example:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title> <style> .warp { height: 260px; width: 360px; overflow: hidden; } .warp ul { list-style: none; padding: 0; margin: 0 auto; } .warp li { height: 30px; line-height: 30px; display: flex; justify-content: space-between; font-size: 15px; } </style> </head> <body> <div id="app"> <vue-seamless-scroll :data="listdata" :class-option="classoption" @copy-data="listdata = listdata.concat(listdata)" class="warp"> <ul class="item"> <li v-for="item in listdata"><span class="title" v-text="item.title"></span><span class="date" v-text="item.date"></span> </li> </ul> </vue-seamless-scroll> </div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.js"></script> <script src="../dist/vue-seamless-scroll.min.js"></script> <script> new vue({ el: '#app', data: { listdata: [{ 'title': '无缝滚动第一行无缝滚动第一行', 'date': '2017-12-16' }, { 'title': '无缝滚动第二行无缝滚动第二行', 'date': '2017-12-16' }, { 'title': '无缝滚动第三行无缝滚动第三行', 'date': '2017-12-16' }, { 'title': '无缝滚动第四行无缝滚动第四行', 'date': '2017-12-16' }, { 'title': '无缝滚动第五行无缝滚动第五行', 'date': '2017-12-16' }, { 'title': '无缝滚动第六行无缝滚动第六行', 'date': '2017-12-16' }, { 'title': '无缝滚动第七行无缝滚动第七行', 'date': '2017-12-16' }, { 'title': '无缝滚动第八行无缝滚动第八行', 'date': '2017-12-16' }, { 'title': '无缝滚动第九行无缝滚动第九行', 'date': '2017-12-16' }] }, computed: { classoption: function () { return { step: 0.5, limitmovenum: 5 } } } }) </script> </body> </html>
<html> <head> ... </head> <body> <div id="app"> <vue-seamless-scroll></vue-seamless-scroll> </div> <script src="vue.js"></script> <script src="vue-seamless-scroll"></script> <script> new vue({ el: '#app' }) </script> </body> </html>
configure
defaultoption () { return { step: 1, //步长 越大滚动速度越快 limitmovenum: 5, //启动无缝滚动最小数据量 this.datalist.length hoverstop: true, //是否启用鼠标hover控制 direction: 1, //1 往上 0 往下 openwatch: true, //开启data实时监听 singleheight: 0, //单条数据高度有值hoverstop关闭 waittime: 1000 //单步停止等待时间 } }
tks
vue-seamless-scroll发现bug或者有什么不足望指点,感觉不错点个star吧。