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 937381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:09+00:00 2026-05-15T21:28:09+00:00

The Problem As the page is scrolled from top to bottom, the user will

  • 0

The Problem

As the page is scrolled from top to bottom, the user will pass through these ranges. Suppose they are values in an alphabetical address book: as they scroll from top to bottom, they will pass from A to Z. There are tabs on the side labeled with the names of sections in the document, labeled ‘Aa-An’, ‘Am-As’, etc.

What I want is, as the user scrolls from top to bottom, the scroll handler determines which of these sections is currently scrolled into and highlights the tab correlated with the current section.

In Javascript, on the document’s scroll event, I need to determine whether the document’s vertical scroll falls between one of a set of values. The values look like this:

var indices = {
  'Aa-An': [{id: 92, name: 'Aardvark'}, {id: 13, name: 'Affable'}, ...],
  'Am-As': [{id: 28, name: 'Amber'}, ...],
  ...
}

var heights = {
  'item_92': {top: 170, bottom: 380},
  'item_28': {top: 380, bottom: 600},
  ...
}

The keys in heights are the id attributes of the first element in each array in indices. top is the location on the page of the first element in that list, and bottom is the location on the page of the bottom edge of the last element in that list.

The Current Solution

My scroll handler is:

var scroll = get_vertical_scroll();

for(var key in heights) {
  var top = heights[key].top;
  var bottom = heights[key].bottom;

  if(scroll >= top && scroll < bottom) {
    select('.index_tab').removeClass('selected');
    get('index_'+key).addClass('selected');
    return;
  }
}

At most, there will only be 12 keys in heights to iterate through, so this is an effective linear solution. But, the scroll event is already pretty slow and if there’s a smart way to make this O(1) — even in only some cases — that would go a long way given that this happens on each scroll of the document.

  • 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-05-15T21:28:10+00:00Added an answer on May 15, 2026 at 9:28 pm

    Since your data is sorted, you could get to O(log(N)) with a slightly modified binary search that returns based on threshold instead of exact match. However, I doubt that searching those 12 items is what is slowing you down, since you’re not doing anything expensive like DOM lookups. You should use a javascript profiler like Firebug’s to see where the slowdown is actually occurring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
I have a problem where I cannot identify visitors to my intranet page because
I have a asp.net page I'm writing and I'm perplexed by this problem. I
I have found a problem with use JQuery timeout, i have a page with
OK, Here is my problem, I have a master page with a HEAD section
everybody; I have this problem in asp.net, I have a page where I insert
Problem: I have an address field from an Access database which has been converted
I have a strange problem with a WPF DataGrid from WPF Toolkit. The scrollbars
Can someone please advise a good approach to this problem: On page load, an
Is it just me, or is there a problem with page 68 of The

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.