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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:35:50+00:00 2026-06-03T23:35:50+00:00

I’m putting together a jQuery plugin. The plugin takes panels, and auto-sizes their height.

  • 0

I’m putting together a jQuery plugin. The plugin takes panels, and auto-sizes their height. So I start off with something like this:

<div class="panel">Test 1</div>
<div class="panel">Test 2</div>
<div class="panel">Test 3</div>

The code for that looks something like:

    sizePanels: function(){
      panels.each(function(){
        $(this).height(docHeight);
      });
    },

There is a down button, that when clicked, will take the user to the next $(“.panel):

nextPanel: function(){
  $.scrollTo($(".panel:eq(" + panelIndex + ")"), 250, { easing: "swing" });
}

With that, I’m keeping track of the panel index that their on:

    if (panelIndex < (panelCount - 1) ) {
      panelIndex += 1;
    }

I’m trying to figure out a way to track if they happen to scroll manually, and pass one of the elements, to then increase the “panelIndex”, so that the button doesn’t move them up instead of down because it was never incremented properly due to the user using the scroll bar instead of the button. This is what I have so far:

   $(window).scroll(function(){
    panels.each(function(index){
      if ($(window).scrollTop() > $(this).scrollTop()) { 
        panelIndex = index; 
        // console.log(index);
      }
    });

    if (panelIndex < panelCount - 1){
      s.showDownButton();
    }
  });

The code excessively checks and feels somewhat overboard. is there a better way to do it?

  • 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-03T23:35:52+00:00Added an answer on June 3, 2026 at 11:35 pm

    An easy optimization is to only calculate the scrollTop once and to exit the each loop when a match is found. You can exit a $.each loop by returning false.

    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        panels.each(function(index){
            if (scrollTop > $(this).scrollTop()) { 
                panelIndex = index; 
            } else {
                return false;
            }
        });
    
        if (panelIndex < panelCount - 1){
            s.showDownButton();
        }
    });
    

    The next way that I would suggest optimizing this is to pre-calculate the scrollTop of each panel on page load (and when the viewport is resized). If you store these values in an array, then you can loop through them very quickly.

    Here is some rough code to illustrate the idea:

    var panelTops = [];
    
    findPanelTops(); // calculate on load
    $(window).on("resize", findPanelTops); // calculate on resize
    
    function findPanelTops() {
        panelTops = [];
        panels.each(function(index) {
            panelTops.push($(this).scrollTop());
        });
    }
    
    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        for (var i = 0; i < panelTops.length; i++) {
            if (scrollTop > panelTops[i]) { 
                panelIndex = i;
            } else {
                break;
            }
        };
    
        if (panelIndex < panelCount - 1){
            s.showDownButton();
        }
    });
    

    The scroll event can fire a lot and very quickly, so you want to keep the amount of computation as minimal as possible. One way to get around all of this is to implement a scrollend handler. This will only fire when the scroll event has appeared to have stopped.

    Here is some basic code for doing that. It will fire when the scroll event has stopped for more than 500ms:

    var scrollTimeout = null;
    
    function onScroll() {
        if (scrollTimeout) {
            clearTimeout(scrollTimeout);
        }
        scrollTimeout = setTimeout(onScrollEnd, 500);
    }
    
    function onScrollEnd() {
        // Scrolling has stopped
        // Do stuff ...
    
       scrollTimeout = null;
    }
    
    $(window).on("scroll", onScroll);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.