Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Change background colour on scroll

Participant ,
Nov 12, 2016 Nov 12, 2016

Copy link to clipboard

Copied

Hi All,

I currently have the below code that changes the background colour when scrolling. There a a few problems with the code below:

1. the colour only changes on scroll down and does not change back on scroll up.

2. I would like to set a more accurate point for when the colour changes. i.e. I would like to set it to change once a certain anchor comes into the viewport or to change at 200 vh for example. Any help would be greatly appreciated.

Many Thanks,

Alex

#test{

  height: 200vh;

  width: 100%;

  display: block;

  background-color: rgba(255,255,255,1);

}

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

<!--for RGBA support. http://pioupioum.fr/sandbox/jquery-color/ -->

<script type="text/javascript" src="https://raw.github.com/piouPiouM/jquery-color/master/jquery.color.js"></script>

<!--the magic -->

<script type="text/javascript">

$(document).ready(function(){

    $(document).scroll(function(){

        if($(this).scrollTop() > 200)

            $('#test').animate({backgroundColor: 'rgba(0,0,0,.9)'}, 1000);

    });

});

</script>

Views

900
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 12, 2016 Nov 12, 2016

Copy link to clipboard

Copied

If you post a working example of this online, you might get more help.

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 12, 2016 Nov 12, 2016

Copy link to clipboard

Copied

Hi Nancy,

This is a working example that I have found online, see if this helps: Not Studio - London Design Agency

Cheers

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 12, 2016 Nov 12, 2016

Copy link to clipboard

Copied

LATEST

See if either of these Code Pens help you.

https://codepen.io/Funsella/pen/yLfAG

http://codepen.io/Funsella/pen/dpRPYZ

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines