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;
}
相关推荐
BillKu8 分钟前
npm 安装命令中关于 @ 的讲解,如:npm install @vue-office/docx vue-demi
前端·vue.js·npm
bestadc12 分钟前
LeetCode 几道 Promises 和 Time 的题目
javascript·算法·leetcode
yangzhi_emo21 分钟前
ES6笔记4
前端·笔记·es6
萌萌哒草头将军27 分钟前
Node.js v24.8.0 新功能预览!🚀🚀🚀
前端·javascript·node.js
超人不会飛30 分钟前
大模型应用 Vue H5 模板:快速落地流式交互与富文本渲染的开箱方案
前端·vue.js·github
用户4582031531731 分钟前
CSS无需JavaScript的交互效果实现
前端·css
影i33 分钟前
在 Vue + Codemirror 中优雅回显 JSON
前端
奇怪的前端733 分钟前
Alien-Signals 响应式系统
前端·vue.js
DLF_ou38 分钟前
vue3+openlayers项目初始化
vue.js
你单排吧38 分钟前
Electron打包图标修改失败问题
前端