Vue2 悬浮球

<template>
  <div>
    <FloatBall />
  </div>
</template>
 
<script>
import FloatBall from "@/components/FloatBall.vue"
export default {
  name: "HomeView",
  components: { FloatBall },
  data() {
    return {
    
    };
  },
  mounted() {
   
  }
};
</script>
 
<style lang="less" scoped>
</style>
 

<template>
    <transition>
        <div ref="floatBall" class="floatBall" @touchstart.stop="handleStart"
            @touchmove.prevent.stop="handleMove($event)" @touchend.stop="handleEnd"
            :style="{ left: left + 'px', top: top + 'px', width: itemWidth + 'px', height: itemHeight + 'px' }"
            v-if="isShow">
            {{ text }}
        </div>
    </transition>
</template>

<script>
export default {
    props: {
        text: {
            type: String,
            default: '悬浮'
        },
        itemWidth: {
            type: Number,
            default: 80
        },
        itemHeight: {
            type: Number,
            default: 80
        }
    },
    data() {
        return {
            left: 0,
            top: 0,
            startToMove: false,
            isShow: true,
            timer: null,
            currentTop: null,
            clientW: document.documentElement.clientWidth, // 视口宽
            clientH: document.documentElement.clientHeight, // 视口高
        }
    },
    created() {
        this.left = (this.clientW - this.itemWidth - 30)
        this.top = (this.clientH / 2 - this.itemHeight / 2)
    },
    mounted() {
        this.bindScrollEvent()
    },
    beforeDestroy() {
        this.removeScrollEvent()
    },
    methods: {
        handleStart() {
            this.startToMove = true
            this.$refs.floatBall.style.transition = "none"
        },
        handleMove(e) {
            const clientX = e.targetTouches[0].clientX;
            const clientY = e.targetTouches[0].clientY;
            const isInScreen = clientX <= this.clientW && clientX >= 0 && clientY <= this.clientH && clientY >= 0
            if (this.startToMove && e.targetTouches.length === 1) {
                if (isInScreen) {
                    this.left = clientX - this.itemWidth / 2
                    this.top = clientY - this.itemHeight / 2
                }
            }
        },
        handleEnd() {
            if (this.left < (this.clientW / 2)) {
                this.left = 30;//不让贴边 所以设置30没设置0
                this.handleIconY()
            } else {
                this.left = this.clientW - this.itemWidth - 30;//不让贴边 所以减30
                this.handleIconY()
            }
            this.$refs.floatBall.style.transition = "all .3s"
        },
        handleIconY() {
            if (this.top < 0) {
                this.top = 30;
            } else if (this.top + this.itemHeight > this.clientH) {
                this.top = this.clientH - this.itemHeight - 30;
            }
        },
        bindScrollEvent() {
            window.addEventListener('scroll', this.handleScrollStart)
        },
        removeScrollEvent() {
            window.removeEventListener('scroll', this.handleScrollStart)
        },
        handleScrollStart() {
            this.isShow = false
            this.timer && clearTimeout(this.timer)
            this.timer = setTimeout(() => {
                this.handleScrollEnd()
            }, 300)
            this.currentTop = document.documentElement.scrollTop || document.body.scrollTop
        },
        handleScrollEnd() {
            this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 判断是否停止滚动的条件
            if (this.scrollTop == this.currentTop) {
                this.isShow = true
            }
        }
    },
}
</script>

<style scoped>
.floatBall {
    position: fixed;
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: #f0f;
    line-height: 80px;
    text-align: center;
    color: #fff;
}

.v-enter {
    opacity: 1;
}

.v-leave-to {
    opacity: 0;
}

.v-enter-active,
.v-leave-active {
    transition: opacity 0.3s;
}
</style>
  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Vue2中,可以通过`<router-link>`组件来实现按钮跳转页面。 首先,需要安装并配置Vue Router。在安装和配置完成后,即可在Vue组件中使用`<router-link>`组件来实现页面跳转。 下面是一个简单的示例: ```html <template> <div> <h1>Home Page</h1> <router-link to="/about"> <button>About Page</button> </router-link> </div> </template> ``` 在上面的示例中,我们通过`<router-link>`组件来实现了一个跳转到`/about`页面的按钮。`to`属性指定了要跳转的页面路径。 当用户点击按钮时,路由会自动导航到指定的页面。 ### 回答2: 使用Vue2实现按钮跳转页面有以下几个步骤: 1. 在VueHTML模板中创建一个按钮元素,使用`v-on:click`指令绑定一个点击事件。 ```html <button v-on:click="redirectToPage">跳转页面</button> ``` 2. 在Vue的Script部分,定义一个`methods`对象,并在其中定义`redirectToPage`方法,该方法用于按钮点击事件的处理。 ```javascript <script> export default { methods: { redirectToPage() { // 在这里实现按钮跳转逻辑 } } } </script> ``` 3. 使用Vue的编程式导航,使用`$router.push`方法进行页面跳转。 ```javascript <script> import { router } from '@/router'; export default { methods: { redirectToPage() { this.$router.push('/target-page'); // 跳转到目标页面的路径 } } } </script> ``` 4. 在Vue的路由配置中,设置目标页面的路径与组件的对应关系。 ```javascript import Vue from 'vue'; import Router from 'vue-router'; import TargetPage from '@/components/TargetPage'; Vue.use(Router); export const router = new Router({ routes: [ { path: '/target-page', name: 'TargetPage', component: TargetPage } ] }); ``` 通过以上方法,当按钮被点击时,Vue会调用`redirectToPage`方法,通过编程式导航将页面跳转到目标页面。 ### 回答3: 在Vue2中实现按钮跳转页面,可以采用以下步骤: 1. 首先,在Vue组件的template中添加一个按钮元素,例如: ``` <template> <button @click="goToPage">点击跳转页面</button> </template> ``` 2. 在Vue组件的script标签中,定义一个方法来处理按钮的点击事件,例如: ``` <script> export default { methods: { goToPage() { // 在这里进行页面跳转的逻辑处理 } } } </script> ``` 3. 在goToPage方法中,使用Vue的路由功能来实现页面的跳转,首先需要在main.js中配置路由: ``` import Vue from 'vue' import VueRouter from 'vue-router' import Page from '@/views/Page.vue' Vue.use(VueRouter) const routes = [ { path: '/page', name: 'Page', component: Page }, // 其他路由配置... ] const router = new VueRouter({ mode: 'history', routes }) export default router ``` 4. 然后,在goToPage方法中,使用this.$router.push方法来进行跳转,例如: ``` goToPage() { this.$router.push('/page') } ``` 这样就可以通过点击按钮,实现跳转到名为Page的页面。需要注意的是,在实际使用中,需要根据项目的实际情况来配置路由和页面组件,并在需要跳转的页面组件中进行相应的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钓了猫的鱼儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值