skip to Main Content

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

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

2

Answers


  1. window.addEventListener('scroll', function() {
      let offset = window.pageYOffset;
      document.querySelector('.title').style.backgroundPositionY = offset * 0.7 + 'px';
    });
    .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;
    }
    <div class="title">
      <div class="title-text">
        <h1>I'm Travis</h1>
        <h2>
        The developer
        </h2>
      </div>
    </div>

    put this line in css

    .title{
    background-attachment: fixed;
    }
    
    Login or Signup to reply.
  2. 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}%`
    });
    * {
        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%;
    }
    <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>

    Please let me know this is what you are looking for

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search