Utilize the Power of JQuery Range Slider to Dynamically Update CSS Property Values (specifically column / fr and column-gap)

Currently, I am in the process of creating a JQuery range slider that can dynamically update the columns and column-gaps of my photo grid. While searching for a solution, I came across a relevant post where someone had shared their code. However, there seems to be an error in the code provided, and unfortunately, I lack the expertise to rectify it.

If you're interested, here is the link to the post: Use JQuery Range Slider to Live-Update a CSS Value

The snippet of code they shared is as follows:


$(document).ready(function(e) {
  
    $("#slider-grid").on('input change', function(e) {
        var minmax = this.value;
        $("#wrapper).css("grid-gap", minmax + 'px')
    });
  
    $("#slider-height").on('input change', function(e) {
        var minmax = this.value;
        $(".item").css("height", minmax + 'px')
    });
  
    $("#slider-width").on('input change', function(e) {
        var minmax = this.value;
        $("#wrapper").css("grid-template-columns", "repeat(auto-fit, minmax(" + minmax + "vw, 1fr))")
    });
  
});

Your assistance in resolving this issue would be greatly appreciated!

Answer №1

Thanks to the suggestion of @CBroe, implementing jQuery fixed my issue. I have included a new code snippet below to demonstrate the JavaScript functionality.

$(document).ready(function(e) {
  $("#slider-grid").on('input change', function(e) {
    var minmax = this.value;
    $("#wrapper").css("grid-gap", minmax + 'px')
  });
  
  $("#slider-height").on('input change', function(e) {
    var minmax = this.value;
    $(".item").css("height", minmax + 'px')
  });
  
  $("#slider-width").on('input change', function(e) {
    var minmax = this.value;
    $("#wrapper").css("grid-template-columns", "repeat(auto-fit, minmax(" + minmax + "vw, 1fr))")
  });
});
body {
  margin: 20px;
  background: black;
}

#wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20vw, 1fr));
  grid-gap: 20px;
}

.item {
  height: 150px;
  border: 0px solid #eee;
  box-shadow: 0 0px 0px 0 rgba(0,0,0,0.1);
  display: inline-block;
  border-radius: 0px;
  margin: 0vw;
  background: orange;
  border: 0px solid orange;
}
<!-- To address the issue, import jQuery using the following link: -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="contrastSlider">
     <input id="slider-grid" type="range" value="contrast" max="100" min="0" step="1"/>
  <input id="slider-height" type="range" value="contrast" max="100" min="0" step="1"/>
  <input id="slider-width" type="range" value="contrast" max="100" min="0" step="1"/>
</div>

<div id="wrapper">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Answer №2

Hey there, I reviewed your solution and noticed that the jQuery library is missing. I made some code modifications in this snippet to make it more efficient. Additionally, please avoid using the change event as it leads to unnecessary calls to the DOM.

$(document).ready(function(e) {
    const $grid = $('#slider-grid');
    const $height = $('#slider-height');
    const $width = $('#slider-width');
    $(document).on("input", {$grid, $height, $width}, function (e) {
        var curEl = e.target.id;
        if(curEl == $grid[0].id) {
            $("#wrapper").css("grid-gap", $grid.val() + 'px')
        }
        if(curEl == $height[0].id) {
            $(".item").css("height", $height.val() + 'px')
        }
        if(curEl == $width[0].id) {
            $(".item").css("width", $width.val() + 'px')
        }
    });
});
body {
    margin: 20px;
    background: black;
}

#wrapper {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(20vw, 1fr));
    grid-gap: 20px;
}

.item {
    height: 150px;
    border: 0px solid #eee;
    box-shadow: 0 0px 0px 0 rgba(0,0,0,0.1);
    display: inline-block;
    border-radius: 0px;
    margin: 0vw;
    background: orange;
    border: 0px solid orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="contrastSlider">
    <input id="slider-grid" type="range" value="contrast" max="100" min="0" step="1"/>
    <input id="slider-height" type="range" value="contrast" max="100" min="0" step="1"/>
    <input id="slider-width" type="range" value="contrast" max="100" min="0" step="1"/>
</div>

<div id="wrapper">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</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

Arranging an ng-repeat list in a dynamic order

I am currently working with AngularJS and focusing on reordering the ng-repeat list. The list represents online users who can receive messages at any time. When a user receives a message, I update the UI to display the most recent message beneath their nam ...

Oops! It seems like there is an issue with reading the property 'filter' of an undefined object. Any ideas on how to resolve this error

Having an issue with a custom filter that is causing an error "ERROR TypeError: Cannot read property 'filter' of undefined". I need help fixing this as it's preventing anything from rendering on the page. Any suggestions on what changes I sh ...

What is the best way to arrange these badges in a row?

I'm struggling to horizontally align these badges next to each other, as inline-block isn't working and they keep stacking vertically instead of side by side. How can I achieve the alignment shown in the image below? This is the desired appeara ...

What is the best way to symbolize a breadcrumb offspring?

In my table representation, the breadcrumb is shown as: <ol class="breadcrumb" data-sly-use.breadcrumb="myModel.js"> <output data-sly-unwrap data-sly-list="${breadcrumb}"> <li itemscope itemtype="http://data-vocabulary.org/ ...

Trouble Loading TypeScript Class in Cast Situation

I've encountered an issue with my TypeScript model while using it in a cast. The model does not load properly when the application is running, preventing me from accessing any functions within it. Model export class DataIDElement extends HTMLElement ...

How come my SQL query is functioning properly in pgAdmin, but I am encountering a query error (Error: Connection Terminated) when attempting to execute it from the server?

Currently, I am working on my Capstone project which involves storing telemetry data in a PostgreSQL 9.5 database and using node for the server. The issue I am facing is that every time I attempt to send a query from the server, I encounter a query error ...

To dismiss the Div, simply click on any area outside of it. Leveraging the power of SVG, D3

I need a way to hide my div by clicking outside of it. My SVG has a background and a graph with nodes on top of that. I have a special node (circle) on the graph, clicking on which makes a box appear. To show the box, I use the following code: d3.select ...

Create a selection menu in an HTML dropdown list using unique values from a JSON object that is separated by commas

I'm working with a JSON object that contains multiple key value pairs, one of which is the Languages key. This Languages key holds comma-separated values such as "English, Hindi, French," and so on. My goal is to extract distinct values from the Lang ...

Canvas Frustratingly Covers Headline

Several months ago, I successfully created my portfolio. However, upon revisiting the code after six months, I encountered issues with its functionality. Previously, text would display above a canvas using scrollmagic.js, and while the inspector shows that ...

What is the best way to forward a file upload request from a Next.js API to another API?

Trying to crop an image within a Next.js application, then sending it through an API route within the app before reaching an external API endpoint is proving to be a challenge. The process works fine without going through the API route, but once that step ...

What is the process for including a selected-by option in a VIS network graph?

I'm trying to outline the neighboring nodes of the node that has been selected (highlightNearest). https://i.sstatic.net/lynhu.png Unfortunately, I haven't had success achieving this using JavaScript. Link to StackBlitz ...

Tips for Resizing a Div While Maintaining Aspect Ratio

I'm trying to create a div with a background image size of 1170px x 929px that will maintain its aspect ratio when the width is resized. Can someone explain how to achieve this using CSS? Below is the code I have so far: <section class="box"> ...

Is there a way to retrieve the title, description, and image URL of a URL through Ajax, similar to how Facebook shares a link?

Currently, I am developing a project that involves allowing users to submit a URL. The system will then extract the title, images, and description from the provided URL and offer the option to toggle between different images. Upon submission, these extrac ...

Interactive Dropdown-Buttons dynamically inserted

I have been utilizing a third-party library called Materializecss from this link: My issue arises when attempting to create a Dropdown feature upon clicking a button. It works perfectly fine when I manually place the button in the HTML and click on it, tr ...

Display and conceal frequently asked questions using JQuery

I'm currently facing an issue with using JQuery to toggle between showing and hiding content when a user clicks on a specific class element. Here is my HTML code: <div class="faqSectionFirst"> Question? <p class="faqTextFirst" style=' ...

Error message "500 Internal Server Error" is displayed when making an AJAX post request in

After choosing an option from a dropdown list, I am trying to load data using an AJAX request. However, upon selection, I encounter a 500 (Internal Server Error). How can this issue be resolved? Check out my AJAX code below: $('#teacher').on(&a ...

An adaptive Bootstrap grid that collapses and changes based on screen dimensions

Currently, I am in the process of creating a collapsible grid using Bootstrap to display details for each item: If you would like to see a visual representation of the concept, click here. To assist in my project, I have been referring to a helpful threa ...

What Methods Can I Use to Acquire the Device ID?

Is there a way to retrieve the device ID using Jquery or pure JavaScript, without relying on Cordova for my project? I have come across solutions that involve Cordova, but I prefer not to use it. Are there any alternatives available? Edit: By unique ID, ...

Navigate to a different position of a specified element within an expanding pivot grid

I'm currently dealing with a pivot table/grid that allows users to expand and collapse rows. My challenge is figuring out how to scroll to an expanded row after an expansion occurs. Typically, I use the following code for scrolling functions: ...

Load all HTML content from external files using a Bootstrap modal

Using Bootstrap 4.1.3 I have a specific requirement that is different from the solution provided here: Bootstrap Modal Dynamic Content The scenario involves fetching the complete modal HTML content from an external file and then presenting it as a modal. ...