3

I wanted to smoothly transition and change background color while scrolling, preferably using just CSS, and came across the following as a good example: https://minimill.co/

How can I achieve the smooth transition in changing the background color? And also when a button is clicked on the navigation bar, navigate to that particular section of the page? I attempted to check the source code but wasn't any help. The whole source code is in 1 line.

Thank you and will be sure to vote up and accept answer.

Dan Me
  • 2,143
  • 4
  • 19
  • 19
  • 1
    A good example on how to change the background color while scrolling is on this link: [https://codepen.io/Funsella/pen/yLfAG](https://codepen.io/Funsella/pen/yLfAG), or the second version of this in this link [http://codepen.io/Funsella/pen/dpRPYZ](http://codepen.io/Funsella/pen/dpRPYZ) – Thanasis1101 Feb 09 '17 at 20:44
  • @Thanasis Wow thank you so much but it is using a framework/plugin? And would there be an alternative to achieving it without using CSS? – Dan Me Feb 09 '17 at 20:48
  • Do you mean without using javascript? Because on the question you say "preferably using just CSS". – Thanasis1101 Feb 09 '17 at 20:52
  • The first link uses the scrollie jQuery Plugin. The second link uses the in-view plugin. And they both use jQuery. In general you can see which plugins are used by clicking the little settings button left from the title JS. – Thanasis1101 Feb 09 '17 at 21:02

2 Answers2

3

WITHOUT EXTRA PLUGINS

If you want to use only JavaScript then you can go about this solution.

In the code below I have 3 divs and each one has the attribute data-color which contains the color that we want to have on the background when the user is over that div. I made it so the color changes not just when the div is on top of the page but when we are after the 2/3 of the previus div.

When the user scrolls, the function below document.onscroll = function() { is called. This function loops through all the divs (credits: https://stackoverflow.com/a/11291363/7053344) and if (if (scrollTop > curDiv.offsetTop - heightBefore){) the scroll top is bigger than the top of a div (curDiv.offsetTop) minus the 1/3 of the hight of the previous div (heightBefore), then the background is changed according to the div's data-color attribute. The smooth transition for the change of the background color is achieved by this line: transition: background 1.5s; on the CSS.

Also included below are the jumps that you wanted. From the first div there is a link that sends you to the second div etc. You can modify them to fit your navigation bar. In order to understand jumps better you can look here.

JSFiddle: https://jsfiddle.net/0pe5n97z/2/

var test = document.getElementById("test");

document.onscroll = function() {

    scrollTop = document.documentElement.scrollTop;
    test.innerHTML = scrollTop;
    
    allDivs = document.getElementsByTagName('div');

    for( i=0; i< allDivs.length; i++ )
    {
      curDiv = allDivs[i];
        
        
        // The code below makes the background color change when the
        // scroll top passes the 2/3 of the previous div.
        
        heightBefore = 0;    
        if (i > 0){
          heightBefore = allDivs[i-1].offsetHeight / 3;
        }
        
        if (scrollTop > curDiv.offsetTop - heightBefore){
          color = curDiv.getAttribute("data-color");
           document.body.style.background = color;
        }
                
    }
};
body {
    background: green;
    transition: background  1.5s;
}
<body>
<div style="position:fixed" id="test"></div>

<center>
<div id="div1" data-color="green">
    <p>Title goes Here</p>
    <a name="green">
        <p>Green area</p>
        Go To <a href="#red" style="color:red">Red area</a>
    </a>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

<div id="div2" data-color="red">
    <a name="red">
        <p>Red area</p>
        Go To <a href="#blue" style="color:blue">Blue area</a>
    </a>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

<div id="div3" data-color="blue">
    <a name="blue">
        <p>Blue area</p>
        Return To <a href="#green" style="color:green">Green area</a>
    </a>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

</center>

</body>

UPDATE

In order to make it work on different browsers too, you must add these lines in the CSS:

-webkit-transition: background 1.5s;
-moz-transition: background 1.5s;
-ms-transition: background 1.5s;
-o-transition: background 1.5s;
transition: background 1.5s;

and then change the scrollTop initialization in javascript from this:

scrollTop = document.documentElement.scrollTop;

to this:

scrollTop = window.pageYOffset;

You can test it in this updated JSFiddle.

Sources for this update:


WITH EXTRA PLUGINS

As for your question:

smoothly transition and change background color while scrolling

as I wrote in the comment these sources are very helpful:

The examples in these links use javascript, jquery and other plugins, which I think are neccesary.

As for your question:

when a button is clicked on the navigation bar, navigate to that particular section of the page

this link explains it very well:

Below there is a small example of what you want, that was created by using and combining content from the links above:

$( window ).ready(function() {
  
    var wHeight = $(window).height();

    $('.slide')
      .height(wHeight)
      .scrollie({
        scrollOffset : -50,
        scrollingInView : function(elem) {
                   
          var bgColor = elem.data('background');
          
          $('body').css('background-color', bgColor);
          
        }
      });

  });
* { box-sizing: border-box }

body {
  font-family: 'Coming Soon', cursive;
  transition: background 1s ease;
   background: #3498db;
}

p {
  color: #ecf0f1;
  font-size: 2em;
  text-align: center;
  
}

a {
  text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2542/jquery.scrollie.min_1.js"></script>

<div class="main-wrapper">
  
  <div class="slide slide-one" data-background="#3498db">
    <p>Title</p>
    <center>Go To <a href="#green" style="color:green">Green</a>.</center>   
  </div>
  
  <div class="slide slide-two" data-background="#27ae60">
      <a name="green">
        <p>Green area</p>
        <center>Go To <a href="#red" style="color:red">Red</a>.</center>
      </a>
  </div>
  
  <div class="slide slide-three" data-background="#e74c3c">
      <a name="red">
        <p>Red area</p>
        <center>Page over. Hope that was helpful :)</center>
      </a>
  </div>
  
  
</div>

Other approaches:

Community
  • 1
  • 1
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • Thank you so much for responding. Just a few things to clear up before I accept the answer and vote up. Could you explain where javascript, query, or plugins are used? And how they are operating? And what do `transition: background 1s ease;` and `data-background="#3498db"`, and `* { box-sizing: border-box }` do? – Dan Me Feb 09 '17 at 22:38
  • In general javascript is used for changing content dynamically. In this example the jquery and the jquery-scrollie (libraries of javascript) are used in order to make it more simple to make the color change, in other words to use this line of code: `$('.slide').height(wHeight).scrollie({...` which includes some options setting (`scrollOffset : -50` which means every how many pixels the color will change, the `scrollingInView : function` in which we say what we want to do -in our case change background color -, etc). – Thanasis1101 Feb 09 '17 at 23:25
  • The `transition: background 1s ease;` command means that when the background is changed from one color to another, it will not happen immediately but will change smoothly after 1 second (more info [here](http://www.w3schools.com/cssref/css3_pr_transition-timing-function.asp)). The `data-background="#3498db"` command sets the color for each div. In this example there are 3 divs, the blue the green and the red. So for every div it sets what the color should be. The `* { box-sizing: border-box }` command is not very neccesary for this but you can look [here](https://css-tricks.com/box-sizing/). – Thanasis1101 Feb 09 '17 at 23:37
  • Appreciate your response. What is `'.slide'`? Did you arbitrarily define it? And what made you include `* { box-sizing: border-box }`? And `*` means apply to everything, correct? – Dan Me Feb 10 '17 at 17:39
  • And I'm using ReactJS. Where should I include the jQuery? Looked around and can't see to find the answer that pertains to this particular jQuery. – Dan Me Feb 10 '17 at 17:44
  • .slide reffers to the html elements which have the `class="slide"` and changes those. `* { box-sizing: border-box }` was just in the examples given in links. The last question: correct. About ReactJS, sorry I know nothing. You can google it. – Thanasis1101 Feb 10 '17 at 17:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135406/discussion-between-dan-me-and-thanasis). – Dan Me Feb 10 '17 at 17:45
0

Try this css:

 -webkit-transition: background-color 1000ms linear; 

see my fiddle i did quickly: https://jsfiddle.net/kreza/4jfy1rhg/2/

Reza
  • 65
  • 7