The scrolling top value is dynamic when in developer mode

Looking to create a custom scrollbar for my web application.

The approach I'm taking involves shifting the default scrollbar 100px to the right and hiding it with overflow:hidden in the parent container

$(".scrollbar-vertical").parent().children(".content").scroll(function(){

    scrollerHeight = ( $(this).parent().height() / $(this).prop("scrollHeight") ) * $(this).parent().height();
    
    sTop = ( $(this).scrollTop()  / ( $(this).prop("scrollHeight") - $(this).outerHeight()  ) * ( $(this).height() - scrollerHeight) );
    console.log($(this).scrollTop());
    $(this).parent().children(".scrollbar").children(".scroller").css({
        margin:sTop+"px"+" 0 0 0"
    });
});
$(".scrollbar-vertical").parent().children(".content").each(function(){
    scrollerHeight = ( $(this).parent().height() / $(this).prop("scrollHeight") ) * $(this).parent().height();
    $(this).parent().children(".scrollbar").children(".scroller").css({
        height:scrollerHeight
    });
    pad = $(this).css("padding-bottom").match(/[-\d]+/g)[0]*1+$(this).css("padding-top").match(/[-\d]+/g)[0]*1;
    if($(this).prop("scrollHeight")  - pad <= $(this).height()){
        $(this).parent().children(".scrollbar").hide();
    }
});
.example{
    width:300px;
    height:100px;
    overflow:hidden;
    position:relative;
    background:#eee;
}
.content{
    width:100%;
    height:100%;
    overflow:scroll;
    padding:0 100px 100px 0;
    margin:0 100px 100px 0;
    float:left;
}
.scrollbar-vertical{
    width:5px;
    height:100%;
    position:absolute;
    float:left;
    right:0;
}

.scrollbar{
    transition:0.3s;
    background:transparent;
}
.scrollbar:hover{
    background:rgba(0,0,0,0.2) !important;
}

.scrollbar-vertical .scroller{
    width:100%;
    min-height:10px;
    background:#333 !important;
}


.scrollbar-vertical:hover{
    width:10px;
}

.scrollbar-horizontal{
    height:5px;
    width:100%;
    position:absolute;
    float:left;
    bottom:0;
}

.scrollbar-horizontal:hover{
    height:10px;
}

.scrollbar-horizontal .scroller{
    height:100%;
    min-width:10px;
    background:#333 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "example">
    <div class = "content">
        content..<br>
        content..<br>
        content..<br>
        content..<br>
        content..<br>
        content..<br>
    </div>
    <div class = "scrollbar scrollbar-vertical">
        <div class = "scroller"></div>
    </div>
</div>

It works well when overflow-y: scroll is applied to .content, but fails with overflow: scroll.

Even with overflow: scroll set, the scrollbar functions correctly in Chrome's developer mode. The issue seems to stem from $(this).scrollTop() increasing by 17 outside of developer mode. Any insights on why this might be happening are appreciated.

Check out the codepen here.

Answer №1

17px represents the size of a scrollbar in Chrome on desktop displays with a pointer type interface. When a scrollbar is present, this additional amount of pixels is included in the overall dimension of your container. The scrollbar becomes visible because you have set it to always be displayed using overflow:scroll.

In Chrome on desktops, the width of a vertical scrollbar is 17px, while the height of a horizontal scrollbar is also 17px.

If you notice that the scrollbars are hidden when in developer mode, it may be due to enabling the "Toggle device toolbar" option, which allows for previewing touch-based devices. In such cases, Chrome will display mobile/touch scrollbars above the content only when they are actively used. If there is no need for scrollbars (i.e., nothing to scroll), they will not appear even if you include overflow:scroll; in your styling.

To toggle the device toolbar while in developer mode, simply press Ctrl + Shift + M.

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

What is the Most Effective Method for Implementing a Unique Font-Family without Sacrificing Loading Speed

I have recently incorporated my own unique font to my website and utilized the code below to specify a custom font-family. Unfortunately, I've encountered an issue where the custom font initially displays as a standard web font while the page is loadi ...

Which Client-Side JavaScript Frameworks Pair Seamlessly With Node.js, Express.js, and socket.io.js?

Currently, I am in the process of developing a web application utilizing Node.js, Express.js, and socket.io.js on the server side. Are there any front-end frameworks (such as Agility, Angular, Backbone, Closure, Dojo, Ember, GWT, jQuery, Knockback, Knocko ...

How can I match all routes in Express except for '/'?

I've been working on implementing an authentication system for my app that involves checking cookies. My approach was to use router.all('*') to handle every request, verify the cookie, and then proceed to the actual handler. However, I encou ...

Issue: A problem has arisen with the element type, as it was supposed to be a string for built-in components but is currently undefined. This error is being encountered

Help needed: Error in my code! I am working with three files: HeaderOption.js, HeaderOptions.js, Search.js This is the content of HeaderOptions.js import HeaderOption from "./HeaderOption"; import DotsVerticalIcon from "@heroicons/react/o ...

Leveraging useContext to alter the state of a React component

import { createContext, useState } from "react"; import React from "react"; import axios from "axios"; import { useContext } from "react"; import { useState } from "react"; import PermIdentityOutlinedIcon f ...

Sequelize querying using the `WHERE NOT EXISTS()` condition

I am currently working with a many-to-many relationship setup in my database: var Genres = db.define('Movie', { name: { type: Sequelize.STRING(100), allowNull: false }, description: { type:Sequelize.STRING(), ...

What could be the reason for jQuery AJAX sending requests repeatedly?

I have a message board where users can comment on messages similar to Facebook. When you click the "comment" link, a textarea slides down for you to write your comment. However, if you click "comment," then cancel so the textarea slides back up, and then c ...

Why won't my div tag show conditionally with AngularJS ng-show?

I'm having trouble displaying a div tag on a form based on the boolean flag specified in ng-show. Unfortunately, the div is not showing up at all. Here's what I've tried so far without success. Any assistance would be greatly appreciated! F ...

Is it possible to return an empty array within an HttpInterceptor when encountering an error?

When encountering errors that require displaying empty "lists" in dropdowns, I utilize this interceptor: public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request).pipe(catchEr ...

Generate a graphical representation with react-d3

In an attempt to create a histogram using react-d3-components, I have the following code: import React, { Component } from 'react'; import * as d3 from 'd3'; import * as ReactD3 from 'react-d3-components'; import propTypes fr ...

Tips for using the identical function on matched elements

I am working on a code where I want each textbox input to change its corresponding image. The function is the same for all partners in the list (txt & img). I have looked around and found some similar posts, but I am struggling to make the function wor ...

Retrieve an array from the updated scope

I need help with my code. How can I retrieve the names from an array and display them in my input box? Also, how do I get all the changed names back into the array? Thanks in advance! - Marco app.js var g[]; var names = ['John', 'Steve&apo ...

Executing functions across modules in node.js

I need assistance with two of my modules: var client = require('./handlers/client.js'); var server = require('./handlers/server.js'); server.createClient() The client module: var client = function(){ console.log("New client"); ...

Align Text Center inside a Magical H1 Heading Tag

I'm having a bit of trouble with something that should be simple. I want to center the text inside my h1 tag on a wizard, and I've added this CSS to my stylesheet.css: .h1textalign { text-align:center; } Here's how I'm trying to apply ...

When using async functions in iterative processes

In my current setup, I am utilizing a for-each loop to handle a list and specifically require element n to be processed only after element n-1 has completed: let elements = ["item1", "item2", "item3"]; elements.forEach(function(element){ someAsyncFun ...

utilizing Nuxt code in Elixir/Phoenix

Overview In my previous work, I combined frontend development with nuxt and backend support from elixir/phoenix, along with nginx for reverse proxy. Looking to enhance the performance of the system, my goal is now to migrate everything to Elixir/Phoenix. ...

Learn the process of implementing an SVG icon in your Angular Material project

I'm currently attempting to incorporate a single icon into my Angular Material application. Following the documentation, I have implemented $mdIconProvider in the following manner: app.config(function($stateProvider, $urlRouterProvider, $mdIconProvid ...

What is the process for selecting the default option in a drop-down menu?

Is there a way to set the default value in a drop-down list using material-ui? I attempted to use displayEmpty="true", but it did not work. I want the first option, A, to be pre-selected so that it is visible to the user in the UI without them having to m ...

Ways to extract transparency data of each pixel from a PNG uploaded by the user via the front-end

Currently, I am in need of extracting the transparency value for each individual pixel from a PNG image that is submitted by the user, directly within the front-end environment. At the moment, I have implemented a method where I prevent the form from bein ...

Creating a custom HTML5 pattern for formatting Pakistani phone numbers

For my HTML5 pattern for Pakistan Phone Number Format, I have the following criteria: 03xx-xxxxxxx My current regex code is as follows: /03[0-9]{2}-[0-9]{7}/ Now, I want to ensure that the next 7 digits after the hyphen meet the following conditions: ...