Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:44:53+00:00 2026-06-05T13:44:53+00:00

I have several divs with id=scroll_1, scroll_2, scroll_3, etc… When any of these divs

  • 0

I have several divs with id=”scroll_1″, “scroll_2”, “scroll_3”, etc… When any of these divs is in the center of the window, I want to use jQuery highlight and/or change the background color of whichever div is in the center of the window. Currently the background color changes once it is in the center of the screen but I am having issues changing it back to original background color once it is no longer in the center (i.e the user has scrolled down/up to another scroll_x id.

Edit the only relevant css code I have is:

[id^=scroll_]{
background-color:white;
}

Thanks for the help!

<script>
$(document).ready(function() {      
    var window_height = $(window).height();
var obj_height = $('#scroll_1').height(); //height of object we are scrolling past
var top = $('#replyer').offset().top + (obj_height /2); //position on screen to start highlighting #scroll_x

    $(window).scroll(function() {
    var scrollMiddle = $(window).scrollTop() + ((window_height/2) - (obj_height /2));

    if (scrollMiddle >= top) {
        var scrollPosition = $(document).scrollTop()+ ((window_height/2) - (obj_height /2)),
                currentPosition = 0;
    $('div[id^="scroll_"]').each(function() {//iterate over #scroll_x and only change background until another #scroll_x is in the middle of the screen
            currentPosition = $(this).offset().top;
            if (currentPosition >= scrollPosition) {
            $(this).prev(function(){
                $(this).css('background-color',"#aaa"); //change previous #scroll_x back to original background color - Not Working Currently
            });

                return false; // break the loop
                }

                $(this).css('background-color',"#ccc"); //currently changes background of #scroll_x once in middle of screen but stays highlighted when scrolling up/down to previous/next iteration of #scroll_x
            });
        }
    });
});
</script>

Html:

<div id="replyer">
    Top line before repeating divs
</div>
<div id="scroll_1">
    First object to scroll over.
</div>
<div id="scroll_2">
    Want to highlight div currently in the middle of screen
</div>
<div id="scroll_3">
    Only div in middle of screen should be highlighted (background change)
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:44:55+00:00Added an answer on June 5, 2026 at 1:44 pm

    I’m not sure if it’s exactly what you’re after, but here is a demo that will change the object that overlaps the middle of the browser to green.

    Fiddle: http://jsfiddle.net/j2ULW/1/

    Full source:

    <html>
    <head>
      <title>Scroll test</title>
    </head>
    <style type="text/css">
      body {
        background: #000;
      }
      [id^=scroll_]{
        background-color:#aaa;
        height: 600px;
      }
      #replyer {
        height: 400px;
        background: white;
      }
    </style>
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <body>
      <div id="replyer">
        Top line before repeating divs
      </div>
      <div id="scroll_1">
          First object to scroll over.
      </div>
      <div id="scroll_2">
          Want to highlight div currently in the middle of screen
      </div>
      <div id="scroll_3">
          Only div in middle of screen should be highlighted (background change)
      </div>
    <script>
    $(document).ready(function() {      
      var window_height = $(window).height();
      $(window).scroll(function() {
        var scrollMiddle = $(window).scrollTop() + (window_height/2);
        $('div[id^="scroll_"]').each(function() {
          elTop = $(this).offset().top;
          elBtm = elTop + $(this).height();
          if (elTop < scrollMiddle && elBtm > scrollMiddle) {
            $(this).css('background-color',"#00ff00");
          } else {
            $(this).css('background-color',"#aaa");
          }
        });
      });
    });
    </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several divs that share a common class. If one of these divs
I basically have several divs each with ids id_1, id_2 id_3 etc. The divs
I have several DIVs that I want to place beside each other, but because
I have several divs that I want to work with dynamically. <div class=parent-19 id=123>
i have the following jquery code. basically i will have several overlapped divs and
I have several DIVs on a page. Inside these DIVs I have several form-elements
I want to have several divs with content side-by-side like D1 D2 D3 D4
I have several divs that I want to generate css background images for. They
I have several vertically stacks divs and I want to have a div appear
I have several divs with class image and several with class thumbnail (both can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.