当前位置:首页 > VUE

vue实现漂浮广告

2026-02-19 20:48:45VUE

Vue 实现漂浮广告

在 Vue 中实现漂浮广告可以通过动态绑定样式、监听窗口滚动事件以及使用 CSS 动画来实现。以下是具体实现方法:

使用动态样式绑定

通过 Vue 的 v-bind:style 动态绑定广告元素的位置,结合 position: fixedposition: absolute 实现漂浮效果。

<template>
  <div class="ad-container" :style="{ top: adTop + 'px', left: adLeft + 'px' }">
    <img src="ad-image.png" alt="广告" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      adTop: 100,
      adLeft: 100,
    };
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
      this.adTop = window.scrollY + 100;
    },
  },
};
</script>

<style>
.ad-container {
  position: fixed;
  z-index: 999;
}
</style>

使用 CSS 动画

通过 CSS 的 @keyframes 实现广告的漂浮动画效果。

<template>
  <div class="floating-ad">
    <img src="ad-image.png" alt="广告" />
  </div>
</template>

<style>
.floating-ad {
  position: fixed;
  bottom: 20px;
  right: 20px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
  100% {
    transform: translateY(0);
  }
}
</style>

结合 Vue Transition

使用 Vue 的 <transition> 组件实现广告的淡入淡出效果。

<template>
  <transition name="fade">
    <div v-if="showAd" class="ad-container">
      <img src="ad-image.png" alt="广告" @click="closeAd" />
    </div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      showAd: true,
    };
  },
  methods: {
    closeAd() {
      this.showAd = false;
    },
  },
};
</script>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
.ad-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
}
</style>

使用第三方库

如果需要更复杂的动画效果,可以引入第三方库如 gsapanime.js

vue实现漂浮广告

<template>
  <div ref="ad" class="ad-container">
    <img src="ad-image.png" alt="广告" />
  </div>
</template>

<script>
import { gsap } from 'gsap';

export default {
  mounted() {
    gsap.to(this.$refs.ad, {
      duration: 2,
      x: 100,
      y: 50,
      repeat: -1,
      yoyo: true,
    });
  },
};
</script>

<style>
.ad-container {
  position: fixed;
  bottom: 20px;
  left: 20px;
}
</style>

注意事项

  • 漂浮广告可能会影响用户体验,建议添加关闭按钮。
  • 移动端适配时需考虑屏幕尺寸和触摸事件。
  • 广告内容应避免遮挡页面重要元素。

标签: 广告vue
分享给朋友:

相关文章

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue实现 页面

vue实现 页面

Vue 实现页面的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 是官方脚手架工具,适合传统项目;Vite 提供更快的现代构建体验。 npm cre…

vue实现绘图

vue实现绘图

Vue 实现绘图的方法 在 Vue 中实现绘图功能,可以通过多种方式实现,包括使用原生 HTML5 Canvas、第三方库(如 Fabric.js、Konva.js)或 SVG。以下是几种常见的实现方…