JSFiddle - React, Tailwind, and code Playground

by Justin Breen

HTML

<div id="wrapper">
    <div id="container"></div>
    <p>Scroll Down</p>    
</div>
<div id="content">
</div>

CSS

#wrapper {
    background: #bada55;
    width: 100%;
    height: 400px;
}

#container {
    background-image: url("http://farm3.staticflickr.com/2533/4062253262_90a3635234_o.jpg");
    background-attachment: scroll;
    background-position: center;
    background-size: cover;
    width: 100%;
    height: 300px;
}

#content {
    height: 2000px;
}

p {
    line-height: 1.5;
    font-family: sans-serif;
    font-size: 3.875em;
    margin: 0 0 20px;
    text-align: center;
    color: #fff;
}

JavaScript

$(window).scroll(function() {
  var scrolledY = $(window).scrollTop();
  $('#container').css('background-position', 'center ' + (scrolledY * -1) + 'px');
});

// Jumping back to 0 on scroll instead of center center
// it's jumping to center 0 on scroll
// how to calculate so it's starts the Y position at center