The tooltip for the jQuery slider-bar is lagging behind the slider handle

In customizing a jQuery slider bar from a tutorial by Thoriq Firdaus for use in a Django Python application, I found that the original size and range of the slider were too small for my needs. I needed to set the range to min: -100 and max: +100. Modifying the JS and CSS for the slider to fit these requirements was no problem. However, after resizing the slider, the tooltip no longer aligned or moved along with the slider handle.

Upon inspecting the element using Firebug, I noticed that the tooltip seemed to be stuck within a range of -100px and +100px from the left of the slider. This caused it to lose synchronization with the slider handle when I moved it (see images below).

Slider tooltip positioned at nearly maximum left:

Slider tooltip positioned at the far right:

I attempted various fixes, such as setting a min: and max: range in the JS for the tooltip (which did not work) and adjusting the CSS properties to center and align the tooltip correctly, but to no avail.

Does anyone have insight into how I can make the tooltip move along with the slider handle? Is this change required in the JS code or the CSS styling?

I also tried replicating the issue on a JSFiddle, but encountered difficulties with the appearance of the slider handle.

My HTML Code:

<section>
    <span class="tooltip"></span>
    <div id="slider"></div>
</section>

My CSS Styling:

#slider{
    border-width: 1px;
    border-style: solid;
    border-color: #333 #333 #777 #333;
    border-radius: 25px;
    width: 700px;
    position: absolute;
    height: 13px;
    background-color: #8e8d8d;
    background: #85837A;
    box-shadow: inset 0 1px 5px 0px rgba(0, 0, 0, .5),
                0 1px 0 0px rgba(250, 250, 250, .5);
    left: 20px;
}

.tooltip {
    position: absolute;
    bottom: 10px;
    display: block;
    z-index: 1031;
    width: 35px;
    height: 20px;
    color: #E456ff;
    bgcolor: #E456ff;
    text-align: center;
    font: 10pt Tahoma, Arial, sans-serif ;
    border-radius: 3px;
    border: 1px solid #333;
    box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, .3);
    box-sizing: border-box;
    background: linear-gradient(top, rgba(69,72,77,0.5) 0%,rgba(0,0,0,0.5) 100%);
    opacity: 1;
    filter: alpha(opacity=1);
}

My JavaScript Functionality:

$(function() {

    //Store frequently used elements in variables
    var slider  = $('#slider'),
        tooltip = $('.tooltip');

    //Hide the tooltip initially
    tooltip.hide();

    //Initialize the Slider
    slider.slider({
        min: -100,
        max: +100,
        value: 0,

        start: function(event,ui) {
            tooltip.fadeIn('fast');
        },

        slide: function(event, ui) { //When sliding the slider

            var value  = slider.slider('value'),
                volume = $('.volume');

            tooltip.css('left', value).text(ui.value);  //Adjust the tooltip position
        },

        stop: function(event,ui) {
            tooltip.fadeOut('slow');
        },
    });

});

Answer №1

Check out this updated link!

Take a look now, I believe it's all set.

Here are the recent modifications:

Initializing values based on width %

NOTE: Just in case the JSFiddle link goes missing

The code below represents the JSFiddle solution shared by nsthethunderbolt, which I have copied here.

HTML

    <section> <span class="tooltip"></span> 
        <div id="slider"></div>
        <div id="slider"></div>
    </section>

CSS

section {
    width: 700px;
    height: auto;
    margin: 0px auto 0;
    position: relative;
}
#slider {
    border-width: 1px;
    border-style: solid;
    border-color: #333 #333 #777 #333;
    border-radius: 25px;
    width: 400px;
    position: absolute;
    height: 13px;
    background-color: #8e8d8d;
    background: #85837A;
    box-shadow: inset 0 1px 5px 0px rgba(0, 0, 0, .5), 0 1px 0 0px rgba(250, 250, 250, .5);
    left: 20px;
}


.tooltip {
    border: 1px solid #333;
    border-radius: 3px;
    top:20px;
    position:absolute;
    box-shadow: 1px 1px 2px 0 rgba(0, 0, 0, 0.3);

    color: #e456ff;
    display: block;
    font: 10pt Tahoma, Arial, sans-serif;
    height: 20px;
    opacity: 1;

    text-align: center;
    width: 35px;
    z-index: 1031;
}

Javascript

$(function () {
    var initX=0,minX=50,width=400;
    
    var slider = $('#slider'),
        tooltip = $('.tooltip');

   
    slider.slider({
       
        min: -50,
        max: +50,
        value: 0,

        start: function (event, ui) {
            tooltip.fadeIn('fast');
        },

        
        slide: function (event, ui) { 
            var value = slider.slider('value'),
                volume = $('.volume');
            tooltip.css('left', initX+(value*width)/100).text(ui.value); 
        },

        stop: function (event, ui) {

        },
    });
    initX=slider.slider("value");
    var txt=initX;
   initX+=(minX*width)/100;
tooltip.css('left',initX).text(txt);
});

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

How to designate a default selection value using local array data source in Select2.js version 4.0?

If I am using the select2.js v4 plugin with a local array data source, how can I set the default selected value? For instance, consider the following code: var data_names = [{ id: 0, text: "Henri", }, { id: 1, text: "John", }, { id: 2, text: ...

Show the selected value in the dropdown in the order that I choose

I am trying to implement a multi-select drop down feature where the selected options will be displayed in a textbox in the order they were selected. The code I have works fine, but it currently displays the values in random order instead of the order in wh ...

XSLT - Dividing table into three columns while maintaining continuity on the same page

I'm looking for a solution where I can display a long two column table in a three-column page layout. The table should seamlessly flow from one column to the next, maintaining its headers throughout. Current attempts show the entire table in just one ...

The Navbar design in Bootstrap is not scaling properly with the background image, making

I am currently in the process of developing a Bootstrap 5 webpage based on a provided design, but I am encountering some challenges. Here is the design I am working from: https://i.sstatic.net/MsFzq.jpg Here is my current progress: https://i.sstatic.ne ...

Revamping the Bootstrap admin template

Is there a way to customize this Bootstrap 3 admin template? https://getbootstrap.com/docs/3.3/examples/dashboard/ I want to make some changes like removing the top fixed navbar and shifting everything up by 50px (as defined in dashboard.css). However, I ...

Tips for properly filling empty spaces in a flexbox layout using HTML

Is there a way to fill in the gap using CSS so that boxes automatically take up the empty spaces, even if they are of different sizes? How can I make sure the boxes adjust automatically as the screen size changes? .c1 { display: flex; flex-wrap: w ...

Is the menu not appearing in the header section of the HTML/CSS?

Hey there, I'm a new student diving into the world of HTML/CSS. I've been working on creating a page layout, but for some reason my menu links are appearing under the header section instead of within it. I'm a bit confused and could really u ...

The brief interruption in ASP.NET SignalR communication results in the user being temporarily disconnected, and quickly reconnected

Is it possible to maintain a connection when the user navigates to another subpage? I am using @Url.Action("Action","Controller") I have a JQuery click function attached to Url.Action which triggers a signalR hub server method. However, I am facing an is ...

Timing issue with the animation callback

I have been experimenting with creating a bounce effect on my custom scroller by utilizing the translate3d method for scrolling. I managed to successfully implement it, but now I am facing an issue where if you continuously scroll out of bounds (try double ...

Setting the ajax mock calls count to zero in Jest test suites

In my current testing setup, I have a test structure that involves using ajax calls and mocking them based on the Jest tutorial found here: describe('it behavior', function() { it('is as it is', function() { jQuery.ajax.mock.call ...

Place an IconButton component next to each Material UI TableRow

I am trying to include an icon next to the material UI table row component. Similar to the hint icon shown in the screenshot below Here is my attempt so far, but it's not functioning as expected: Check out the code on CodeSandbox https://i.stack.i ...

Utilize flexbox to align two buttons side by side and manage their positioning

I have a collection of buttons that I want to display in rows of two. I am looking for a way to set the specific width of each button and decide whether they should be aligned left, center, or right. Ideally, the outcome would resemble the following: http ...

What is the best way to ensure that my ajax key/values are properly identified within the "params" object in a controller?

Attempting to send data using JQuery ajax to a grails controller Here is the data: var data = {'status':"SOMETHING", 'scheduleDate':remindDate.toString("MMMM dd yyyy h:mm:ss tt"), ...

An undefined error for the variable 'y' in a JavaScript AJAX scenario

I'm completely new to JavaScript and struggling with writing a code that dynamically counts the words on a webpage. Currently, this piece of code is enclosed within a 'whenkeydown' function: var text = $(this).val(); var word=text.split(" ...

CSS: using syntax to target element by its unique identifier

Check out this guide: I followed the instructions and used an external stylesheet. I added #menu before each CSS item, for example: #menu ul{ font-family: Arial, Verdana; font-size: 14px; margin: 0; padding: 0; list-style: none;} or: #menu ul li{ displ ...

Styling elements with pseudo-classes when a horizontal scroll bar is activated

I have been experimenting with CSS pseudo-classes, specifically the before and after classes. While I have had no issues with the before class, I am running into a horizontal scroll problem when using the after class. I want to avoid using overflow: hidde ...

What are the steps to resize a YouTube embedded video?

I am currently attempting to use a YouTube video as the background for breadcrumbs on my website. However, I am facing an issue with setting the video width to cover the full screen. The image below shows that the video is displayed in the center of the if ...

How can the parent div's height be determined by the child div when using position: absolute?

I am struggling with setting the height of a parent div that contains a nested child div with absolute positioning. The child div has a fixed height of 652px, but I cannot seem to get the parent div to match this height. I have tried adjusting the clear an ...

Functionality limited to Firefox browser

I'm having some trouble with my website development in HTML5 and CSS3. Specifically, I am struggling to get my processing sketch to work on browsers other than Firefox. Below is the code I am working with: <!DOCTYPE html> <html> ...

A way to verify a datalist field using jQuery validator within a PHP environment

Greetings, I currently work as a web application developer and I am facing an issue with jQuery validation. I have a form where I need to display a list of employees using the `datalist` tag. The client should be able to enter a correct name or select an e ...