Vue飘窗效果(css飘窗效果,跟随页面滚动)

72 篇文章 2 订阅
8 篇文章 1 订阅

简述:飘窗效果,相信大家都见过,一般用于官网页面,下面给大家整理了下,飘窗效果在vue中的使用,效果图如下;

1、话不多说,直接上代码,HTML部分;

<template>
  <!-- 飘窗效果 -->
  <div
    id="thediv"
    ref="thediv"
    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/logo.png" width="320" border="0" /> -->
      <div class="tips" v-for="(cur, i) in defineIndexInfo" :key="i">
        <div v-html="cur.noticeContent"></div>
      </div>
    </a>
  </div>
</template>

2、CSS

#thediv {
  z-index: 100;
  position: absolute;
  top: 0px;
  left: 0px;
}

3、JS(vue.js)

  data() {
    return {
      //定义相关参数
      xPos: 0,
      yPos: 0,
      xin: true,
      yin: true,
      step: 3,
      delay: 18,
      height: 0,
      Hoffset: 0,
      Woffset: 0,
      yon: 0,
      xon: 0,
      pause: true,
      thedivShow: true,
    };
  },

  created() {
    this.callLabList();//调用执行
  },
  mounted() {
    interval = setInterval(this.changePos, this.delay);
  },
  methods: {
    //滚动逻辑方法
    changePos() {
      let width = document.documentElement.clientWidth;
      let height = document.documentElement.clientHeight;
      if (this.$refs.thediv) {
        this.Hoffset = this.$refs.thediv.offsetHeight; //获取元素高度
        this.Woffset = this.$refs.thediv.offsetWidth;
        // 滚动部分不随屏幕滚动
        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;
        }
      }
      // 滚动时跟随屏幕滚动 需要时解开注释
      // 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";
    },
    clearFdAd() {
      clearInterval(interval);
    },
    starFdAd() {
      interval = setInterval(this.changePos, this.delay);
    },
  },

4、组件引入调用;

import Baywindow from "@/components/Baywindow.vue";

components: {
    Baywindow,
  },

<!-- 飘窗 -->
<Baywindow/>

感觉有用,就一键三连,感谢(●'◡'●)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城笑笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值