将图像设置为文本背景,并添加视差效果。

huangapple go评论69阅读模式
英文:

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(&quot;wheel&quot;, function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(&quot;.title&quot;).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: &#39;Anton&#39;, 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(&quot;https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;s=2b76b1e6da83c1a0f59eebaa481e12aa&amp;auto=format&amp;fit=crop&amp;w=751&amp;q=80&quot;) 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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;hero&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;header-container&quot;&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;div class=&quot;title&quot;&gt;
I&#39;m Travis.&lt;br&gt;
Developer.
&lt;/div&gt;
&lt;div class=&quot;subtitle&quot;&gt;
&lt;p&gt;Frontend - backend - everything&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;right-side-container&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- 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(&#39;scroll&#39;, function() {
let offset = window.pageYOffset;
document.querySelector(&#39;.title&#39;).style.backgroundPositionY = offset * 0.7 + &#39;px&#39;;
});

<!-- language: lang-css -->

.title {
background-image: url(&#39;https://cdn.firstcry.com/education/2022/04/25155522/1378635314.jpg&#39;);
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 -->

&lt;div class=&quot;title&quot;&gt;
&lt;div class=&quot;title-text&quot;&gt;
&lt;h1&gt;I&#39;m Travis&lt;/h1&gt;
&lt;h2&gt;
The developer
&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

put this line in css

.title{
background-attachment: fixed;
}

答案2

得分: 0

以下是代码部分的中文翻译:

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

    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}%`
    });

&lt;!-- 语言: lang-css --&gt;

    * {
        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&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;s=2b76b1e6da83c1a0f59eebaa481e12aa&amp;auto=format&amp;fit=crop&amp;w=751&amp;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%;
    }

&lt;!-- 语言: lang-html --&gt;

    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;div class="hero"&gt;
         &lt;div class="container"&gt;
            &lt;div class="header-container"&gt;
                &lt;br&gt;&lt;br&gt;&lt;br&gt;
                &lt;br&gt;&lt;br&gt;&lt;br&gt;
                &lt;div class="title"&gt;
                    我是特拉维斯。&lt;br&gt;
                    开发者。
                &lt;/div&gt;
                &lt;div class="subtitle"&gt;
                    &lt;p&gt;前端 - 后端 - 一切&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="right-side-container"&gt;
       
        &lt;/div&gt;
    &lt;/div&gt;

&lt;!-- 结束代码片段 --&gt;

请注意,以上是您提供的代码的中文翻译。如果您需要更多帮助或有其他问题,请随时告诉我。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let mouseX, mouseY, trackX, trackY;
document.addEventListener(&quot;wheel&quot;, function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
trackX = (4 * mouseX / 170) + 40;
trackY = (4 * mouseY / 170) + 50;
document.querySelector(&quot;.title&quot;).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: &#39;Anton&#39;, 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(&quot;https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-0.3.5&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;s=2b76b1e6da83c1a0f59eebaa481e12aa&amp;auto=format&amp;fit=crop&amp;w=751&amp;q=80&quot;) 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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;hero&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;header-container&quot;&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;div class=&quot;title&quot;&gt;
I&#39;m Travis.&lt;br&gt;
Developer.
&lt;/div&gt;
&lt;div class=&quot;subtitle&quot;&gt;
&lt;p&gt;Frontend - backend - everything&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;right-side-container&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Please let me know this is what you are looking for

huangapple
  • 本文由 发表于 2023年4月17日 14:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定