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

  • SEARCH
  • Home
  • 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 8194831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:56:54+00:00 2026-06-07T04:56:54+00:00

SOLUTION $(window).scroll(function() { var y = $(this).scrollTop(); for (var i = 0; i <

  • 0

SOLUTION

$(window).scroll(function() {
            var y = $(this).scrollTop();
            for (var i = 0; i < offset.length; i++)
                {
                    if (y < offset[i].bottom) {
                        $("#catSelect option:selected").removeAttr("selected");
                        $("#catSelect option[value=#"+offset[i].id+"]").attr("selected", "selected");
                        break;
                    }
                }
        });

Thank you for your help guys!

PROBLEM

I am a beginner at JavaScript and jQuery, so I hope some kind soul can help me out.
Below is a snippet of a code that executes just fine… but as it is pretty repetitive, I am trying to come up with some code to help me ‘automate’ all these conditions within the switch statement instead of manually specifying it (if that makes sense). I’m going with a ‘for’ loop but no results as of yet.

A nudge in the right direction, please? Thank you 🙂

~ preliminary code ~

var offset = [];
        $(".cat").each(function() {
            var top =  $(this).offset().top;
            var bottom = top + $(this).outerHeight();
            var id = $(this).attr("id");
            offset.push({"top": top, "bottom": bottom, "id": id });
            $(document.body).append(offset.length);
        });

~ the code it’s all about ~

$(window).scroll(function() {
    var y = $(this).scrollTop();

        switch (true) {
            case (y < offset[0].bottom):
                $("#catSelect option:selected").removeAttr("selected");
                $("#catSelect option[value=#"+offset[0].id+"]").attr("selected", "selected");
                break;
            case (y < offset[1].bottom):
                $("#catSelect option:selected").removeAttr("selected");
                $("#catSelect option[value=#"+offset[1].id+"]").attr("selected", "selected");
                break;
            case (y < offset[2].bottom):
                $("#catSelect option:selected").removeAttr("selected");
                $("#catSelect option[value=#"+offset[2].id+"]").attr("selected", "selected");
                break;
            }

        });
  • 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-07T04:56:57+00:00Added an answer on June 7, 2026 at 4:56 am

    As it’s only the array index that differs in the different cases:

    for (var i = 0; i < offset.length; i++) {
      if (y < offset[i].bottom) {
        $("#catSelect option:selected").removeAttr("selected");
        $("#catSelect option[value=#"+offset[i].id+"]").attr("selected", "selected");
        break;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<script type=text/javascript> function pageLoad() { var $scrollingDiv = $(#scrollfix); $(window).scroll(function () { if ($(window).scrollTop()
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide
I implemented infinite scroll like so: new_page_value = 1; $(window).scroll(function() { if($(window).scrollTop() >= $(document).height()
The App crashs when I add this line `requestWindowFeature(Window.FEATURE_NO_TITLE); may be the solution is
$(window).scroll(function() { hash = $('.layer:in-viewport').attr('id'); if ( currentHash != hash ) { // fires
I've made this little snippet to scroll the window to the top of the
When using wx.TextCtl with the wx.TE_RICH2 option in windows, I get this strange bug
There has to be a easy solution to this. I have been trying to
Try this showcase: <!doctype html> <html> <head> <style> html { min-height: 100%; overflow-y: scroll;
I've got a solution for keeping a sidebar in the viewport as you scroll

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.