Can the custom scrollbar CSS property of a DOM node be located using JavaScript?

Is it possible to find a CSS property of a pseudo-class (like scrollbar pseudoclasses in Chrome) for an element styled with it? Here's an example:

<body><div #a></div></body>
<style>
  #a::-webkit-scrollbar {
    width: 10px
  }
</style>

I attempted to use

getComputedStyle(domNode,'::-webkit-scrollbar').width
, but the correct value is not being returned.

Answer №1

To find the size of the scroll bar, subtract the element's offset by its client size.

var myDiv = document.getElement...

var verticalScrollBarWidth = myDiv.offsetWidth - myDiv.clientWidth;

var horizontalScrollBarHeight = myDiv.offsetHeight - myDiv.clientHeight;

These are two properties:

Object.defineProperty(HTMLElement, "verticalScrollBarWidth", {
    get: function() {
        var tmpWidth = HTMLElement.offsetWidth - HTMLElement.clientWidth;

        if (tmpWidth > 0)
            return tmpWidth;

       return -1;
    },
    set: undefined
});

Object.defineProperty(HTMLElement, "horizontalScrollBarHeight", {
    get: function() {
        var tmpHeight = HTMLElement.offsetHeight - HTMLElement.clientHeight;

        if (tmpHeight > 0)
            return tmpHeight;

       return -1;
    },
    set: undefined
});

How to use it:

var myDiv = document.getElement...

var vScrollBarWidth = myDiv.verticalScrollBarWidth;
var hScrollBarWidth = myDiv.horizontalScrollBarHeight;

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

Ng-Repeat is generating empty list items

When I view this code in my browser, I see two list items, but there is no content displayed within them. The loop seems to be functioning correctly, but the items are not pulling any information from my list. I have revised my submission to accurately re ...

Using a JavaScript variable to be displayed in a PHP code

Can someone please help me troubleshoot this code? I am attempting to display a JavaScript variable in PHP after applying a regex, but I keep getting the error Uncaught TypeError: document.getElementById(...).html is not a function $.post('display.ph ...

What is the best method for generating a vertical tab using JavaScript in a paragraph format?

Struggling to create a menu similar to this using bootstrap 4. Despite my efforts, I keep encountering various issues. Is there someone who can help me solve this problem? Remember, I am using bootstrap 4. Here is a demo: <!doctype html> <html ...

Arrangement of box and buttons on R shiny interface

My dashboard page features action buttons aligned to the left, along with a plot and two additional zoom and reset buttons. I am looking to center the box on the screen and position the zoom and reset buttons at the top right corner. I attempted to use t ...

Tips for managing asynchronous code that invokes other asynchronous code in AngularJs

My factory utilizes indexedDB and a method called getRecipe that relies on this indexed db to fetch data. The issue arises because indexedDB returns its instance in an asynchronous call, and getRecipe is another method that also returns its value asynchro ...

Creating a blank div in bootstrap without any content inside and filling it with a background color

Is it possible to display 3 lines inline side by side in bootstrap? I've encountered a challenge with the height and width! While using pure css works fine, integrating bootstrap seems to be causing issues. Below is an example of the code using CSS f ...

Exploring the wonders of retrieving JSON data in PHP through an Ajax request

A front-end developer is sending an array of data formatted as a JSON object using an Ajax call. The JSON object structure is shown below: { "name": " Test Name ", "image_url": "test URL", "include": [ "1" ], "dimension": [ null ], "media_type" ...

Why is my IEDriverServer typing so slow? Seeking JavaScript solutions

While working with Selenium and IEDriverServer, I have encountered an issue where the keys are being sent to the input at a very slow pace. After conducting some research, many websites recommend ensuring that the Browser and IEDriverServer are of the sam ...

Obtain the Zero-width non-joiner character (‌) using the innerHTML attribute

I am attempting to retrieve a &zwnj; using the innerHTML method The desired output should be This section contains a zero-width‌&zwnj;non-joiner, a non-breaking&nbsp;space &amp; an ampersand However, the current output is: This part c ...

Unexpected outcomes experienced with AJAX in ASP.NET due to javascript integration

I experimented with two methods (server and client side) for initiating a JavaScript AJAX call to post a ticket on a website and then retrieve a link containing the ticket number for tracking or editing purposes. However, both approaches yielded different ...

The submit button on the HTML form is not functioning properly and requires activation

Currently, I am a student focusing on honing my skills by working on an app for a blog post. To kickstart my project, I downloaded the "clean-blog" template from the startbootstrap website. If you are interested, here is the link to the template: My curr ...

Using two body tags in the code of a Wordpress site can lead to malfunctioning

I've encountered a strange issue with my Wordpress sites hosted on BlueHost. There seems to be a double <head> tag appearing in the HTML code, even though there is only one. This duplicate tag is causing issues with the proper rendering of the s ...

Unable to retrieve data when utilizing SWR and Context API in Next.js

I'm currently working on fetching data from an API using SWR and dynamically setting the currency based on user preferences through context API. However, I'm facing issues as I am unable to view any data. Can someone please provide assistance or ...

Enhance your <head> section by adding lines when utilizing Layouts within Iron Router

Is there a way to add more lines to the <head> section using Iron Router and layouts? Take for example, inserting the following code snippet into the <head>... <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=ed ...

Why is the object not being initialized in a new API call while the string variable is successfully initialized?

There seems to be a basic issue that I am missing, as to why this is happening. GET: example.com/users //returns all data GET: example.com/users?status=1 //returns data with status = 1 GET: example.com/users // this does not work returns the same dat ...

Is there a way to retrieve the hand-drawn lines at no cost in the form of a list, with each line represented as a collection of coordinates

I am currently contemplating the idea of utilizing fabric.js for an online handwriting recognition system. In order to make this system work, I need to transmit the sketched lines as a collection of lines, where each line consists of several points. If a ...

Generate a dynamic file import loop within a React application

Suppose I have a directory containing several .md files: /static/blog/ example_title.md another_example.md three_examples.md and an array that includes all the titles: const blogEntries = ["example_title", "another_example", "three_examples"] In ...

Get the Label Values Based on CheckBox Status

Is there a way to retrieve the values of labels corresponding to checkboxes in HTML? I have multiple labels and checkboxes next to each other, and I want to be able to get the label values if the checkbox is checked. Can you provide guidance on how to do ...

Guide on transferring href parameter from the parent Vue component to the child component

Hey there! I've been working on creating a Vue page, breaking it down into components, and populating it with content from json. It all seems pretty straightforward, but I've hit a snag. Why is the href parameter not showing up in the right place ...

Navigating data attributes with jQuery

I'm encountering some difficulties with the following code, and I'd appreciate some guidance on the process, please. Below is the HTML snippet: <span data-credit-name="Name_1"><strong>1</strong> Name 1</span><br /> ...