vue实现web端飘窗组件

项目中需要实现飘窗效果

效果如下:

 

 一、封装飘窗组件FloatWidow.vue,放在目录/components下 

<template>
  <!--悬浮窗口样式的提示信息-->
  <!--  @mouseover:当鼠标移入是触发,移入和移出其子元素时候也会触发
        @mouseout:当鼠标移出元素时候触发,移入和移出其子元素也会触发
      -->
  <div id="theDiv" ref="theDiv" @mouseover="clearFdAd" style="position: absolute" @mouseout="starFdAd" v-show="theDivShow">
    <div>
      <div style="position: absolute; cursor: pointer; right: 0; font-size: 12px; color: white" @click="theDivShow = false">关闭</div>
      <!--  图片的长宽  -->
      <div
        :style="{
          width: `${width}px`,
          height: `${height}px`
        }"
      >
        <a :href="item.href" target="_blank">
          <img :src="item.image" style="border: 0" alt="" />
        </a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'FloatingWindow',
  props: {
    item: {
      type: Object,
      default() {
        return {}
      }
    },
    width: {
      type: Number,
      required: true
    },
    height: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      // 最顶层左右移动,数值越大越靠右
      xPos: 0,
      // 靠左上下移动,数值越大起始位置越靠下
      yPos: 0,
      // 移动速度,数值越大移动速度越快
      step: 1,
      // 周期性执行或者调用code之间的时间间隔
      delay: 15,
      // height: 0,
      Hoffset: 0,
      Woffset: 0,
      // 与yPos结合使用,0代表向上移动,1代表向下移动
      yon: 0,
      // 与xPos结合使用,0代表向右下方移动,1代表向左下方移动
      xon: 0,
      pause: true,
      // 开启窗口
      theDivShow: true,
      start: ''
    }
  },
  watch: {
    theDivShow() {
      console.log(this.theDivShow)
    }
  },
  mounted() {
    // 第一个飘动窗口
    // setInterval: 会不停的调用函数,直到clearInterval被调用或者窗口被关闭。由setInterval返回的ID值可用做clearInterval方法的参数
    // setInterval(code,millisec[,"lang"])
    // code: 必需的。要调用的函数或者要执行的代码串
    // millisec:必需的.周期性执行或者调用code之间的时间间隔,以毫秒计算
    this.start = setInterval(this.changePos, this.delay)
  },
  created() {
    this.xPos = Math.round(Math.random() * 1670) // 生成0~1670的随机整数
    this.yPos = Math.round(Math.random() * 868) // 生成0~868的随机整数
    this.yon = Math.round(Math.random()) // 生成0~1的随机整数
    this.xon = Math.round(Math.random()) // 生成0~1的随机整数
  },
  methods: {
    // 飘动窗口配置
    changePos() {
      // 网页可见区域的宽度
      let width = document.documentElement.clientWidth
      // 网页可见区域的高度
      let height = document.documentElement.clientHeight
      // 获取标签元素的高度
      this.Hoffset = this.$refs.theDiv.offsetHeight
      // 获取标签元素的宽度
      this.Woffset = this.$refs.theDiv.offsetWidth

      // 滚动部分跟随屏幕滚动
      // document.body.scrollLeft 网页被卷去的左
      // scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
      // document.documentElement.scrollLeft : 设置或获取页面文档向右滚动过的像素数
      this.$refs.theDiv.style.left = this.xPos + document.body.scrollLeft + document.documentElement.scrollLeft + 'px'
      // document.body.scrollTop 网页被卷去的高
      // scrollTop:设置或获取位于对象对顶端和窗口中可见内容的最顶端之间的距离
      // document.documentElement.scrollTop :设置或获取页面文档向下滚动过的像素数
      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:可以取消由setInterval设置的timeout时间间隔。参数必需是setInterval返回的ID值-->
      clearInterval(this.start)
    },
    starFdAd() {
      this.start = setInterval(this.changePos, this.delay)
    }
  }
}
</script>

<style lang="scss" scoped>
#theDiv {
  z-index: 100;
  position: absolute;
  background: red;
  //overflow: hidden;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>

其中数据可以根据需求进行修改

二、在页面引入使用

1.页面组件引入并挂在到components

import FloatingWindow from '@/components/website/components/FloatingWindow'

2.页面上使用

目前为死数据,可以跟据接口需求进性修改

<!-- 飘浮框   -->
    <div v-for="(item, idx) in item5" :key="idx">
      <FloatingWindow :item="item" :height="100" :width="250"></FloatingWindow>
    </div>
item5: [
        {
          id: 1,
          href: 'https://cn.bing.com/search?q=%E5%9B%BE%E7%89%87&form=ANNTH1&refig=94e881aecf564f74981db9329e90c570',
          image: 'https://pic3.zhimg.com/v2-58d652598269710fa67ec8d1c88d8f03_r.jpg?source=1940ef5c'
        },
        {
          id: 2,
          href: 'https://cn.bing.com/search?q=%E5%9B%BE%E7%89%87&form=ANNTH1&refig=94e881aecf564f74981db9329e90c570',
          image:
            'https://ts1.cn.mm.bing.net/th/id/R-C.8df10ff001c5d2c11e217ad37b03d490?rik=gCRKgEmLlUljMQ&riu=http%3a%2f%2fup.deskcity.org%2fpic_source%2f8d%2ff1%2f0f%2f8df10ff001c5d2c11e217ad37b03d490.jpg&ehk=LdJrVIHRRs0UaUVhS1wKrQoppQwkP43HTAyU9p72oA8%3d&risl=&pid=ImgRaw&r=0'
        }
      ]

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue.js 是一种用于构建用户界面的 JavaScript 框架,它提供了一种基于组件的开发模式,可用于构建单页面应用程序。在 Vue.js实现 Web 端的定位功能获取经纬度可以通过使用 HTML5 的 Geolocation API 来实现。 Geolocation API 是一种 HTML5 标准,允许 Web 应用程序使用 JavaScript 与浏览器通信并访问用户的地理位置信息。在 Vue.js 中,您可以使用 Geolocation API 获取用户的地理位置数据,并使用 Vue.js组件和生命周期方法来处理数据和更新 UI。以下是实现步骤: 首先,在您的 Vue.js 应用程序中,您需要创建一个组件来获取用户的位置。在这个组件中,您可以通过调用浏览器的 Geolocation API 来获取用户的位置信息。 ``` <template> <div> <button @click="getLocation">获取位置信息</button> <div v-if="lontitude && latitude"> 经度: {{longitude}} 纬度: {{latitude}} </div> </div> </template> <script> export default { data() { return { longitude: null, latitude: null } }, methods: { getLocation() { navigator.geolocation.getCurrentPosition(position => { this.longitude = position.coords.longitude this.latitude = position.coords.latitude }) } } } </script> ``` 这个组件包含一个按钮,当用户单击该按钮时将调用 getLocation 方法。该方法将调用 Geolocation API 中的 getCurrentPosition 方法,这将向用户请求权限以访问其位置信息。如果用户同意,浏览器将获取用户的位置信息,并将其保存在该组件的 data 对象中。 现在,我们已经获取到了用户的位置信息。您可以使用这些信息来更新 UI 并执行其他操作。例如,您可以使用这些信息来显示地图或定向用户的位置。 综上所述,Vue.js 使用 Geolocation API 可以轻松获取用户的当前位置信息,并可使用 Vue.js组件和生命周期方法进一步处理信息和更新 UI。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值