news 2026/9/2 14:34:30

Vue视差标题背景

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue视差标题背景

一、说明

这个我是为了放在博客的标题部分作为背景图,上下滚动的时候比较好看。

原理就是通过几张透明的png进行叠加,然后在上下滚动时,外层png移动的快,内层png移动得慢来实现视差效果。

先放一张示意图:

这里实际的距离X和Y在观察者看来是一样的,原因是距离观察者的距离Z不一样导致的。

视差示意图.png

再放一张视差标题背景的3d示意图:

实际示意图

二、Vue代码

这里为了代码高亮分三部分展示

html部分

<template>

<section>

<img :src="p0Src" id="p0" :style="{ transform: `translateY(${p0Top}px)`, zIndex: `100` }" alt="p0">

<img :src="p1Src" id="p1" :style="{ transform: `translateY(${p1Top}px)`, zIndex: `200` }" alt="p1">

<img :src="p2Src" id="p2" :style="{ transform: `translateY(${p2Top}px)`, zIndex: `300` }" alt="p2">

<img :src="p3Src" id="p3" :style="{ transform: `translateY(${p3Top}px)`, zIndex: `400` }" alt="p3">

<img :src="p4Src" id="p4" :style="{ transform: `translateY(${p4Top}px)`, zIndex: `500` }" alt="p4">

<img :src="p6Src" id="p6" :style="{ transform: `translateY(${p6Top}px)`, zIndex: `600` }" alt="p6">

<div id="banner_title"

class="container"

:style="{ marginRight: `0px`, marginTop: `${bannerTitleMarginTop}px`, width: `75%`}">

<h1>{{blogTitle}}</h1>

<p class="description">{{blogDescription}}</p>

</div>

</section>

</template>

js部分

<script>

const imgUrl = "https://xxxxxxxxxxxx/";

export default {

name: "Banner",

data() {

return {

p0Src: imgUrl + 'banner/ppp0.png',

p1Src: imgUrl + 'banner/pp1.png',

p2Src: imgUrl + 'banner/pp2.png',

p3Src: imgUrl + 'banner/pp3.png',

p4Src: imgUrl + 'banner/pp4.png',

p6Src: imgUrl + 'banner/pp6.png',

p0Top: 0,

p1Top: 0,

p2Top: 0,

p3Top: 0,

p4Top: 0,

p6Top: 0,

bannerTitleMarginTop: -100,

requestId: undefined, // 用于跟踪 requestAnimationFrame 的标识

};

},

props:{

blogTitle:{

type: String,

require: true

},

blogDescription:{

type: String,

require: true

},

},

mounted() {

this.addScrollListener();

},

beforeDestroy() {

this.removeScrollListener();

},

methods: {

addScrollListener() {

// 使用 passive 参数优化滚动性能

window.addEventListener('scroll', this.handleScroll, {passive: true});

},

removeScrollListener() {

window.removeEventListener('scroll', this.handleScroll);

},

handleScroll() {

const value = window.scrollY;

this.p0Top = value * 0.6;

this.p1Top = value * 0.36;

this.p2Top = value * 0.24;

this.p3Top = value * 0.16;

this.p4Top = value * 0.12;

this.p6Top = 0;

this.bannerTitleMarginTop = value * 1.1 - 100;

// 使用 requestAnimationFrame 更新样式

if (this.requestId === undefined) {

this.requestId = requestAnimationFrame(this.updateStyles);

}

},

updateStyles() {

// 清除请求动画帧标识

this.requestId = undefined;

}

}

}

</script>

style部分

<style scoped lang="scss">

section {

position: relative;

width: 100%;

height: 100vh;

padding: 0px;

display: flex;

justify-content: center;

align-items: center;

overflow: hidden;

img {

position: absolute;

top: 0;

left: 0;

width: 100%;

//height: 100%;

object-fit: cover;

pointer-events: none;

will-change: transform; // 提前告知浏览器哪些属性可能会发生变化

}

#p4,#p3,#p2,#p1,#p0 {

width: 100%;

height: 100%;

}

#p6 {

width: 100%;

height: 150%;

}

section::before {

content: '';

position: absolute;

bottom: 0;

width: 100%;

//height: 100px;

background: linear-gradient(to top, #1c0522, transparent);

//z-index: 1000;

}

}

@-webkit-keyframes bounce {

0%,10%,25%,40%,50% {

-webkit-transform: translateY(0) rotate(0deg);

transform: translateY(0) rotate(0deg)

}

20% {

-webkit-transform: translateY(-10px) rotate(0deg);

transform: translateY(-10px) rotate(0deg)

}

30% {

-webkit-transform: translateY(-5px) rotate(0deg);

transform: translateY(-5px) rotate(0deg)

}

}

@keyframes bounce {

0%,10%,25%,40%,50% {

-webkit-transform: translateY(0) rotate(0deg);

transform: translateY(0) rotate(0deg)

}

20% {

-webkit-transform: translateY(-10px) rotate(0deg);

transform: translateY(-10px) rotate(0deg)

}

30% {

-webkit-transform: translateY(-5px) rotate(0deg);

transform: translateY(-5px) rotate(0deg)

}

}

</style>

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 19:33:12

记一次 .NET 某光放测试系统 崩溃分析

一&#xff1a;背景 1. 讲故事 微信好友里有位朋友找到我&#xff0c;说他部署在windows上的程序&#xff0c;用debug模式正常&#xff0c;但用 release 模式跑程序就崩溃&#xff0c;如果把程序切到 .NET6 的话又都正常&#xff0c;所以很迷茫&#xff0c;让我看看怎么回事&…

作者头像 李华
网站建设 2026/9/2 12:30:26

9 款 AI 写论文哪个好?实测对比后,这款全流程神器成学术党首选! 毕业论文写作季,AI 写作工具已成学子 “救命稻草”。市面上百度智能云千帆大模型、科大讯

毕业论文写作季&#xff0c;AI 写作工具已成学子 “救命稻草”。市面上百度智能云千帆大模型、科大讯飞星火认知大模型、通义千问、文心一言、ChatGPT、豆包、WPS AI、Grammarly GO、虎贲等考 AI 等 9 款主流工具扎堆&#xff0c;“9 款 AI 写论文哪个好” 成为全网热议的焦点。…

作者头像 李华
网站建设 2026/9/2 5:15:24

深入解析 Apache Commons IO:IOUtils 工具类

前言 在 Java 开发中&#xff0c;输入/输出&#xff08;I/O&#xff09;操作是日常编程中最常见但也最容易出错的部分之一。无论是读取文件、处理网络响应&#xff0c;还是在内存中操作字节流与字符流&#xff0c;开发者常常需要编写大量样板代码来管理资源、处理异常、转换编…

作者头像 李华