Jump to content

Background change color on scroll - help!

Go to solution Solved by creedon,

Recommended Posts

Site URL: https://codepen.io/tobiasthaden/pen/OVWKjm

Hello there everyone!  I was curious if anyone can help me with inserting this bout of code into Squarespace to change my background.  I am curious if this is even possible, considering that from what I understand to change backgrounds we can only use CSS code?  Am I wrong in that?  Anywho, I'm looking to achieve the same effect as this groovy example .  Any insight would be MUCH appreciated. 

 

xo

Link to comment

I don't know for sure but I think a similar effect could be achieved in a Squarespace site.

It would require Javascript and CSS. You would need the business plan or above to execute the Javascript. For a v7.1 site I could see a page with multiple sections. Each with a background color defined by some Javascript at initialization and then the window scroll would change the background colors.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 3 weeks later...

Yes. On v7.0 with an Index page more easily than other types of pages.

Set up an Index page with five pages in it.

1293819610_ScreenShot2021-02-03at12_48_37PM.png.3455998975368a957f4f83f15c181efe.png

Add the following to Settings > Advanced > Code Injection > HEADER. 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Add the following to Index Settings > Advanced > Page Header Code Injection.

<style>

  body {
  
    -webkit-transition : background-color 0.5s linear;
    -o-transition : background-color 0.5s linear;
    transition : background-color 0.5s linear;
    
    }
    
  .Index-page {
  
    background-color : transparent;
    /* height : 100vh; */ /* can be used when testing with empty sections */
    
    }
    
  </style>

<script>

  $( ( ) => {
  
    /*
    
      change color background on scroll
      
      SS Version : 7.0
      Template : Brine
      
      */
      
    const sectionColors = [ // add a color for each page within the Index page
    
      'white',
      
      'red',
      
      'green',
      
      'blue',
      
      'white' // last or only item doesn't get a comma
      
      ];
      
    // do not change anything below, there be the borg here
    
    $( window ).scroll ( function ( ) {
    
      $( '.Index-page' ).each ( function ( i ) {
      
        if ( ! ( $( this ).position ( ).top <= $( window ).scrollTop ( ) ) )
        
          return true;
          
        $( 'body' ).attr ( 'style', 'background-color :' + sectionColors [ i ] + ' !important' );
        
        } );
        
      } );
      
    } );
    
  </script>

This is for a v7.0 site using the Brine template family.

You can change the number of pages and colors to suit your needs. The names of the Index and other pages can be whatever you need.

Let us know how it goes.

Edited by creedon
version 2

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment

Please post the URL for the Index page.

If your site is not public please set up a site-wide password, if you've not already done so. Post the password here.

We can then take a look at your issue.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment

I've updated my code post.

3 hours ago, oldegray said:

I've also been practicing coding with the floating ball overlay, if that's irritating I can surely turn them off 

 That code looks somewhat familiar! 🙂

17 hours ago, oldegray said:

to only apply this block of code to one specific index page instead of the code calling all Index Pages in the website?

The code when installed per my instructions should only effect the Index pages in which it is installed.

While I was diagnosing what was going on I noticed a few other things you may want to take care of.

The following code appears to be a previous attempt at a background color change. I suggest removing it as it isn't doing anything functional.

<script>
$(window).scroll(function() {
  
  // selectors
  var $window = $(window),
      $body = $('body'),
      $panel = $('.panel');
  
  // Change 33% earlier than scroll position so colour is there when you arrive.
  var scroll = $window.scrollTop() + ($window.height() / 3);
 
  $panel.each(function () {
    var $this = $(this);
    
    // if position is within range of this panel.
    // So position of (position of top of div <= scroll position) && (position of bottom of div > scroll position).
    // Remember we set the scroll to 33% earlier in scroll var.
    if ($this.position().top <= scroll && $this.position().top + $this.height() > scroll) {
          
      // Remove all classes on body with color-
      $body.removeClass(function (index, css) {
        return (css.match (/(^|\s)color-\S+/g) || []).join(' ');
      });
       
      // Add class of currently active div
      $body.addClass('color-' + $(this).data('color'));
    }
  });    
  
}).scroll();
</script>

The following code is CCS wrapped in a script tag. That won't work. Either the tag should be changed to style or the script tag removed and the CSS moved to Design > Custom CSS.

<script>
[data-section-id="philosophy"] .section-background {
    background-image: linear-gradient(100deg,#3874ba,#edccc9) !important;
    background-size: 400% !important;
    text-fill-color: transparent;
    -webkit-text-fill-color: transparent;
    -webkit-animation: Gradient 10s ease infinite !important;
    -moz-animation: Gradient 10s ease infinite !important;
    animation: Gradient 10s ease infinite !important;
}
      </script>

Let us know how it goes.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 2 months later...

@yasminaezz

Please post the URL for your site.

If your site is not public please set up a site-wide password, if you've not already done so.

Post the password here.

Adding a site-wide password is not a security breach. Please read the documentation at the link provided to understand how it works.

We can then take a look at your issue.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 3 weeks later...

@Olivia95

It may be possible to adapt this code to work with a page and page sections for v7.1.

Please post the URL for the page on your site where you want this effect.

If your site is not public please set up a site-wide password, if you've not already done so.

Post the password here.

Adding a site-wide password is not a security breach. Please read the documentation at the link provided to understand how it works.

We can then take a look at your issue.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 5 months later...
  • 1 year later...
9 hours ago, rophillips77 said:

I don't see the color changes ... is there something wrong?

Yes. SS updated how backgrounds are handled and I need to update my code.

It may take me several days to update. If you don't hear back from me soon, please feel free to bump this thread.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 1 year later...

Hi , Is this still possible to add to a Squarespace 7.1? in the github project do I need to seperate the html to javascript and css , I want to add a background colour change to 3 sections on my website is that possible using your code? Sorry I am still learning and want to learn coding 

Edited by Oliokd123
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.