vue js 移动端tab滑屏,手指滑动,切屏,代码直接复制vue文件可看效果
<template>
<div>
<div id="apps">
<nav>
<p
v-for="(item,$index) in arrs"
:key="$index"
@click="toggle($index)"
:class="{active:$index==active}"
>{{item}}</p>
</nav>
</div>
<div
class="back"
@touchstart.prevent="touchStart"
@touchmove.prevent="touchMove"
@touchend="touchEnd"
ref="back"
>
<div class="backbg" style="background:red"></div>
<div class="backbg" style="background:blue"></div>
<div class="backbg" style="background:yellow"></div>
<div class="backbg" style="background:pink"></div>
<div class="backbg" style="background:orange"></div>
<div class="backbg" style="background:white"></div>
<div class="backbg" style="background:grey"></div>
</div>
</div>
</template>
<script>
import More from './More'
export default {
name: "newmore",
components: {
More
},
data() {
return {
active: 0,
currentPlay: 0,
percent: 0,
arrs: ["菜单1", "菜单2", "菜单3", "菜单3",'菜单5','菜单6','菜单7']
};
},
created() {
this.touch = {}
},
mounted(){
},
destroyed(){
},
activated() {
},
methods: {
toggle(index) {
this.active = index;
},
touchEnd() {
let offsetWidth;
if(this.currentPlay === 0&&this.percent>0) return
if(this.currentPlay === this.arrs.length-1&&this.percent<0) return
if (this.percent < -0.35) {
this.currentPlay += 1;
this.active+=1;
offsetWidth = -window.innerWidth*this.currentPlay;
} else if (this.percent > 0.35) {
this.active-=1;
this.currentPlay -= 1;
offsetWidth = -window.innerWidth*this.currentPlay;
}else{
offsetWidth = -window.innerWidth*this.currentPlay;
}
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`;
this.$refs.back.style["transitionDuration"] = 5;
},
touchStart(e) {
const touch = e.touches[0];
this.touch.startX = touch.pageX;
this.touch.startY = touch.pageY ;
},
touchMove(e) {
const touch = e.touches[0];
const deltaX = touch.pageX - this.touch.startX;
const deltaY = touch.pageY - this.touch.startY;
if (Math.abs(deltaY) > Math.abs(deltaX)) {
return;
}
if(this.currentPlay === 0&&deltaX>0) return
if(this.currentPlay === this.arrs.length-1&&deltaX<0) return
var left=-window.innerWidth*this.currentPlay;
var offsetWidth = left + deltaX;
this.percent = deltaX / window.innerWidth;
console.log(this.percent)
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`;
this.$refs.back.style["transitionDuration"] = 5;
},
toggle(index) {
this.active = index;
var offsetWidth = -window.innerWidth*index;
this.currentPlay = index;
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`;
},
}
};
</script>
<style lang="scss" scoped >
#apps{ width: 100%; overflow:hidden; }
#apps nav{ padding: 0 10px; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-align: middle; -ms-flex-align: middle; align-items: middle; overflow: auto; }
#apps p{ text-align: center; font-size: 16px; -ms-flex-negative: 0; flex-shrink: 0; padding: 10px; margin: 5px; }
#apps p.active{ color: #ffff00; background-color: #000000; }
.back {
position: fixed;
width: 100%;
height: 100px;
white-space: nowrap;
.backbg {
position: relative;
vertical-align: top;
display:inline-block;
width: 100%;
height: 100%;
background-color: red;
}
}
</style>