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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:49:21+00:00 2026-05-28T18:49:21+00:00

in jQuery I always encounter alot of double work. I mean, I write the

  • 0

in jQuery I always encounter alot of double work. I mean, I write the same thing for different divs.

I have the following code:

var about = $('#about').offset().top - parseFloat($('#about').css('margin-top').replace(/auto/, 0));
var education = $('#education').offset().top - parseFloat($('#education').css('margin-top').replace(/auto/, 0));
var work = $('#work').offset().top - parseFloat($('#work').css('margin-top').replace(/auto/, 0));

and

$(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y >= about) {
        // if so, ad the fixed class
        $('#nav li').removeClass('current');
        $('.ab_about').addClass('current');
    } else {
        // otherwise remove it
        $('.ab_about').removeClass('current');
    }

    // whether that's below the form
    if (y >= education) {
        // if so, ad the fixed class
        $('#nav li').removeClass('current');
        $('.ab_education').addClass('current');
    } else {
        // otherwise remove it
        $('.ab_education').removeClass('current');
    }

    // whether that's below the form
    if (y >= work) {
        // if so, ad the fixed class
        $('#nav li').removeClass('current');
        $('.ab_work').addClass('current');
    } else {
        // otherwise remove it
        $('.ab_work').removeClass('current');
    }

});

You can say that if I have 20 of those items, it’s gonna be a very long script 😛
Any idea on how to automate things and make it smaller. I tried something with the .each method, but that was a dead end for me.

Ty

  • 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-28T18:49:22+00:00Added an answer on May 28, 2026 at 6:49 pm

    u can just create a simple plugin and assign that plugin to those divs like:

    $.fn.myplugin = function(options){
       var $this = $(this);
       var $navli = $(options.navli);
       var $abClass = $(options.abClass);
       var offset = $this.offset().top - parseFloat($this.css('margin-top').replace(/auto/, 0));
       $(window).scroll(function (event) {
            // what the y position of the scroll is
            var y = $(this).scrollTop();
    
            // whether that's below the form
            if (y >= offset) {
                // if so, ad the fixed class
                $navli.removeClass('current');
                $abClass.addClass('current');
            } else {
                // otherwise remove it
                $abClass.removeClass('current');
            }
        }
    }
    
    $(document).ready(function(){
        $('#about').myplugin({'navli': '#nav li', 'abClass': '.ab_about'});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always used the mouseover event, but while reading the jQuery documentation I
I'm cleaning up some Jquery code. I always read, chaining is good for performance.
I use IE7.js but it doesn't have CSS3 support. I use jQuery always in
I've used some jQuery UI sortable plugins but I always found the same problem,
I'd like some help with the following jQuery code if possible... $(document).ready(function() { $('table.sortable').each(function()
To make sure that $ is always referencing jQuery , I code in this
I have a JQuery slider which works fine in all browsers except IE(always a
I have this jQuery code: <script type=text/javascript > $(function() { $('#add_comment').bind('submit',function() //$(input[type=submit]).click(function() { var
Iam a great fan of javascript frameworks especially jQuery .I have always wanted to
Here is a jQuery issue that I come across and always have to resolve

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.