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;
}
相关推荐
前端W2 分钟前
腾讯地图组件使用说明文档
前端
页面魔术4 分钟前
无虚拟dom怎么又流行起来了?
前端·javascript·vue.js
胡gh4 分钟前
如何聊懒加载,只说个懒可不行
前端·react.js·面试
Double__King7 分钟前
巧用 CSS 伪元素,让背景图自适应保持比例
前端
Mapmost9 分钟前
【BIM+GIS】BIM数据格式解析&与数字孪生适配的关键挑战
前端·vue.js·three.js
一涯10 分钟前
写一个Chrome插件
前端·chrome
鹧鸪yy17 分钟前
认识Node.js及其与 Nginx 前端项目区别
前端·nginx·node.js
跟橙姐学代码18 分钟前
学Python必须迈过的一道坎:类和对象到底是什么鬼?
前端·python
汪子熙20 分钟前
浏览器里出现 .angular/cache/19.2.6/abap_test/vite/deps 路径究竟说明了什么
前端·javascript·面试