Personalized labels for your JQuery Mobile sliders

Struggling to make this work correctly, I aim to introduce tick marks and custom labels into jQuery Mobile's slider widget. My goal is to add tick markers at 0, 25, 50, 75, and 100 with a unique string above each tick. Additionally, I want these labels to scale alongside the jQuery slider as the page size changes. Currently, I can attach labels sans ticks, but it only suits one screen dimension.

    
        $( ".ui-content" ).on( "slidestop", function( event, ui ) {
            event.preventDefault();
            event.stopImmediatePropagation();
            var newState = $("#slider-0").val();
        } );
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"/>


    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

</head>
<body>



<div data-role="page">
    <div data-role="main" class="ui-content" style="padding: 40px;">

        <div role="main" class="ui-content">
            <div class="labels" >
                <label for="points" style="display: inline; padding: 55px;">volume off</label>
                <label style="display: inline; padding: 50px;" for="points">quiet</label>
                <label style="display: inline; padding: 50px;" for="points">normal</label>
                <label style="display: inline; padding: 50px" for="points">loud</label>
                <label style="display: inline; padding: 50px" for="points">max</label>
            </div>
            <input type="range" data-highlight="true" data-popup-enabled="true" name="slider-0" id="slider-0" value="50" min="0" max="100" data-theme="b" data-track-theme="a"/>
        </div>
    </div>
</div>

</body>
</html>

Experimented with using CSS % for padding without expected results. Explored suggestions from jQuery UI Slider Labels Under Slider, Add tick marks to jQuery slider?, and Set font on custom span jQuery Mobile slider labels but aligning ticks and labels with the slider remains elusive. Seemingly a simple formatting issue, yet consolidating all components proves challenging. Any assistance is highly appreciated.

Edit

Further research led me to this blog: https://css-tricks.com/equidistant-objects-with-css/. Following their advice utilizing flex styling in my labels class with

<div class="labels" style="display: flex; justify-content: space-between;">
, tested on Chrome and Safari with success. However, implementing ticks remains uncertain.

Answer №1

A little while ago, I shared a blog post detailing how to customize the jQM slider widget:

Check out the blog post here

The blog post includes an example of adding tick marks and labels that adjust with the slider. Below is the updated code with the labels now positioned above the slider and a demonstration of the code in action on CodePen:

HTML

<div id="example2a" class="example">
    <label for="theSlider2a">Slider with Tick marks:</label>
    <input type="range" name="theSlider2a" id="theSlider2" min="0" max="100" value="60" />
</div> 

JavaScript (customize the label text inside the spans)

var ticks  = '<div class="sliderTickmarks "><span>volume off</span></div>';
    ticks += '<div class="sliderTickmarks "><span>Quiet</span></div>';
    ticks += '<div class="sliderTickmarks "><span>Normal</span></div>';
    ticks += '<div class="sliderTickmarks "><span>Loud</span></div>';
    ticks += '<div class="sliderTickmarks "><span>Max</span></div>';

$("#example2a .ui-slider-track").prepend(ticks);

CSS

.sliderTickmarks{
    -webkit-box-sizing: border-box; 
    box-sizing: border-box;
    height: 100%;
    width: 25%;    
    float: left;
    border-right: 1px solid #888;
}
.sliderTickmarks span{
    position: relative;
    left: 100%; top: -135%;
    margin-left: -16px;
    font-size: 12px; font-weight: normal; white-space: nowrap;
}
.ui-slider-track > div.sliderTickmarks:first-child{
    border-right: 0; width: 0;
}
.ui-slider-track > div.sliderTickmarks:first-child span{
    margin-left: -5px;
}
.ui-slider-track > div.sliderTickmarks:last-of-type{
     border-right: 0;
}

View the DEMO on CodePen

Answer №2

It may seem unconventional, but using a table to neatly display these labels can be effective.

        <div role="main" class="ui-content">
            <table class="labels">
            <tr>
              <td>volume off</td>
              <td>quiet</td>
              <td>normal</td>
              <td>loud</td>
              <td>max</td>
            </tr>
            </table>
            <input type="range" data-highlight="true" data-popup-enabled="true" name="slider-0" id="slider-0" value="50" min="0" max="100" data-theme="b" data-track-theme="a"/>
        </div>

Answer №3

Consider trying out this solution. I personally believe that utilizing the flexbox method is the most effective approach. To address your issue with ticks, you may want to consider incorporating an offset div with a black border on the left.

// I have made no modifications to this code snippet
$( ".ui-content" ).on( "slidestop", function( event, ui ) {
    event.preventDefault();
    event.stopImmediatePropagation();
    var newState = $("#slider-0").val();
} );
.labels {
  display: flex;
  justify-content: space-between;
  width: 92.5%;
  margin-left:7.5%
}
.verticalLine {
    height:10px;
    border-left: solid black;
    margin-left: 50%;
}
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"/>


    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

</head>
<body>



<div data-role="page">
    <div data-role="main" class="ui-content" style="padding: 40px;">
        <!-- Remove padding. Add tick divs -->
        <div role="main" class="ui-content">
            <div class="labels" >
            <label for="points" style="display: inline;">volume off
              <div class="verticalLine"></div>
            </label>               
            <label style="display: inline;" for="points">quiet
              <div class="verticalLine"></div>
            </label>
            <label style="display: inline;" for="points">normal
              <div class="verticalLine"></div>
            </label>
            <label style="display: inline;" for="points">loud
              <div class="verticalLine"></div>
            </label>
            <label style="display: inline;" for="points">max
              <div class="verticalLine"></div>
            </label>
        </div>
            <input type="range" data-highlight="true" data-popup-enabled="true" name="slider-0" id="slider-0" value="50" min="0" max="100" data-theme="b" data-track-theme="a"/>
        </div>
    </div>
</div>

</body>
</html>

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

SWR Fails to Recognize Environment Variables

I'm struggling to utilize ENV variables when using the SWR hook for data fetching. My current approach is as follows: const videoURLWithEnv = `https://youtube.googleapis.com/youtube/v3/search?part=snippet&channelId=UCwkj9jcrMZCcbcIa6nF5LNQ&ma ...

Customize jQuery Autocomplete choices depending on another jQuery Autocomplete input

I need to implement a feature where users can select a company and then an employee from that company. I came across a similar question on this link, but I specifically want both inputs to be autocomplete-enabled. This is the code snippet I currently have ...

Automated web browser capture downloading feature

Currently, I am in the process of developing a tool to streamline tasks at my workplace. One feature we need is an aspx webpage where users can input information, then click on a submit button. When the submit button is clicked, a javascript download dialo ...

Managing the Response from Google Geocode API in a Vue JS Component

I am facing an interesting challenge. I have a scenario where users can choose between two options - using their current location or entering a zip code. When a user inputs a zip code, I need to make a call to the Google geocode API to retrieve the central ...

How to create custom options in a JavaScriptMVC Controller's event listener?

When working with JavascriptMVC's Controller, they have a unique format for handling events Rather than using traditional jQuery methods $(function(){ $('#tabs').click(someCallbackFunction1) $('#tabs .tab').click(someCallback ...

Limiting the number of checkboxes selected in a Checkbox Group based on

I am working on a checkboxGroupInput that has 4 options (denoted as A, B, C, D). My goal is to restrict the selection to only 2 choices. The user should be able to pick a 3rd option. In this scenario, only the newly selected (3rd) and previously selec ...

What is the process for creating a React Component with partially applied props?

I am struggling with a function that takes a React component and partially applies its props. This approach is commonly used to provide components with themes from consumers. Essentially, it transforms <FancyComponent theme="black" text="blah"/> int ...

JavaScript Delayed Redirection

I'm attempting to launch opera from the command line and have it redirect to a page after 30 seconds. Here's what I currently have: C:\Programme\Opera\opera.exe -newpage javascript:function%20func1(){window.location.href='htt ...

Concerns with the Width of Gutters in Susy

I am working with a 24 Susy column grid and trying to create boxes that span 6 columns each, allowing for 4 boxes per row. However, I also want to add gutters around them within a wider container that is 24 columns wide. Despite my efforts, the columns do ...

In regards to navigational bars that are oriented horizontally

Trying to create a simple navigation bar with horizontal links but facing issues. Can't seem to make it work despite following examples online: <!-- Navigator --> <div id="nav"> <ul> <li><a href="#"& ...

Retrieving data from the firebase database by filtering based on the child's specific value

Looking at the firebase database, I can see that only the 'name' field is available. Now, I am trying to retrieve the 'quantity' value associated with that specific 'name'. Can someone provide me with a sample firebase query i ...

What is the best way to resize a PDF to match the width and height of a canvas using

I need help with a project involving rendering previews of PDF documents in a UI similar to Google Docs. {documents.map((doc) => { return ( <div key={doc.id} className=" ...

Exploring the properties of a file directory

As I try to access the d attribute of a path that was generated using Javascript, the output of printing the path element appears as follows: path class=​"coastline" d="M641.2565741281438,207.45837080935186L640.7046722156485,207.0278378856494L640.698 ...

The interval between successive ajax requests

Seeking assistance with calling a service that returns one row of data at a time. The goal is to fetch 100 rows, with a specified time gap between each hit to the service, ideally around 200 ms. Previously used jquery for similar calls: for i=1 to 100 ...

The variable for Google Maps has not been defined

I'm currently working on developing a map that will display multiple markers based on custom posts with metaboxes. The end goal is to have a map showing markers with the post title, some description, and a link to the post. Although I feel like I&apo ...

How to Populate Object Literal with Multiple Objects in AngularJS

When I click on "Evan", I receive the following response: {"id":1,"name":"Evan"} If I click on "Robert", I will get: {"id":2,"name":"Robert"} Is there a way to modify this code so that it follows the aforementioned steps and generates an object similar ...

Encountering difficulties in populating mongoose document following a model query

Hi everyone, this is my first time posting on SO so I'm hoping for some positive outcomes. I've been using Mongoose in a current project without any issues until now. The problem arises when trying to populate object references after querying fo ...

Ways to center a spinner on the screen using CSS during loading

Upon loading the page, my spinner appears in the center of the screen after a second. However, in the initial frame of the page, it is not centered but positioned on the top-left corner instead. How can I ensure that my spinner starts off centered from the ...

Orchard's TinyMce now supports a drop event with jQuery integration

Currently in the process of constructing a website using Orchrad. I have implemented the tinymce4 html editor for building landing pages without any issues thus far. However, my current challenge involves capturing the jQuery drop event within the TinyMce ...

Difficulty arise when AJAX array interacts with MySQL database table

One of the challenges I'm facing on my website involves a series of buttons that correspond to specific table columns in a MySQL database. Both the button IDs and column names are identical. My goal is to gather these IDs into an array, then use AJAX ...