Adjust the alignment of points in JQuery UI slider values

Currently, I am utilizing jQuery UI slider with 6 points along with 6 divs positioned above the slider. My goal is to align the points in the middle of each div located above the slider.

Is there a method available to specify the position of values in jQuery UI slider?

For reference, here is an example of what I am working on: jsfiddle

$('#slider').slider({
    value:1,
    min: 1,
    max: 6,
    step: 1,
    slide: function(event, ui) {

    }
});


<div id="assesment-filter">
    <div class="assesments-wrapper">
        <div id="1" class="assesment">1</div>
        <div id="2" class="assesment">2</div>
        <div id="3" class="assesment">3</div>
        <div id="4" class="assesment">4</div>
        <div id="5" class="assesment">5</div>
        <div id="6" class="assesment">6</div>
    </div>
<div id="slider"></div>
</div>


.assesments-wrapper {
    height: 33px;
}

.assesment {
    width: 33px;
    height: 33px;
    background-color: rgb(219,219,219);
    line-height: 33px;
    float: left;
    text-align: center;
    vertical-align: middle;
    color: white;
    font-size: 18px;
    border-right: 1px solid white;
    border-bottom: 1px solid white;
}

.assesment:hover {
        cursor: pointer;
    }

.ui-slider-handle{
  display: block;
  position: relative;
  top: -6px;
  width: 15px;
  height: 15px;
  background-color: #2CBFD9;
  border-radius: 50%;
  border: 1px solid white;
}

.ui-slider-handle:hover {
  cursor: pointer;
}

.ui-slider{
  background-color: rgb(219,219,219);
  height: 3px;
  width: 203px;
  margin-bottom: 16px;
}

Answer №1

After some careful calculations, I have solved the problem for you.

The width of the anchor in blue color is calculated as

15px + (1px + 1px)(Border) = 17px;

Upon inspecting the ui-slider, it starts at left:0% and increments based on the max, min, and step values.

In this specific case where max=6, min=1, step=1, the increment in percentage is calculated as 100*step/(max-min) which results in 20%.

Each Assessment has a width of 33px+1px=34px, leading to the wrapper div having a size of approximately 34*6=204px.

To center the slider anchor, it needs to start at left:8.5px with a relative position to the wrapper since the anchor's width is 17px and assessment's width is 34px.

As the anchor is placed at the center of the first div initially, half of the task is completed.

At the last step where left:100%, the anchor extends beyond the slider, requiring the ui-slider width to be calculated as width of wrapper - 2*(width of anchor).

If the last step was at left:80%, then the calculation for ui-slider width would be modified accordingly.

Ultimately, the ui-slider width is determined as 204-2*17 = 170px;.

Aligning the first and last steps ensures everything falls into place inside the slider.

You can find the solution in this fiddle: Fiddle For Slider.

Here is the essential code for reference:

.assesments-wrapper {
        height: 33px;
        width: fit-content;
    }

.ui-slider{
  position: relative;
  background-color: rgb(219,219,219);
  height: 3px;
  width: 171px;
  left:8.5px;
  margin-bottom: 16px;
}

I acknowledge that there may be better solutions out there, but I attempted to solve this challenge without resorting to Google search.

Despite my unconventional approach, I will share this on SO :).

Answer №2

To make modifications to the jquery ui code, you will need to extend it accordingly.

Here is a sample basic HTML structure:

<section class="container">  
  <div class="slider3></div>
  <br><br>
</section>

I recommend checking out this pen for reference: http://codepen.io/exampleuser/pen/AbCdEf

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

The presence of a display block property within a div contained inside an li element results in extra whitespace

I am presenting the code below: <div class="settingsMenu" style="top: 135px; left: 149px; display: block;"> <ul> <li class="download" onclick="downTemplate('template1')">Download</li> <l ...

What methods can be used to keep divs from breaking onto separate lines?

My approach involves using divs to mimic a table structure. Below is the header section of this table: <div id="table-header"> <div id="header-row"> <div class="rate-rule-column s-boarder"> ...

Tips for halting the navigation bar when scrolling

Creating a Navigation Bar: <div class="navigation-bar"> <div class="item">Home</div> <div class="item">About us</div> <div class="item">Our Services</div> <div class="item">Contact us</div ...

Ways to locate the drop target element using jquery

Can anyone help me figure out how to retrieve the id of the element where a draggable item was dropped by the user? I attempted to use the var tar = event.target; function, but it consistently returns the id of the main container instead of the desired ele ...

Adding a button to a shadow root that already exists is not the proper procedure

var shadow = document.getElementById( "3rd-party-div" ).shadowRoot; let style = document.createElement("style"); style.textContent = ` .custom_button{ padding: 10px; display:block; float:left; text-ali ...

Can the system automatically generate and display a new user ID right after a form is submitted?

Once users have submitted the basic information in a form, how can I ensure that the newly created userID is displayed in a hidden field on the header page? The initial form collects Name, Phone, and Email details. Upon submission, the 'userID' ...

Adding HTML elements or content dynamically in Angular can be achieved using various methods and techniques

Is there a way to dynamically insert an additional column into an HTML table when a button is clicked? I am looking to have a button below the table that, when clicked, will add the same number of rows currently in the table and introduce a new column. I ...

Troubleshooting issues with custom Bootstrap CSS implementation

I've been working on a website using Twitter Bootstrap and I wanted to add an interesting dynamic where an image starts off as grayscale and then transitions to full color when hovered over. Instead of modifying the Bootstrap.css, I decided to create ...

Using javascript to transform the entire list item into a clickable link

Currently, I am using PHP in WordPress CMS to populate slides on a slider. Each slide contains a link at the bottom, and my goal is to target each slide (li) so that when clicked anywhere inside it, the user is directed to the URL specified within that par ...

Twisting columns causing a problem with rows of consistent height

I am facing a challenge with my CSS HTML code where I need to create a snaking column of labels/values. If one of the values on the left spans two lines, the corresponding one on the right should also adjust its height accordingly. Currently, I can only a ...

Spotlight a newly generated element produced by the*ngFor directive within Angular 2

In my application, I have a collection of words that are displayed or hidden using *ngFor based on their 'hidden' property. You can view the example on Plunker. The issue arises when the word list becomes extensive, making it challenging to ide ...

Is there a way to prevent the Pace js plugin from running on page load, but still have it execute during Ajax requests only?

I have successfully implemented the jquery pace plugin with a progress bar theme. Everything is working well, but I am looking to make it run only on ajax requests. I have tried various solutions found through research, but haven't had any luck. Belo ...

sending form data using JQuery Ajax

Trying to pass two values to a PHP file through AJAX Here is my current code: PHP : <?php $name="Name with spaces"; ?> JS : var yourMessage = $("input#message").val(); $.ajax({ type: "POST", url: "send_message.php", data: "yourNa ...

determining if a button has been clicked using PHP

I am a complete beginner when it comes to php, html, and web development. Currently, I am in the process of learning php and have encountered a specific issue that I need help with. It is similar to the question asked in this link, but unfortunately, I did ...

Ways to customize a directive to show conditionally with no need for container elements

I have created a basic role-based security directive in angularjs. It's my first attempt at making a directive. My goal is to replace the following HTML: <authorize if-granted="GET_POSTS"> Hello WORLD!!!! {{name}} </authorize> with j ...

What is the best way to retrieve the canonicalized minimum and maximum values within the beforeShow method of jQuery UI's datepicker?

I have a pair of datepicker elements that I want to function as a range. When a value is set in the "low" datepicker, it should adjust the minDate and yearRange in the other datepicker, and vice versa with the "high" datepicker. The challenge I'm faci ...

How can I optimize the performance of JS-heavy pages on mobile devices?

As a website owner, I strive to optimize the performance of my site on mobile devices without the need for a separate "mobile-friendly" version or replacing large sections of code. With around 100K of JS code, including jQuery, I am determined to enhance b ...

Maintain the menu item color even after clicking

Here is the menu code snippet: <div class="header"> <div id="logo"></div> <ul class="menu"> <li><a href="/index.aspx">Home</a></li> <li><a href="/about.aspx"& ...

Top choice for asp.net form tooltip?

Searching for a tooltip solution to provide input instructions to users filling out a form. Recommendations for a user-friendly, straightforward code or plugin? Specifically looking for tooltips that appear when a textbox is selected, with the capability ...

Margin ambiguity in a vue.js application

I am facing an issue with my Vue.JS project setup. I want the App.vue to occupy the entire page and have different routes displayed within App.vue using router-view. But, when I try to add a margin to the content of my Game component, the margin seems to ...