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;
}
相关推荐
hpoenixf6 小时前
2026 年前端面试问什么
前端·面试
还是大剑师兰特6 小时前
Vue3 中的 defineExpose 完全指南
前端·javascript·vue.js
泯泷6 小时前
阶段一:从 0 看懂 JSVMP 架构,先在脑子里搭出一台最小 JSVM
前端·javascript·架构
mengchanmian7 小时前
前端node常用配置
前端
华洛7 小时前
利好打工人,openclaw不是企业提效工具,而是个人助理
前端·javascript·产品经理
xkxnq7 小时前
第六阶段:Vue生态高级整合与优化(第93天)Element Plus进阶:自定义主题(变量覆盖)+ 全局配置与组件按需加载优化
前端·javascript·vue.js
A黄俊辉A8 小时前
vue css中 :global的使用
前端·javascript·vue.js
小码哥_常8 小时前
被EdgeToEdge适配折磨疯了,谁懂!
前端
小码哥_常9 小时前
从Groovy到KTS:Android Gradle脚本的华丽转身
前端
灵感__idea9 小时前
Hello 算法:复杂问题的应对策略
前端·javascript·算法