Customize your background and font colors with jQuery UI Slider

I'm encountering an issue with the new design of my website, which involves using jQuery and jQueryUI (specifically SliderUi) along with some css manipulation.

Whenever I slide the #slider, the width of .left is updated based on (ui.value from 1-100 + '%'), triggering a slide and background transition. My question is whether it's possible to change the color of text based on the slider's threshold or value. If so, can you provide suggestions on how to achieve this? See snippet below. Thank you in advance!

$(document).ready(function() {
  $( "#slider" ).slider({
    max: 101,
    min:1,
    value: 51,
    slide: function(event,ui) { 
      var percentage = (ui.value)-1;
      $('.left').css("width",percentage +'%');
    }
  });
});
#slider {z-index: 5;background: #666; }
.left, .right {position:absolute; top: 0; left: 0; height: 100vh;}
.left {background: #000; width: 50%; z-index: 1;}
.right {background: #fff;  z-index: -1; width: 100%;}
.main-content {z-index: 3; position: absolute; top: 0; left: 0; width: 100%;}
h3 {color: blue; text-align: center; margin-top: 30px;}
<link href="https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<div id="slider"></div>
<div class="left"></div>
<div class="right"></div>
<div class="main-content">
  <h3>Certain parts of my font should change color based on the slider-ui threshold</h3>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

Answer №1

Update! Click here

HTML:

<div id="container"></div>
<div class="top"><h3>Lorem ipsum dolor sit amet</h3></div>
<div class="bottom"><h3>Consectetur adipiscing elit</h3></div>

CSS:

.top, .bottom {position:absolute; top: 0; left: 0; height: 100vh;overflow:hidden;}
.top h3 {color: red; text-align: center; margin-top: 30px;width:100vw;}
.bottom h3 {color: blue; text-align: center; margin-top: 30px;width:100vw;}

Original Answer: To achieve the desired font color change based on slider-ui threshold, one approach is to calculate an RGB color value using a function with the slider percentage as input. The concept of creating rainbows in JavaScript can be applied here. Check out this resource for more information: Rainbow Colors in JavaScript

$(document).ready(function() {
  $( "#container" ).slider({
    max: 101,
    min:1,
    value: 51,
    slide: function(event,ui) { 
      var percentage = (ui.value)-1;
      $('.top').css("width",percentage +'%');
      $('h3').css('color', rgb(percentage));
    }
  });
});

function rgb(p){
    var frequency = .08;
       r = Math.sin(frequency*p + 0) * 127 + 128;
       g = Math.sin(frequency*p + 2) * 127 + 128;
       b = Math.sin(frequency*p + 4) * 127 + 128;
     return  'rgb(' + Math.round(r) + ',' + Math.round(g) + ',' + Math.round(b) + ')';
}

Link to Example

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

Converting JSON data from one structure to a different format

Could you assist me in transforming this JSON data into the desired format specified in the result section? [ { name: "FieldData[FirstName][Operator]", value: "=" } { name: "FieldData[FirstName][Value]", value: "test& ...

What steps can I take to ensure that empty and null string fields are not included in my documents in Firestore using Reactjs?

I am currently in the process of adding items to a document, and my main concern is ensuring that empty strings and null instances do not result in unnecessary fields being added. My goal is to create fields only when there are actual values present. awai ...

get data from a JSON array with no specific name on the provided URL

Struggling to extract specific values from a JSON file located at the following URL: The content of the URL data is as follows: [ { "key":{ "parentKey":{ "kind":"user", "id":0, "name":"test 1" }, ...

Changing the value of an element in an array at a specific index using Mongoose and MongoDB

Within my database, I have a field in the schema known as: priority_data: [ String ] This is a collection of data that holds priority information. An illustration of what could be stored in this field is: "priority_data": ["Photo","Video","Music"] The o ...

An improved method for populating object values with checkboxes

Apologies for the vague question, but finding a suitable title was tricky. This is my jQuery code: const selectionInfo = {}; const housenrChecked = $("#housenr_export")[0].checked; selectionInfo.housenr = housenrChecked ? 1 : 0; const phoneChecked= $(" ...

Merging HTML table columns with Laravel 8 when headers are the same

As I work on developing an election website, I am facing an issue with the table displaying the number of votes for candidates from different sections. The problem is that multiple columns are showing up with the same candidate name, and I want to merge th ...

Positioning the div in the center

Having trouble centering a div inside a 100% wide container. Here is the HTML code: <div id="body-outside"> <div id="body-inside"> content </div> </div> And here is the CSS: #body-outside{ width:100%; flo ...

Navigating through JavaScript links in Selenium (Scrapy) and returning to the initial page: A step-by-step guide

Having some difficulties with pages that have javascript links embedded in them. This issue arises when the page contains a list of cities with javascript in their links. Each link needs to be navigated individually, scraping information and then returning ...

Combining Angular 1.x with Angular 2 for enhanced development

Can angular2 be used in a website that employs an angular 1.x theme? What obstacles might arise during the development process? ...

Employ multiple "or" clauses in Sequelize statements

I am trying to create a more complex where statement in sequelize with 3 or conditions. An example of the SQL where clause: where (c1 = 'a' or c2 = 'b' or c3 = 'c' or c4 = 'd') and (c5 = 'e' or c6 = &ap ...

Is anyone else experiencing issues with the Express middleware that checks for IDs? Looking for suggestions on how to fix

Currently working on a project with Node js utilizing Express and MongoDb for the backend. In this project, USERS have the ability to post COMMENTS, so I have created a middleware to access the DELETE route and check if the USER ID matches the ID of the in ...

Utilize HighCharts to seamlessly implement multiple series with the help of AJAX requests

I am facing an issue with plotting the performance results on HighCharts using AJAX. The problem lies with the .addSeries API call not functioning correctly. I need assistance in determining if my approach is correct. $(function () { $('#chart3&a ...

The text content of all drop-down menus simultaneously changes rather than being updated individually

I've implemented three drop-down menus using Bootstrap on my website. https://i.sstatic.net/M7sTF.png I want the selected menu item to update the content of that specific drop-down menu when clicked by a user. https://i.sstatic.net/sjHus.png However ...

Adjust image to fit inside a compact popup window - Implementing React.js and Bootstrap

Hello, I could really use some help with a problem I'm facing. I am currently working on my personal portfolio website using react.js and bootstrap. I've integrated the react popupbox package, but I noticed that on small screens, my picture is no ...

A guide to accurately accessing form data in Jquery Ajax requests

My beforeSend function is not working properly, as the background color does not change. I suspect it may be due to not correctly referencing the id variable posted from the form. Below is the relevant HTML and JS code: HTML <div id="rec<?php echo ...

When the hero banner text is too long, dynamic generation will insert it below the 3 action cards to prevent overlap, incorporating styling to maintain clarity and organization

I am encountering an issue with my dynamically generated hero banner. The issue arises when the body text of the banner is longer than usual, causing it to run behind the action cards positioned at the bottom of the banner. Here is a visual representation ...

Having trouble with updating label text in MUIDataTable in ReactJS?

Looking to implement multi-language support in MUI Datatables. I have been able to modify the translations, but when attempting to change languages by providing a different object with new translations (verified using console log), the label texts do not u ...

The Beauty of Organized Data: Understanding Multiple Columns in VUE JS Projects

I'm currently developing a VUE JS app that calculates route costs for employees based on specific route details. I am looking to create a VUE JS component that will only display a dropdown list with three columns: List of countries Day fee for each ...

Can you provide an explanation for why the new data is not appearing on the screen?

My JQuery Code looks like this: $.ajax({ url: "/Configuration/TestPlan/GetTestPlanDdlList", type: 'POST', dataType: 'json', ...

javascript map sorting by value

I am working with an object that has a many-to-one mapping setup: { 'a' : 'one', 'b' : 'two', 'c' : 'one', 'd' : 'two' } My goal is to transform this object into the ...