tips for inserting text within the confines of a range slider

I am currently utilizing the noUiSlider, although any range slider library should suffice. I have a compact widget (see here: https://codepen.io/chapkovski/pen/pobRwZj) where users need to divide the range into three sections:

var slider = document.getElementById('slider-color');

noUiSlider.create(slider, {
  start: [6000, 12000],
  connect: [true, true, true],
  range: {
    'min': [2000],
    'max': [20000]
  }
});

var connect = slider.querySelectorAll('.noUi-connect');
var classes = ['c-1-color', 'c-2-color', 'c-3-color'];

for (var i = 0; i < connect.length; i++) {
  connect[i].classList.add(classes[i]);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/14.6.2/nouislider.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/14.6.2/nouislider.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
  .c-1-color {
    background: red;
  }
  
  .c-2-color {
    background: yellow;
  }
  
  .c-3-color {
    background: green;
  }
</style>
<div id='slider-color'></div>

The issue I am facing is how to include text within the ranges. For example, for the middle range (yellow), it should display something like 'Second category: XX%' (where XX% varies depending on the handles' positions). When I try the following:

.c-3-color::after {
content:'my text goes here';
}

the layout gets completely distorted.

Answer №1

In the parent element, there is a scale applied, causing it to grow in size. To balance this effect, adjustments need to be made on the child or pseudo child elements. See the example below:

.c-3-color::after {
  content:'my text goes here';
  display: block;
  position: absolute;
  top: 50%;
  left: 33%;
  transform: translateY(-50%) scale(4,2);
}

DEMO (with modified code for smaller lines)

var slider = document.getElementById('slider-color');

noUiSlider.create(slider, {
  start: [6000, 12000],
  connect: [true, true, true],
  range: {
    'min': [2000],
    'max': [20000]
  }
});

var connect = slider.querySelectorAll('.noUi-connect');
var classes = ['c-1-color', 'c-2-color', 'c-3-color'];

for (var i = 0; i < connect.length; i++) {
  connect[i].classList.add(classes[i]);
}
.c-3-color::after {
  content:'my text goes here';
  display: block;
  position: absolute;
  top: 50%;
  left: 33%;
  transform: translateY(-50%) scale(3,2);
  font-size:12px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/14.6.2/nouislider.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/14.6.2/nouislider.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
  .c-1-color {
    background: red;
  }

  .c-2-color {
    background: yellow;
  }

  .c-3-color {
    background: green;
  }
</style>
<div id='slider-color'></div>

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

Issue with Angular7: Service worker not working properly

In my Angular service worker, I have successfully cached sounds (mp3 files) for offline playback. Below is the ngsw-config-json file configuration: { "index": "/index.html", "assetGroups": [{ "name": "app", "installMode": "prefetch", "reso ...

Changing the identification of elements

I noticed that when I open object X and Y, they have the same fields, tabs, and checkboxes. However, I am confused as to why the IDs of the checkboxes and other elements are different. Can you explain what gwt-uid(number) means? Please take a look at the a ...

Having difficulty showcasing API call results in a Vue.js component

I am currently experimenting with Vue.js in an attempt to showcase results from a Wikipedia API call within a component using the v-for directive. However, I seem to be encountering some backend issues that I cannot pinpoint. To access the jsFiddle link, ...

Leverage the power of Next.js Dynamic routing query object with an SWR fetch request

While working with Next.js dynamic routing, I am facing an issue where my SWR fetch request is being called before the query object is properly set. This occurs specifically when using the routing query object. Let's take the example of a dynamic rou ...

The ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

How to import an HTML file using TypeScript

I need to load an html file located in the same directory as the typescript file and return it from the function. public ...(... ) : angular.IHttpPromise<string> { ... return $http({ method: 'GET', url: &apos ...

Prevent the use of repetitive borders by utilizing display: inline-block

I've been experimenting with the behavior of div elements when used as a table, rather than using the traditional table element or display properties. The main reason for this is that I have found tables and display properties to be problematic for my ...

gathering various GET and POST data points

I am working on a form where the data is being passed through the URL like this ?choice=selection+A&choice=selection+C When collecting it in a form, I'm using the $_GET method to retrieve it as an array: $temp = $_GET["choice"]; pri ...

Although the buttons have the class ui-btn-inline, they do not exhibit inline behavior in the

My header has 2 rows. The first row is a ui-grid-solo and the second row is a ui-grid-c. The issue I'm facing is with the alignment of buttons within the first row. Despite using the class ui-btn-inline, the buttons are not behaving as inline elements ...

"Unraveling nested URL queries with Nest.js: A step-by-step

My NestJS application is facing an issue where I need to retrieve a query from the URL. The problem arises when trying to access the nested object `createdAt`, as it is stored as an object and cannot be retrieved directly using @Query. An example of the r ...

Tips for utilizing a vue.js nested for loop with two arrays using v-for

The issue has been resolved, and both my parent view and child component code are now correct and functioning properly I am using Vue.js with the goal of iterating similarly to a nested for loop to display a matrix table. Initially, I tried to achieve thi ...

Literal Route Waypoints Guidance Service

I am currently working on an angularjs directive that utilizes the Google Maps Direction Service. In my controller, I have a getFunction that watches for changes in a scope variable. Once the scope variable changes, it triggers the calculateAndDisplayRoute ...

Enhance the v-autocomplete dropdown with a personalized touch by adding a custom

Currently utilizing the v-autocomplete component from Vuetify, and I am interested in incorporating a custom element into its dropdown menu. You can see the specific part I want to add highlighted with a red arrow in this screenshot: This is the current s ...

Adjust the JSON format that is retrieved from an Ajax call

I am working with a JQuery plugin that includes the following code snippet: transformResult: function(response, originalQuery) { } Within this function, I have the task of converting Json data from the originalQuery to the response format: [ { "Id": ...

How to update razor page content dynamically in ASP.NET Core without refreshing the entire page using JavaScript/jQuery

I have a question about reloading a table on my webpage automatically. I want the page to reload every second based on the value entered in an input field, without the need to click any buttons. Here is what I have tried: <input id="txtRefresh&quo ...

Can I safely refresh the browser during integration test specifications?

We currently have a set of Protractor and Jasmine specs for our AngularJS project. Is it acceptable to use the following code snippet to clean up things between specs: afterAll(function(){ browser.restart(); } Your insights on this matter would be gre ...

What is the best way to simulate the axios call within my express controller?

My axios call is already separated into a module and tested using mocked axios. However, I am facing a dilemma when it comes to testing the controller that imports this function. I understand that it needs to be mocked, but I am uncertain about the most ef ...

What is the best way to apply fill to SVG using Tailwind CSS?

I tried using the code provided, but unfortunately, I was unable to Override the fill. The issue seems to have been resolved, but can someone help me achieve this using tailwind CSS? <div className=""> <svg className="text- ...

JavaScript random number functionality experiencing an unexpected problem

function generateRandomNumbers(){ var max = document.getElementById('max').value; var min = document.getElementById('min').value; var reps = document.getElementById('reps').value; var i; for (i=0; i<reps ...

Can HTML be integrated into QwebKit using JavaScript?

Is it necessary to load JavaScript objects into QWebkit during the application loading process? I find using setHtml with formatted HTML and JavaScript strings difficult to maintain. So my question is, can I embed the HTML and JavaScript as resources int ...