vue广告悬浮框,页面来回移动,鼠标放上停止,离开移动

1.dom

javascript 复制代码
 <div class="popup-dialog" id="popupDialog" @mouseover="onMmouseover" @mouseout="onMouseout">
   <p>vue广告悬浮</p>
  </div>

2.js

javascript 复制代码
 mounted() {
        this.initPopup();
    },
 beforeDestory() {
      if (this.times) {
          clearInterval(this.times);
      }
  },
  methods:{
  onMmouseover() {
    if (this.times) {
            clearInterval(this.times);
        }
    },
    onMouseout() {
      
        if (this.times) {
            clearInterval(this.times);
        }
        this.initPopup();
    },
    initPopup() {
        let count = 11; //速度
        // let count = 500         //速度
        let stepX = 1;
        let stepY = 1;
        let pop = document.getElementById("popupDialog");
        let popWidth = pop.offsetWidth;
        let popHeight = pop.offsetHeight;
        let clientw = document.body.clientWidth;
        let clienth = document.body.clientHeight;
        let x = parseInt(pop.getBoundingClientRect().left);
        let y = parseInt(pop.getBoundingClientRect().top);
        this.times = setInterval(() => {
            let distenceX = clientw - x;
            let distenceY = clienth - y;
            if (distenceX - popWidth < 0 || distenceX > clientw) {
                stepX = -stepX;
            }
            if (distenceY - popHeight < 0 || distenceY > clienth) {
                stepY = -stepY;
            }
            x += stepX;
            y += stepY;
            this.changePos(pop, x, y);
        }, count);
    },
    changePos(pop, x, y) {
        pop.style.left = x + "px";
        pop.style.top = y + "px";
    },
  }
  

3.css

javascript 复制代码
.popup-dialog {
    position: fixed;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    z-index: 999;
    background: #ccc;
}
相关推荐
yuki_uix1 天前
别让 AI 骗了:这些状态管理工具真的适合你吗?
前端·ai编程
日月云棠1 天前
UE5 打包后 EXE 程序单实例的两种实现方法
前端·c++
滕青山1 天前
Base64编码/解码 核心JS实现
前端·javascript·vue.js
Novlan11 天前
@tdesign/uniapp 常见问题
前端
sww_10261 天前
SAA ReactAgent工作原理
开发语言·前端·javascript
linux_cfan1 天前
拒绝“黑屏”与“哑剧”:Web视频播放器UX体验与自动播放选型指南 (2026版)
前端·javascript·音视频·html5·ux
小庄梦蝶1 天前
宝塔使用nodejs管理器下载nodejs版本失败解决方式之一
linux·运维·前端
be or not to be1 天前
假期js学习汇总
前端·javascript·学习
SuperEugene1 天前
日期与时间处理:不用库和用 dayjs 的两种思路
前端·javascript
rfidunion1 天前
springboot+VUE+部署(13。创建多表查询)
vue.js·spring boot·后端