vue項目實現(xiàn)按鈕可隨意移動
更新時間:2022年03月30日 08:51:15 作者:帥_帥
這篇文章主要為大家詳細介紹了vue項目實現(xiàn)按鈕可隨意移動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
vue項目中實現(xiàn)按鈕可隨意移動,供大家參考,具體內(nèi)容如下
組件代碼如下:
在項目中引入該組件即可
<template>
? <div v-show="hide" class="move-button" ref="moveBtn"
? ? ? ?@mousedown="btnDown"
? ? ? ?@touchstart="btnDown"
? ? ? ?@mousemove="btnMove"
? ? ? ?@touchmove="btnMove"
? ? ? ?@mouseup="btnEnd"
? ? ? ?@touchend="btnEnd"
? ? ? ?@touchcancel="btnEnd">
? ? <div class="button-mainbg">
? ? </div>
?? ?</div>
</template>
<script>
export default {
? ? name: 'MoveButton',
? ? data() {
? ? ? ? return {
? ? ? ? ? ? hide: true,
? ? ? ? ? ? img: require('@/assets/img/moveButton.png'),
? ? ? ? ? ? flags: false,
? ? ? ? ? ? position: {
? ? ? ? ? ? ? ? x: 0,
? ? ? ? ? ? ? ? y: 0
? ? ? ? ? ? },
? ? ? ? ? ? nx: '',
? ? ? ? ? ? ny: '',
? ? ? ? ? ? dx: '',
? ? ? ? ? ? dy: '',
? ? ? ? ? ? xPum: '',
? ? ? ? ? ? yPum: '',
? ? ? ? ? ? isShow: false,
? ? ? ? ? ? moveBtn: {},
? ? ? ? ? ? timer: null,
? ? ? ? ? ? currentTop:0
? ? ? ? }
? ? },
? ? mounted() {
? ? ? ? this.moveBtn = this.$refs.moveBtn;
? ? ? ? window.addEventListener('scroll', this.hideButton);
? ? },
? ? beforeDestroy() {
? ? ? ? window.addEventListener('scroll', this.hideButton);
? ? },
? ? methods: {
? ? ? ? hideButton() {
? ? ? ? ? ? this.timer&&clearTimeout(this.timer);
? ? ? ? ? ? this.timer = setTimeout(()=>{
? ? ? ? ? ? ?this.handleScrollEnd();
? ? ? ? ? ? },300);
? ? ? ? ? ? this.currentTop = document.documentElement.scrollTop || document.body.scrollTop;
? ? ? ? ? ? this.hide = false;
? ? ? ? },
? ? ? ? handleScrollEnd(){
? ? ? ? ? ? let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
? ? ? ? ? ? if(scrollTop === this.currentTop){
? ? ? ? ? ? this.hide = true;
? ? ? ? ? ? clearTimeout(this.timer);
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? // 實現(xiàn)移動端拖拽
? ? ? btnDown() {
? ? ? ? ? ? this.flags = true;
? ? ? ? ? ? let touch;
? ? ? ? ? ? if (event.touches) {
? ? ? ? ? ? ? ? touch = event.touches[0];
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? touch = event;
? ? ? ? ? ? }
? ? ? ? ? ? this.position.x = touch.clientX;
? ? ? ? ? ? this.position.y = touch.clientY;
? ? ? ? ? ? this.dx = this.moveBtn.offsetLeft;
? ? ? ? ? ? this.dy = this.moveBtn.offsetTop;
? ? ? ? },
? ? ? btnMove() {
? ? ? ? ? ? if (this.flags) {
? ? ? ? ? ? ? ? let touch;
? ? ? ? ? ? ? ? if (event.touches) {
? ? ? ? ? ? ? ? ? ? touch = event.touches[0];
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? touch = event;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.nx = touch.clientX - this.position.x;
? ? ? ? ? ? ? ? this.ny = touch.clientY - this.position.y;
? ? ? ? ? ? ? ? this.xPum = this.dx + this.nx;
? ? ? ? ? ? ? ? this.yPum = this.dy + this.ny;
? ? ? ? ? ? ? ? let clientWidth = document.documentElement.clientWidth;
? ? ? ? ? ? ? let clientHeight = document.documentElement.clientHeight;
? ? ? ? ? ? ? ? if (this.xPum > 0 && this.xPum < (clientWidth - this.moveBtn.offsetWidth)) {
? ? ? ? ? ? ? ? ? ? this.moveBtn.style.left = this.xPum + "px";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (this.yPum > 0 && this.yPum < (clientHeight - this.moveBtn.offsetHeight)) {
? ? ? ? ? ? ? ? ? ? this.moveBtn.style.top = this.yPum + "px";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //阻止頁面的滑動默認事件
? ? ? ? ? ? ? ? document.addEventListener("touchmove", this.handler, {
? ? ? ? ? ? ? ? ? ? passive: false
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? //鼠標釋放時候的函數(shù)
? ? ? ? btnEnd() {
? ? ? ? ? ? this.flags = false;
? ? ? ? ? ? document.addEventListener('touchmove', this.handler, {
? ? ? ? ? ? ? ? passive: false
? ? ? ? ? ? });
? ? ? ? },
? ? ? ? handler(e) {
? ? ? ? ? ? if(this.flags){
? ? ? ? ? ? ? ? event.preventDefault();
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return true
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
</script>
<style lang="stylus" scoped>
.move-button {
? ? border-radius:50%;
? ? width: 50px;
? ? height: 50px;
? ? position: fixed;
? ? z-index: 10;
? color: #FFF;
? .button-mainbg{
? ? position: relative;
? ? width:50px;
? ? height:50px;
? ? border-radius:50%;
? ? background: url("../../assets/img/moveButton.png") no-repeat;
? ? background-size: 50px 50px;
? }
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解Vue基于vue-quill-editor富文本編輯器使用心得
這篇文章主要介紹了Vue基于vue-quill-editor富文本編輯器使用心得,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Vue+Element UI實現(xiàn)概要小彈窗的全過程
彈窗效果是我們?nèi)粘i_發(fā)中經(jīng)常遇到的一個功能,下面這篇文章主要給大家介紹了關于Vue+Element UI實現(xiàn)概要小彈窗的相關資料,需要的朋友可以參考下2021-05-05
VUE開發(fā)分布式醫(yī)療掛號系統(tǒng)后臺管理頁面步驟
本文從整體上介紹Vue框架的開發(fā)流程,結(jié)合具體的案例,使用Vue框架調(diào)用具體的后端接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

