dom.js
export functionhasClass(el, className){
let reg=newRegExp('(^|\\s)'+ className+'(\\s|$)')return reg.test(el.className)}
export functionaddClass(el, className){if(hasClass(el, className)){return}
let newClass= el.className.split(' ')
newClass.push(className)
el.className= newClass.join(' ')}
export functiongetData(el, name, val){const prefix='data-'if(val){return el.setAttribute(prefix+ name, val)}return el.getAttribute(prefix+ name)}
let elementStyle= document.createElement('div').style
let vendor=(()=>{
let transformNames={
webkit: 'webkitTransform',Moz: 'MozTransform',O: 'OTransform',
ms: 'msTransform',
standard: 'transform'}for(let key in transformNames){if(elementStyle[transformNames[key]]!== undefined){return key}}returnfalse})()
export functionprefixStyle(style){if(vendor===false){returnfalse}if(vendor=== 'standard'){return style}return vendor+ style.charAt(0).toUpperCase()+ style.substr(1)}

竖向拖拽滑动进度条插件
<template><divclass="hello"><divclass="progress-bar" ref="progressBar"@click="progressClick"><divclass="bar-inner"><!-- 背景--><divclass="progress" ref="progress"></div><!-- 小圆点--><divclass="progress-btn-wrapper"
ref="progressBtn"@touchstart.prevent="progressTouchStart"@touchmove.prevent="progressTouchMove"@touchend="progressTouchEnd"><divclass="progress-btn"></div></div></div></div></div></template><script>import{ prefixStyle} from"./dom";const progressBtnWidth=16;const transform=prefixStyle("transform");
exportdefault{
name:"HelloWorld",data(){return{
msg:""};},created(){this.touch={};},
methods:{progressTouchStart(e){this.touch.initiated=true;if(this.touch.bottom>0){this.touch.startY= e.touches[0].pageY+this.touch.bottom;}else{this.touch.startY= e.touches[0].pageY;}},progressTouchMove(e){if(!this.touch.initiated){return;}const deltaY=this.touch.startY- e.touches[0].pageY;const offsetWidth=Math.min(this.$refs.progressBar.clientHeight,Math.max(0, deltaY));this.touch.bottom= offsetWidth;this._offset(offsetWidth);},progressTouchEnd(){this.touch.initiated=false;this._triggerPercent();},progressClick(e){const rect=this.$refs.progressBar.getBoundingClientRect();const offsetWidth=Math.min(
rect.height,Math.max(0, rect.bottom- e.pageY));this._offset(offsetWidth);this._triggerPercent();},_triggerPercent(){const barHeight=this.$refs.progressBar.clientHeight- progressBtnWidth;const percent=this.$refs.progress.clientHeight/ barHeight;this.$emit("percentChange", percent);},_offset(offsetWidth){this.$refs.progress.style.height= `${offsetWidth}px`;this.$refs.progressBtn.style[
transform]= `translate3d(0,${-offsetWidth}px,0)`;}},
watch:{percent(newPercent){if(newPercent>=0&&!this.touch.initiated){const barWidth=this.$refs.progressBar.clientHeight- progressBtnWidth;const offsetWidth= newPercent* barWidth;this._offset(offsetWidth);}}}};</script><!--Add"scoped" attributetolimit CSStothis component only--><style scoped>.progress-bar{
width:10px;
height:200px;
margin:0 auto;}.bar-inner{
position: relative;
width:10px;
height:200px;
background: green;}.progress{
position: absolute;
width:100%;
bottom:0;
background: #ffcd32;}.progress-btn-wrapper{
position: absolute;
left:-10px;
bottom:-13px;
width:30px;
height:30px;}.progress-btn{
position: relative;
top:7px;
left:7px;
box-sizing: border-box;
width:16px;
height:16px;
border:3px solid #333;
border-radius:50%;
background: #ffcd32;}</style>
横向拖拽滑动进度条插件
<template><divclass="progress-bar" ref="progressBar"@click="progressClick"><divclass="bar-inner"><!-- 背景--><divclass="progress" ref="progress"></div><!-- 小圆点--><divclass="progress-btn-wrapper" ref="progressBtn"@touchstart.prevent="progressTouchStart"@touchmove.prevent="progressTouchMove"@touchend="progressTouchEnd"><divclass="progress-btn"></div></div></div></div></template><script type="text/ecmascript-6">import{prefixStyle} from 'common/js/dom'const progressBtnWidth=16const transform=prefixStyle('transform')
exportdefault{
props:{
percent:{
type:Number,default:0}},created(){this.touch={}},
methods:{progressTouchStart(e){this.touch.initiated=truethis.touch.startX= e.touches[0].pageXthis.touch.left=this.$refs.progress.clientWidth},progressTouchMove(e){if(!this.touch.initiated){return}const deltaX= e.touches[0].pageX-this.touch.startXconst offsetWidth=Math.min(this.$refs.progressBar.clientWidth- progressBtnWidth,Math.max(0,this.touch.left+ deltaX))this._offset(offsetWidth)},progressTouchEnd(){this.touch.initiated=falsethis._triggerPercent()},progressClick(e){const rect=this.$refs.progressBar.getBoundingClientRect()const offsetWidth= e.pageX- rect.leftthis._offset(offsetWidth)this._triggerPercent()},_triggerPercent(){const barWidth=this.$refs.progressBar.clientWidth- progressBtnWidthconst percent=this.$refs.progress.clientWidth/ barWidththis.$emit('percentChange', percent)},_offset(offsetWidth){this.$refs.progress.style.width= `${offsetWidth}px`this.$refs.progressBtn.style[transform]= `translate3d(${offsetWidth}px,0,0)`}},
watch:{percent(newPercent){if(newPercent>=0&&!this.touch.initiated){const barWidth=this.$refs.progressBar.clientWidth- progressBtnWidthconst offsetWidth= newPercent* barWidththis._offset(offsetWidth)}}}}</script><style scoped lang="stylus" rel="stylesheet/stylus">@import"~common/stylus/variable".progress-bar
height:30px.bar-inner
position: relative
top:13px
height:4px
background:rgba(0,0,0,0.3).progress
position: absolute
height:100%
background: $color-theme.progress-btn-wrapper
position: absolute
left:-8px
top:-13px
width:30px
height:30px.progress-btn
position: relative
top:7px
left:7px
box-sizing: border-box
width:16px
height:16px
border:3px solid $color-text
border-radius:50%
background: $color-theme</style>