英文:
How to set Image to text background with parallax effect
问题
以下是您提供的内容的翻译:
我试图实现文本背景作为图像的效果。图像应具有视差效果。
我尝试了下面的代码,但无法按照我想要的方式工作。请建议使用CSS / JS的最佳解决方案,无论哪种对这种情况更好。
是否有其他可用的替代方法来实现这一点或任何技巧?我甚至尝试了遮罩,但没有实现目标。
以下是代码
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let mouseX, mouseY, trackX, trackY;
document.addEventListener("wheel", function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(".title").style.backgroundPosition = `${trackX}% ${trackY}%`
});
<!-- language: lang-css -->
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 200vmin;
font-size: 100%;
font-family: 'Anton', sans-serif;
}
body {
background: #1e5abc;
}
.hero {
background: linear-gradient(rgb(235, 236, 231), rgb(255, 255, 255));
height: 100vh;
width: 100%;
display: flex;
flex-wrap: wrap;
position: relative;
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 50% 100%, 0% 85%, 0% 0%);
}
.container {
flex: 1 1 50%;
display: flex;
justify-content: center;
align-items: center;
}
.right-side-container {
flex: 1 1 50%;
clip-path: url(#clipPath);
}
.title {
background: url("https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2b76b1e6da83c1a0f59eebaa481e12aa&auto=format&fit=crop&w=751&q=80") center no-repeat;
background-position: 40% 50%;
-webkit-background-clip: text;
color: transparent;
text-transform: uppercase;
letter-spacing: 0.1em;
line-height: 1.3em;
text-anchor: middle;
alignment-baseline: hanging;
font-size: calc(12px + 6.2vw);
text-align: center;
}
.subtitle {
text-align: center;
letter-spacing: 0.35em;
font-size: 1.5vw;
color: #2b2b2b;
}
img {
height: 100%;
width: 100%;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hero">
<div class="container">
<div class="header-container">
<br><br><br>
<br><br><br>
<div class="title">
I'm Travis.<br>
Developer.
</div>
<div class="subtitle">
<p>Frontend - backend - everything</p>
</div>
</div>
</div>
<div class="right-side-container">
</div>
</div>
<!-- end snippet -->
希望这个翻译对您有所帮助。
英文:
I am trying to achieve the effect for text background as image. Image should be with parallax effect.
I tried below code but not able to work like how I want. Please suggest the best solution with CSS / JS whichever is better for this case.
Is there any alternatives avail to do this or any tricks I even tried masking, but not achieved goal.
Code Below
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let mouseX, mouseY, trackX, trackY;
document.addEventListener("wheel", function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(".title").style.backgroundPosition = `${trackX}% ${trackY}%`
});
<!-- language: lang-css -->
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 200vmin;
font-size: 100%;
font-family: 'Anton', sans-serif;
}
body {
background: #1e5abc;
}
.hero {
background: linear-gradient(rgb(235, 236, 231), rgb(255, 255, 255));
height: 100vh;
width: 100%;
display: flex;
flex-wrap: wrap;
position: relative;
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 50% 100%, 0% 85%, 0% 0%);
}
.container {
flex: 1 1 50%;
display: flex;
justify-content: center;
align-items: center;
}
.right-side-container {
flex: 1 1 50%;
clip-path: url(#clipPath);
}
.title {
background: url("https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2b76b1e6da83c1a0f59eebaa481e12aa&auto=format&fit=crop&w=751&q=80") center no-repeat;
background-position: 40% 50%;
-webkit-background-clip: text;
color: transparent;
text-transform: uppercase;
letter-spacing: 0.1em;
line-height: 1.3em;
text-anchor: middle;
alignment-baseline: hanging;
font-size: calc(12px + 6.2vw);
text-align: center;
}
.subtitle {
text-align: center;
letter-spacing: 0.35em;
font-size: 1.5vw;
color: #2b2b2b;
}
img {
height: 100%;
width: 100%;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hero">
<div class="container">
<div class="header-container">
<br><br><br>
<br><br><br>
<div class="title">
I'm Travis.<br>
Developer.
</div>
<div class="subtitle">
<p>Frontend - backend - everything</p>
</div>
</div>
</div>
<div class="right-side-container">
</div>
</div>
<!-- end snippet -->
答案1
得分: 0
Sure, here's the translated code part:
.title {
background-attachment: fixed;
}
Please let me know if you need any further translations.
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
window.addEventListener('scroll', function() {
let offset = window.pageYOffset;
document.querySelector('.title').style.backgroundPositionY = offset * 0.7 + 'px';
});
<!-- language: lang-css -->
.title {
background-image: url('https://cdn.firstcry.com/education/2022/04/25155522/1378635314.jpg');
background-size: cover;
background-attachment: fixed;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
background-repeat: no-repeat;
}
.title-text {
position: relative;
top: 50%;
transform: translateY(-50%);
text-align: center;
color: white;
padding: 10px;
}
<!-- language: lang-html -->
<div class="title">
<div class="title-text">
<h1>I'm Travis</h1>
<h2>
The developer
</h2>
</div>
</div>
<!-- end snippet -->
put this line in css
.title{
background-attachment: fixed;
}
答案2
得分: 0
以下是代码部分的中文翻译:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
let mouseX, mouseY, trackX, trackY;
document.addEventListener("wheel", function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(".title").style.backgroundPosition = `${trackX}% ${trackY}%`
});
<!-- 语言: lang-css -->
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 200vmin;
font-size: 100%;
font-family: 'Anton', sans-serif;
}
body {
background: #1e5abc;
}
.hero {
background: linear-gradient(rgb(235, 236, 231), rgb(255, 255, 255));
height: 100vh;
width: 100%;
display: flex;
flex-wrap: wrap;
position: relative;
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 50% 100%, 0% 85%, 0% 0%);
}
.container {
flex: 1 1 50%;
display: flex;
justify-content: center;
align-items: center;
}
.right-side-container {
flex: 1 1 50%;
clip-path: url(#clipPath);
}
.title {
background: url("https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2b76b1e6da83c1a0f59eebaa481e12aa&auto=format&fit=crop&w=751&q=80") center no-repeat;
background-position: 40% 50%;
-webkit-background-clip: text;
color: transparent;
text-transform: uppercase;
letter-spacing: 0.1em;
line-height: 1.3em;
text-anchor: middle;
alignment-baseline: hanging;
font-size: calc(12px + 6.2vw);
text-align: center;
background-attachment: fixed;
}
.subtitle {
text-align: center;
letter-spacing: 0.35em;
font-size: 1.5vw;
color: #2b2b2b;
}
img {
height: 100%;
width: 100%;
}
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hero">
<div class="container">
<div class="header-container">
<br><br><br>
<br><br><br>
<div class="title">
我是特拉维斯。<br>
开发者。
</div>
<div class="subtitle">
<p>前端 - 后端 - 一切</p>
</div>
</div>
</div>
<div class="right-side-container">
</div>
</div>
<!-- 结束代码片段 -->
请注意,以上是您提供的代码的中文翻译。如果您需要更多帮助或有其他问题,请随时告诉我。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let mouseX, mouseY, trackX, trackY;
document.addEventListener("wheel", function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(".title").style.backgroundPosition = `${trackX}% ${trackY}%`
});
<!-- language: lang-css -->
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 200vmin;
font-size: 100%;
font-family: 'Anton', sans-serif;
}
body {
background: #1e5abc;
}
.hero {
background: linear-gradient(rgb(235, 236, 231), rgb(255, 255, 255));
height: 100vh;
width: 100%;
display: flex;
flex-wrap: wrap;
position: relative;
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 50% 100%, 0% 85%, 0% 0%);
}
.container {
flex: 1 1 50%;
display: flex;
justify-content: center;
align-items: center;
}
.right-side-container {
flex: 1 1 50%;
clip-path: url(#clipPath);
}
.title {
background: url("https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2b76b1e6da83c1a0f59eebaa481e12aa&auto=format&fit=crop&w=751&q=80") center no-repeat;
background-position: 40% 50%;
-webkit-background-clip: text;
color: transparent;
text-transform: uppercase;
letter-spacing: 0.1em;
line-height: 1.3em;
text-anchor: middle;
alignment-baseline: hanging;
font-size: calc(12px + 6.2vw);
text-align: center;
background-attachment: fixed;
}
.subtitle {
text-align: center;
letter-spacing: 0.35em;
font-size: 1.5vw;
color: #2b2b2b;
}
img {
height: 100%;
width: 100%;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hero">
<div class="container">
<div class="header-container">
<br><br><br>
<br><br><br>
<div class="title">
I'm Travis.<br>
Developer.
</div>
<div class="subtitle">
<p>Frontend - backend - everything</p>
</div>
</div>
</div>
<div class="right-side-container">
</div>
</div>
<!-- end snippet -->
Please let me know this is what you are looking for
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论