Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Image is not displaying when adding position property of css

I'm trying to unblur an image when scrolling. But when I add position: fixed; property to my CSS class the image is not getting displayed.Also the JS is giving an error saying 'scrollPercent not defined'. My inspiration to this code is :codepen

$(document).ready(function() {

  $(window).scroll(function() {

    var s = $(window).scrollTop(),
      d = $(document).height(),
      c = $(window).height();
    scrollPercent = (s / (d - c)).toFixed(2);

    // opacity value 0% to 100%
    $('.clear-img').css('opacity', scrollPercent);

  });

});
.imgsrc {
  position: fixed;
  background-position: center;
  webkit-background-size: cover;
  width: 100%;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: -1;
}

.clear-img {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="blurred-image-container">
  <div class="imgsrc">
    <img src="https://cdn.glitch.global/ea44df9f-5322-47c9-83ba-4f453113a115/blur.png?v=1651333241542" />
  </div>
  <div class="imgsrc clear-img">
    <img src="https://cdn.glitch.global/ea44df9f-5322-47c9-83ba-4f453113a115/unblur.png?v=1651333316753" />
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Did you include jQuery correctly?

It looks ok to me (I added a height: 1200px to body to allow for scrolling inside the snippet):

$(document).ready(function() {
  $(window).scroll(function() {
    const s = $(window).scrollTop(),
      d = $(document).height(),
      c = $(window).height();
    const scrollPercent = (s / (d - c)).toFixed(2);

    // opacity value 0% to 100%
    $('.clear-img').css('opacity', scrollPercent);
  });
});
body {
  height: 1200px;
  width: auto;
}

.imgsrc {
  position: fixed;
  background-position: center;
  webkit-background-size: cover;
  width: 100%;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: -1;
}

.imgsrc img {
  width: 100vw;
}

.clear-img {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="blurred-image-container">
  <div class="imgsrc">
    <img src="https://cdn.glitch.global/ea44df9f-5322-47c9-83ba-4f453113a115/blur.png?v=1651333241542" />
  </div>
  <div class="imgsrc clear-img">
    <img src="https://cdn.glitch.global/ea44df9f-5322-47c9-83ba-4f453113a115/unblur.png?v=1651333316753" />
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!