当前位置:首页 > VUE

vue实现crt窗口

2026-02-20 13:09:15VUE

Vue 实现 CRT 窗口效果

在 Vue 中实现 CRT(阴极射线管)风格的复古屏幕效果,可以通过 CSS 动画、滤镜和伪元素模拟扫描线、像素化和辉光等视觉特征。以下是具体实现方法:

基础 HTML 结构

创建一个 Vue 组件模板,包含需要展示 CRT 效果的内容区域:

<template>
  <div class="crt-screen">
    <div class="crt-content">
      <!-- 你的内容在这里 -->
      <slot></slot>
    </div>
  </div>
</template>

核心 CSS 样式

通过 CSS 实现扫描线、像素化和辉光效果:

.crt-screen {
  position: relative;
  background-color: #000;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 30px rgba(0, 255, 0, 0.3);
  overflow: hidden;
}

.crt-content {
  position: relative;
  color: #0f0; /* 经典绿色文本 */
  font-family: 'Courier New', monospace;
}

/* 扫描线效果 */
.crt-screen::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    rgba(0, 255, 0, 0.1) 1px,
    transparent 1px
  );
  background-size: 100% 2px;
  pointer-events: none;
  z-index: 1;
  animation: scanline 8s linear infinite;
}

/* 像素化/模糊效果 */
.crt-content {
  text-shadow: 0 0 5px rgba(0, 255, 0, 0.8);
  filter: blur(0.5px);
}

/* 辉光动画 */
@keyframes glow {
  0% { box-shadow: 0 0 10px rgba(0, 255, 0, 0.5); }
  50% { box-shadow: 0 0 20px rgba(0, 255, 0, 0.8); }
  100% { box-shadow: 0 0 10px rgba(0, 255, 0, 0.5); }
}

/* 扫描线动画 */
@keyframes scanline {
  0% { background-position: 0 0; }
  100% { background-position: 0 100%; }
}

添加交互效果

在 Vue 组件中添加鼠标悬停时的辉光增强效果:

<script>
export default {
  data() {
    return {
      isHovering: false
    }
  },
  methods: {
    handleHover(isHover) {
      this.isHovering = isHover;
    }
  }
}
</script>

更新模板和样式以响应悬停状态:

<template>
  <div 
    class="crt-screen" 
    :class="{ 'crt-glow': isHovering }"
    @mouseenter="handleHover(true)"
    @mouseleave="handleHover(false)"
  >
    <!-- ... -->
  </div>
</template>
.crt-glow {
  animation: glow 1.5s ease-in-out infinite;
}

高级效果增强

通过 JavaScript 动态生成噪点效果,模拟老式显示器的信号干扰:

mounted() {
  this.generateNoise();
},
methods: {
  generateNoise() {
    const noiseCanvas = document.createElement('canvas');
    noiseCanvas.width = this.$el.offsetWidth;
    noiseCanvas.height = this.$el.offsetHeight;
    const ctx = noiseCanvas.getContext('2d');

    for (let x = 0; x < noiseCanvas.width; x++) {
      for (let y = 0; y < noiseCanvas.height; y++) {
        const val = Math.floor(Math.random() * 10);
        ctx.fillStyle = `rgba(0, 255, 0, ${val/100})`;
        ctx.fillRect(x, y, 1, 1);
      }
    }

    const noise = document.createElement('div');
    noise.style.backgroundImage = `url(${noiseCanvas.toDataURL()})`;
    noise.style.position = 'absolute';
    noise.style.top = '0';
    noise.style.left = '0';
    noise.style.width = '100%';
    noise.style.height = '100%';
    noise.style.pointerEvents = 'none';
    noise.style.opacity = '0.05';
    noise.style.animation = 'flicker 0.1s infinite';

    this.$el.appendChild(noise);
  }
}

添加对应的 CSS 动画:

@keyframes flicker {
  0% { opacity: 0.05; }
  50% { opacity: 0.1; }
  100% { opacity: 0.05; }
}

响应式调整

在 Vue 中添加窗口大小变化的监听,确保效果适应不同尺寸:

data() {
  return {
    resizeObserver: null
  }
},
mounted() {
  this.resizeObserver = new ResizeObserver(() => {
    this.generateNoise();
  });
  this.resizeObserver.observe(this.$el);
},
beforeDestroy() {
  this.resizeObserver.disconnect();
}

通过以上方法,可以在 Vue 中实现一个逼真的 CRT 窗口效果,包含扫描线、像素化、辉光和噪点等经典特征。用户可以通过调整 CSS 参数(如扫描线密度、辉光强度等)进一步自定义视觉效果。

vue实现crt窗口

标签: 窗口vue
分享给朋友:

相关文章

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <…

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…