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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:55:54+00:00 2026-05-27T01:55:54+00:00

I am trying to use this script to freeze scrolling when an ‘lightbox-style’ image

  • 0

I am trying to use this script to freeze scrolling when an ‘lightbox-style’ image is opened. There’s probably just a small error in the script but can’t figure it out

jQuery.fn.lockToggle = function () { 

    var top = $(window).scrollTop();
    var left = $(window).scrollLeft();

    var clickCount = 0;

    $(this).click(function() {

        clickCount++;

        if(clickCount % 2 === 1  ){
            $('body').css('overflow', 'hidden');

            $(window).scroll(function(){
                $(this).scrollTop(top).scrollLeft(left);
            });

        } else {
           $('body').css('overflow', 'auto');
           $(window).unbind('scroll'); 
        }
    });

    return this;
};    

How this script works (in display order):

  1. Grabs the current scroll position and stores it in two variables
  2. Creates a variable for the number of clicks the object has
  3. If the click count is odd then it freezes the page in its current position
  4. Else (is even) then it allows the page to be freely scrolled

All of this works in a simple jQuery function that can be used just by calling .lockToggle()

You can view this in a full example here:

http://jsfiddle.net/JamesKyle/8H7hR/30/

(Click on the image)

  • 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-27T01:55:55+00:00Added an answer on May 27, 2026 at 1:55 am

    You’re binding a click event each time you click. This binds more and more events each time, and does not execute the locking immediately. This is not what you seem to want.

    From my understanding, the error you’re pointing at is the fact that the scrolling does not lock when clicking, and does not unlock when clicking again. You can implement this correctly with a simple isLocked variable as follows: http://jsfiddle.net/8H7hR/32/.

    var isLocked = false;
    
    jQuery.fn.lockToggle = function () {    
        var top = $(window).scrollTop();
        var left = $(window).scrollLeft();
    
        if(isLocked) { // locked so unlock
    
           isLocked = false;
           $('body').css('overflow', 'auto');
           $(window).unbind('scroll');
    
        } else { // unlocked so lock
    
            isLocked = true;
            $('body').css('overflow', 'hidden');
            $(window).scroll(function(){
                $(this).scrollTop(top).scrollLeft(left);
            });
    
        }
    
        return this;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use this jQuery script to make an image caption from
I am trying to use this in my page class. I only just started
I'm trying to use this script (which extracts the domain from a URL): CREATE
why does this script show empty alert box. I am trying to use php
Trying to use this following script to load a page so that I can
Im trying to use this code: <script> jQuery('#MySelectorDiv').fadeTo(500,0.2); </script> To fade out a bunch
Im trying to use this jQuery script and this is confusing me: function CallService()
I'm trying to use this little script I found here , but for some
I'm trying to use this script from Github at https://github.com/blueimp/jQuery-File-Upload The script has js
I'm trying to use this php script in order to locate a stock quote

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.