Tips for stopping automatic scrolling (universal solution)

Issue or Inquiry

I am seeking a way to disable actual scrolling on an element while still utilizing a scrollbar for convenience (avoiding the need for manual JavaScript implementations instead of relying on browser functions).

If anyone has an improved solution or alternative approach, your assistance would be greatly appreciated.

Update 13-03-2017

I have identified a solution that appears to be effective (tested across IE11, Edge, Chrome, Firefox, Chrome for Android, and Firefox for Android).

Therefore, the remaining content of this query regarding the problem can be disregarded.

Update 12-03-2017

I found a method to toggle (prevent) scrolling in IE 11, Edge, Firefox, Chrome, and Firefox for Android. However, it seems to perform poorly (significant lag) specifically in Chrome for Android. Any insights into this issue?

This outlines my solution as of 12-03-2017, but Chrome for Android does not respond well to it (!). Assistance in refining this solution (or suggesting an alternate method) would be valuable.

  • A single div containing scrollable content with position: fixed (#scrollsviascrolldistract in the provided code).
  • Another div with the same height as the scrollable content div, positioned absolutely (or relatively based on preference). This corresponds to #scrolldistract in the embedded code snippet.
  • Utilize window.requestAnimationFrame with a callback function adjusting the marginTop value of the scrollable content div (#scrollsviascrolldistract) relative to the current scrollTop window value (utilizing negative margin for this purpose).
  • To retain control and prevent scrolling, refrain from updating the marginTop value, thereby maintaining the position of the scrollable content div unaffected by the scrollbar position.

The below code snippet has been tested on IE 11, Edge, Firefox, and Chrome (exhibiting satisfactory performance), while encountering issues specifically in Chrome for Android (unsatisfactory). Additionally, testing was conducted on Firefox for Android (resulting in favorable outcomes). This method outperforms using two div elements with position: fixed.

var scrollDisabled = false;

function toggleScroll() {
  scrollDisabled = !scrollDisabled;
}

function doScroll() {
  var st;
  if (!scrollDisabled) {
    $("#scrollsviascrolldistract").css("marginTop", -$(window).scrollTop());
  }
  window.requestAnimationFrame(doScroll);
}

window.requestAnimationFrame(doScroll);

$("#scrolltoggler").on('click', toggleScroll);
body {
  background-color: whitesmoke;
}

#scrollsviascrolldistract {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
}

#scrolldistract {
  position: absolute;
  left: 0;
  top: 0;
  height: 6000px;
}

.red,
.blue {
  position: relative;
  width: 100%;
  height: 300px;
}

.red {
  background-color: red;
}

.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scrolldistract">
  <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" height="6000">
</div>

<div id="scrollsviascrolldistract">
  <div class="red">BEGIN</div>
  <div class="blue"><button id="scrolltoggler">Toggle Scroll On/Off</button></div>... 

Prior Solution (please disregard 12-03-2017; refer to updated solution)

My previous strategy involved enabling scrolling within an invisible element, supplemented by a spacer image to achieve the desired height.

However, various cross-browser discrepancies emerged.

Prior Solution - Successful Aspects and Limitations

Chrome

  • Mouse and scrollbar scrolling functionality are operational.
  • Keyboard-based scrolling via focus() immediate success.

Chrome for Android

  • Touch-based scrolling functionalities as intended.

Firefox

  • Ctrl+PageUp/Ctrl+PageDown causing tab switch disables scrolling until interacting with scrollbar.
  • Key-based scrolling functional, yet requires clicking in background for focus.

Firefox for Android

  • Significant complications arise, ceasing scrolling abruptly after short periods without clear cause.

Edge

  • Functional besides the limitation on focus(). Require additional clicks in document area (red/blue blocks or background) for operation.

Internet Explorer 11

  • Exclusively supports mouse-based scrolling. Inability to attain focus manually hinders keyboard-driven scrolling.

Code and Resources

Demo available at: https://jsfiddle.net/hn63z0jt/

HTML5 markup sample with correct DOCTYPE:

<div id="scrollsviascrolldistract">
  <div class="red">BEGIN</div>
  <div class="blue">&nbsp;</div>... 

CSS styling:

body {
  background-color: whitesmoke;
}

#scrollsviascrolldistract {
  position: fixed;
  left: 0px;
  top: 0px;
}

#scrolldistract {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    overflow-y: scroll;
    background: rgba(0,0,0,0);
  z-index: 999;
}

.red, .blue {
  position: relative;
  width: 900px;
  height: 300px;
}

.red {
  background-color: red;
}

.blue {
  background-color: blue;
}

JavaScript implementation (incorporating jQuery):

$(function() {
    var $window = $(window);
    var $scrollsviascrolldistract = $('#scrollsviascrolldistract');
    var $scrolldistract = $('#scrolldistract');

    $scrolldistract.focus();

    $scrolldistract.on('scroll', function() {
      var scrollTop = $scrolldistract.scrollTop();
      $scrollsviascrolldistract.css('marginTop', -scrollTop);
    });
});

Answer №1

I have figured out a solution to my own query.

Here is the method I found to toggle (prevent) scrolling:

  • Use one div for the scrolling content with position: fixed applied (#scrollsviascrolldistract in my code).
  • Add another div with the same height as the scrolling content div, positioned absolutely (or relatively, if preferred). This element is named #scrolldistract in the provided code snippet.
  • Invoke window.requestAnimationFrame with a callback function that adjusts the marginTop value of a puller div (referred to as
    #puller</code here), which should be the first child of the scrolling content <code>div
    (#scrollsviascrolldistract). The adjustment is based on the current scrollTop value of the window, using negative margins for this purpose. Initially, attempting to adjust the scrolling content directly (modifying the marginTop of #scrollsviascrolldistract) did not give desired results in some browsers.
  • To retain control and prevent scrolling, refrain from updating the marginTop value, ensuring that the scrolling content remains unaffected by the scrollbar's position.

The below code snippet has been tested in IE 11, Edge, Firefox, Chrome, Chrome for Android, and Firefox for Android. It appears to perform better compared to utilizing two div elements with position: fixed.

... Code snippet continues here ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on arranging the JSON array within an AngularJS controller

Can someone assist me with sorting the JSON list provided below in the controller and then displaying it in the view? The orderBy filter only sorts one level, but I need it to sort recursively for all children. Input: R2 -->S4 ------>T5 ------> ...

Controllers within controllers that fail to choose an option in a select tag dropdown will result in the hiding of another input element

My application utilizes nested controllers following John Papa's style guide, with a controller as approach. One of the controllers manages the form for client validation and submission, while the other is responsible for handling location-related fun ...

The functionality of core-ui-select is not functioning properly following the adjustment of the

I've implemented the jquery plugin "core-ui-select" to enhance the appearance of my form select element. Initially, it was functioning perfectly with this URL: However, after applying htaccess to rewrite the URL, the styling no longer works: I&apos ...

Angular parent scope does not reflect changes when a directive updates the shared scope variable

My directive is designed to validate and modify a specific binded value before triggering the button action. However, I am facing an issue where any updates made to the directive value are not being reflected in the parent scope. Even though I have used (= ...

Troubleshooting a Pop-up Error and JSON Bug in an MVC Application

Within a JQuery Popup, I am utilizing multiple TextBox controls: <li id="lblAmountPerTurbine"> <label for="AmountPerTurbine"><strong>Amount Per Turbine:</strong></label> <%= Html.TextBox("Amo ...

Show or hide specific elements based on parameters using ng-show in Angular

One challenge I am facing is how to manage a menu with multiple buttons that open submenus without using a massive switch statement in my code. Instead, I want to try something different: In the HTML file, I call the function toggleVis and pass the nam ...

Unable to generate a fresh directory with mongoose and express

As the title suggests, I am working on an application that generates a link using mongoose _id and express's app.get when certain information is inputted. However, I am facing an issue where I have to reload the entire server in order to access the di ...

Modify the color of an Ionic button for a single button, rather than changing the color for all buttons

How can I modify this code so that only the clicked button changes its color, not all of them? Here is the current code: .html: <ion-col size="6" *ngFor="let data of dataArray" > <ion-card> <ion-card-header> ...

eliminating list item from unordered list depending on the input of either first or last name

My objective is to refine a lengthy list as the user types in a search for a specific person by first or last name. The current code I have functions adequately, but encounters an issue when there is a space in the input field. My goal is to allow users to ...

Tips for arranging images of varying heights on separate lines so they appear cohesive as a group

I've been working with masonry.js but I'm still not achieving the effect I want. Javascript:` var container = document.querySelector('#container'); var msnry = new Masonry( container, {columnWidth: 200, itemSelector: '.item' ...

Display a progress bar in Bootstrap with a labeled indicator positioned above the bar and background to the

Is there a way to create a progress bar with a label positioned to the right using Bootstrap v5.2 Progress Bar? https://i.sstatic.net/gOWie.png I attempted to use negative margins to achieve this layout but faced issues when the label expanded due to lon ...

Quantifying the passage of time for data entry

Looking to create a quiz form where I can track how long it takes users to input/select answers. I attempted the following code but the timing seems off: $('input, select').on('focus', function(event) { el = $(this); name ...

Retrieve the URL action and return it as a JSON object within the MVC framework

After my controller method returns a JSON object with a URL formed as shown below: return Json(new { url = Url.Action("ShowContact", "Customer", new { vm = contactVM }) }, JsonRequestBehavior.AllowGet); I am encountering an issue in the success code of m ...

What would be the ideal labels for the parameters within Array.reduce?

When it comes to enhancing code readability, what naming convention should be employed when naming callback arguments in Array.reduce for optimal best practices? const studentAges= [15,16,14,15,14,20] Generalized Approach const sum = studentAges.reduce ...

What are the steps to setting up SystemJS with Auth0?

I am having trouble configuring SystemJS for Auth0 (angular2-jwt) and Angular 2.0.0-beta.6 as I keep encountering the following error message: GET http://localhost:3000/angular2/http 404 (Not Found)fetchTextFromURL @ system.src.js:1068(anonymous function) ...

repeated firing of keydown event in ReactJS

Having an issue with adding an event listener and checking if it's level 1. When I press the space key once, it seems to fire more than 50 times. Any assistance would be greatly appreciated. document.addEventListener("keyup", function(e) { if(l ...

Incorporating jquery-ui Datepicker with dynamic text input functionality

In my table list, I have multiple input fields with the jQuery datepicker implemented. Each input field is assigned the class "datepicker" and a unique ID number. When clicked on, the datepicker pops up allowing for date selection to be inserted into the f ...

Is it possible to load jQuery dialog from the Controller/View?

I have implemented a jQuery dialog feature to upload files in my application. When the file is selected and submitted, a controller is invoked to process the file and send it to the model for insertion. The model then communicates back to the controller wi ...

Roughly one out of every 500 users tend to send GET requests instead of POST

We have implemented the following AjaxSetup configurations: $.ajaxSetup( { cache: false, type: 'POST', contentType: "application/json", data: "{}", headers: { "Accept": "application/json; charset=utf-8" }, beforeSend: fun ...

Using ESLint to enforce snake_case naming conventions within TypeScript Type properties

When working with TypeScript, I prefer to use snake_case for properties within my Interfaces or Types. To enforce this rule, I have configured the ESLint rule camelcase as follows: 'camelcase': ["error", {properties: "never"}], Even though the E ...