【vue】 vue2 实现飘窗效果

简介: 【vue】 vue2 实现飘窗效果


具体效果

飘窗效果

代码

Baywindow.vue

<template>
  <!-- 飘窗效果 -->
  <div
    id="thediv"
    ref="thediv"
    style="position: absolute; z-index: 111; left: 0; top: 0"
    v-show="thedivShow"
    @mouseover="clearFdAd"
    @mouseout="starFdAd"
  >
    <div
      style="
        cursor: pointer;
        text-align: right;
        font-size: 12px;
        color: #999999;
      "
      @click="thedivShow = false"
    >
      关闭
    </div>
    <a href="http://www.baidu.com" target="_blank"
      ><img src="../assets/111.jpg" width="320" border="0"
    /></a>
  </div>
</template>
<script>
var interval;
export default {
  data() {
    return {
      xPos: 0,
      yPos: 0,
      xin: true,
      yin: true,
      step: 1,
      delay: 18,
      height: 0,
      Hoffset: 0,
      Woffset: 0,
      yon: 0,
      xon: 0,
      pause: true,
      thedivShow: true,
    };
  },
  mounted() {
    interval = setInterval(this.changePos, this.delay);
  },
  methods: {
    changePos() {
      let width = document.documentElement.clientWidth;
      let height = document.documentElement.clientHeight;
      this.Hoffset = this.$refs.thediv.offsetHeight; //获取元素高度
      this.Woffset = this.$refs.thediv.offsetWidth;
      // 滚动部分跟随屏幕滚动
      // this.$refs.thediv.style.left = (this.xPos + document.body.scrollLeft + document.documentElement.scrollLeft) + "px";
      // this.$refs.thediv.style.top = (this.yPos + document.body.scrollTop + document.documentElement.scrollTop) + "px";
      // 滚动部分不随屏幕滚动
      this.$refs.thediv.style.left =
        this.xPos + document.body.scrollLeft + "px";
      this.$refs.thediv.style.top = this.yPos + document.body.scrollTop + "px";
      if (this.yon) {
        this.yPos = this.yPos + this.step;
      } else {
        this.yPos = this.yPos - this.step;
      }
      if (this.yPos < 0) {
        this.yon = 1;
        this.yPos = 0;
      }
      if (this.yPos >= height - this.Hoffset) {
        this.yon = 0;
        this.yPos = height - this.Hoffset;
      }
      if (this.xon) {
        this.xPos = this.xPos + this.step;
      } else {
        this.xPos = this.xPos - this.step;
      }
      if (this.xPos < 0) {
        this.xon = 1;
        this.xPos = 0;
      }
      if (this.xPos >= width - this.Woffset) {
        this.xon = 0;
        this.xPos = width - this.Woffset;
      }
    },
    clearFdAd() {
      clearInterval(interval);
    },
    starFdAd() {
      interval = setInterval(this.changePos, this.delay);
    },
  },
};
</script>
<style lang="scss" scoped>
#thediv {
  z-index: 100;
  position: absolute;
  top: 43px;
  left: 2px;
  height: 184px;
  width: 280px;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>

注册使用

全局注册 在main.js中

// 注册为全局组件
import Baywindow from "@/views/Baywindow.vue";
Vue.component("Baywindow", Baywindow);

局部注册

<template>
  <div class="home">
    <Baywindow></Baywindow>
  </div>
</template>
<script>
import Baywindow from "@/views/Baywindow.vue";
export default {
  name: "Home",
  components: {
    Baywindow,
  },
};
</script>


目录
相关文章
|
4天前
|
缓存 监控 JavaScript
探讨优化Vue应用性能和加载速度的策略
【5月更文挑战第17天】本文探讨了优化Vue应用性能和加载速度的策略:1) 精简代码和组件拆分以减少冗余;2) 使用计算属性和侦听器、懒加载、预加载和预获取优化路由;3) 数据懒加载和防抖节流处理高频事件;4) 图片压缩和选择合适格式,使用CDN加速资源加载;5) 利用浏览器缓存和组件缓存提高效率;6) 使用Vue Devtools和性能分析工具监控及调试。通过这些方法,可提升用户在复杂应用中的体验。
14 0
|
4天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
3天前
|
JavaScript 开发工具 git
Vue 入门系列:.env 环境变量
Vue 入门系列:.env 环境变量
10 1
|
4天前
|
JavaScript 前端开发 定位技术
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
|
4天前
|
JavaScript
Vue中避免滥用this去读取data中数据
Vue中避免滥用this去读取data中数据
|
4天前
|
JavaScript
vue中使用pinia及持久化
vue中使用pinia及持久化
7 0
|
4天前
|
JavaScript 前端开发 算法
Vue3与Vue2:对比分析与迁移指南
Vue3与Vue2:对比分析与迁移指南
|
4天前
|
JavaScript 前端开发 UED
Vue class和style绑定:动态美化你的组件
Vue class和style绑定:动态美化你的组件
|
4天前
|
JavaScript 前端开发 API
Vue 监听器:让你的应用实时响应变化
Vue 监听器:让你的应用实时响应变化
|
4天前
|
JavaScript
vue封装svg
vue封装svg
10 0