Javascript Custom Scrollbar Issue

      var start_mouse_y = 0;
  var scroll_offset = 0;
  function SET_SCROLL_EVENT(e){
    document.getElementById("online_list_scroll").draggable = true;
    start_mouse_y = e.clientY;
  }
  function ADJUST_SCROLL_EVENT(e){
    dont_pass = (412 - set_scroll_height);
    mouse_y = e.clientY;
    scroll_top = parseInt(document.getElementById("online_list_scroll").style.top);
    scroll_offset = (mouse_y - scroll_top) + 46;
    new_top = scroll_top + (mouse_y - start_mouse_y);
    document.getElementById("DEBUG").innerHTML = "my: "+mouse_y+"<br>new_top: "+new_top+"<br>scroll_offset: "+scroll_offset+"<br>scroll_top: "+scroll_top;
    if(new_top <= 0){
      document.getElementById("online_list_scroll").style.top = 0+"px";
    }else if(new_top >= dont_pass){
      document.getElementById("online_list_scroll").style.top = dont_pass+"px";
    }else{
      document.getElementById("online_list_scroll").style.top = new_top+"px";
    }
    scroll_bottom = set_scroll_height + new_top;
    scroll_percent = scroll_bottom / 412;
    online_show = (document.getElementById("online_list").scrollHeight - 412) * scroll_percent;
    online_show = Math.round(online_show);
    document.getElementById("online_list").scrollTop = online_show;
  }
  var SCROLL_FLAG = 0;
  document.onmouseup = function(){ SCROLL_FLAG = 0; };
  document.onmousemove = function(event){ if(SCROLL_FLAG == 1){ADJUST_SCROLL_EVENT(event);} };

Javascript code snippet above

        <div style="float: left; width: 13px; position: relative; height: 412px;">
      <div id="online_list_scroll" style="width: 5px; position: absolute; top: 0px; left: 4px; border-radius: 4px; background-color: #3f3f3f; height: 15px;" 
           onmousedown="if(SCROLL_FLAG == 0){ SET_SCROLL_EVENT(event); SCROLL_FLAG = 1; }"></div>
    </div>

HTML content above

The scrolling behavior of the scrollbar is erratic and fast, jerking back and forth. It does work but not smoothly as expected. Any help or insight would be greatly appreciated as I have been struggling with this all night.

Thank you in advance for any assistance provided.

Answer №1

If you are facing an issue with local variables in your code, the following adjustments should help resolve it. These changes are specific to this particular problem and aim to fix the code. Below is the modified code with comments highlighting where the common mistake lies:

// First ensure that you have defined the necessary variables using 'var'.
        var start_mouse_y = 0;
        var mouse_y = 0;
        var scroll_offset = 0;

        function SET_SCROLL(e) {
            document.getElementById("online_list_scroll").draggable = true;
            start_mouse_y = e.clientY;
// Initialize 'mouse_y' with 'start_mouse_y'.
            mouse_y = start_mouse_y;
        }

        function ADJUST_SCROLL(e) {
            var set_scroll_height = 0;
            var dont_pass = (412 - set_scroll_height);

// Update 'start_mouse_y' to be equal to 'mouse_y', ensuring it tracks previous position correctly.

            start_mouse_y = mouse_y;
            mouse_y = e.clientY;
            var scroll_top = parseInt(document.getElementById("online_list_scroll").style.top);
            scroll_offset = (scroll_top - mouse_y) + 46;

            var new_top = scroll_top + (mouse_y - start_mouse_y);

            // Additional code logic and error handling here
        }

        var SCROLL_ON = 0;
        document.onmouseup = function() {
            SCROLL_ON = 0;
        };

        document.onmousemove = function(event) {
            if (SCROLL_ON == 1) {
                ADJUST_SCROLL(event);
            }
        };

Thank you for your attention,

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

When row highlights overshadow column outlines in an HTML table with CSS styling

I implemented a modified version of ThinkingStiff's solution to a question related to columns, colgroups, and CSS :hover psuedoclass. The result is a table that clearly outlines columns and highlights rows when hovered over. (Note: Vertical column hea ...

Styles are ineffective on the focus property, although they do work before the focus is applied

I'm having trouble changing the font color of the TextInput in material UI. It seems to change to white when I click away, but then reverts back to a purple-ish color (the default) when I focus on it again. I'm not sure what I'm missing here ...

Where should custom PHP code be placed in Prestashop 1.7.5?

Currently, I am facing the challenge of incorporating a custom PHP code snippet into Prestashop's file structure so that it is accessible on all pages such as the cart, categories, and CMS pages. After some research, I have found suggestions to place ...

Vanishing Act: React-js MUI Tooltip vanishes upon clicking

The standard behavior of the MUI Tooltip is as follows:
 If the button/icon to trigger a tooltip is not in focus, the tooltip will not disappear when clicking directly on the popper. However, if the button/icon is focused, the tooltip will disappear upo ...

Tips for concealing an entire menu on WP, with the exception of items labeled as current-menu-ancestor

On my Wordpress site, I am faced with a simple problem to solve. I have a primary menu at the top and a secondary menu on the left side. While the top menu only contains level 1 items, the left menu has all the items. To customize the left menu, I am utili ...

The blur effect isn't functional on the Firefox internet browser

When using the blur filter in my React application, everything works fine in Google Chrome and Microsoft Edge. However, I encountered an issue when testing it in Mozilla Firefox. Despite checking mozilla.org for solutions, I couldn't find anything spe ...

Adjusting Card Size both Vertically and Horizontally in Bootstrap

After struggling to align my bootstrap cards for what feels like forever, I've decided to seek help by creating this post. Hopefully someone can assist me! Initially, the cards were all the same width and stacked correctly, but the height was not con ...

Error encountered while building Dart WebUI

I am encountering issues when attempting to integrate web_ui into my existing Dart application. I have not yet incorporated any of the webui-specific code into my HTML file, I am simply trying to build. I updated the pubspec.yaml file with the web_ui pac ...

What is the recommended approach for conducting backend validation?

As I develop a CRUD application using express.js and node.js, here is the backend API code that I have written: app.post("/signup", (req, res) => { const { name, email, password } = req.body; if (!name) { res.status(401).json("Please provide a n ...

creating a div that functions similarly to a radio button

Is it possible to create a div that mimics the behavior of a radio button? I'm looking for a solution that doesn't involve jquery, as many people have recommended. After doing some research, I found a few potential options. In the past, I'v ...

Creating a Node.js Express application paired with a React.js frontend

In my application, I'm utilizing reactjs as the front-end technology and plan to use nodejs for server-side programming. After setting up the reactjs application, which generated project structure and package.json, I proceeded to install express, wit ...

Using Flutter to navigate to a URL based on the image that was clicked

Currently, I am creating a user interface where clicking on an image will open a specific URL. Additionally, I have implemented a search list feature where clicking on a topic will also open the respective URL associated with it. Should I use `url_launcher ...

Searching for a way to smoothly scroll horizontally from right to left using the mouse wheel?

I am looking to display 3 out of my collection of over 6 articles, with the ability for the user to scroll from right to left when using the mouse wheel. Additionally, I want to hide the scrollbar for a cleaner look. Can anyone assist me with this? I hav ...

AngularJS is receiving an array of null inputs

I have a form where data from my DB is displayed through WebApi. One of the controls will contain an array of values. There is a scenario where the user can edit and save it using the PUT function. Controller : $scope.Put= function () { $scope.i ...

How many requests per second can a Selenium client-sided load test handle?

I am currently using Selenium for client-side testing on an AngularJS web application with a specific browser. My objective is to execute a load test by generating numerous requests from concurrent users. For instance, sending 1k requests per second from x ...

Prevent certain images from loading by blocking them

I am trying to create an extension that blocks two specific images from loading. Initially, I attempted to achieve this by using the following code in the content.js file: $("#rated-image").remove(); //id of one image $(".blur-mask").remove(); //class of ...

Verify that all images retrieved through AJAX have finished loading before proceeding

Is there a way to ensure all images in the HTML loaded via AJAX are fully loaded before performing an action like displaying them? I've experimented with various methods, including using the 'imagesloaded' plugin. var page = 'P40&apos ...

Utilizing Ajax in Rails 5: Trigger form submission upon clicking either of the two radio buttons via Ajax

Note: I have encountered similar questions before, but none of them match my specific scenario. Currently, I am creating a small Ajax form within my rails views using yes or no radio buttons. When I implement this form with remote: true, it functions corr ...

Is there a way to refresh a Thymeleaf table without reloading the entire page?

Displayed below is both my HTML and JavaScript code, utilizing Thymeleaf to dynamically render table content. The button's functionality changes based on the notified value - if it's 1, the button is disabled with a modified CSS style. When the v ...

Using a personalized function in JQuery

I need to execute a JavaScript function I created after a JQuery event has been triggered. The function in question is called scrambleDot, and I defined it previously like so:var scrambleDot = new function() { //my code }. Here's the attempted implem ...