JavaScript implementation of a carousel slider with responsive design

I have successfully implemented a feature carousel slider using the jquery.featured.carousel.js file along with some CSS.

Here is the jsfiddle link to my project: LINK.

When running this code on localhost, I noticed that I need to refresh the page every time in order to get a responsive view when resizing the window.

Below is the JavaScript code I am utilizing:

<script type="text/javascript">
$(document).ready(function() {
    initSlider();
});

$(window).resize(function() {
    initSlider();
});

function initSlider() {
    // Large block of conditional statements for setting carousel options based on window width
}
</script>

In my code, there is a specific section:

$(window).resize(function() {
    initSlider();
});

I removed this section and found that the functionality remained the same.

Note: Due to limitations, I cannot utilize CSS media queries for achieving a responsive layout with this carousel.

I would appreciate any ideas or suggestions on how to modify the code to achieve a responsive design without needing to refresh the page constantly.

Thank you.

Answer №1

First things first, it's important to implement a setTimeout function in your resize function to prevent it from being triggered excessively whenever the page is resized.

You can achieve this by adding the following code:

$(window).resize(function() {
  if(this.resizeTO) clearTimeout(this.resizeTO);
  this.resizeTO = setTimeout(function() {
    $(this).trigger('resizeEnd');
  }, 50);
});
$(window).bind('resizeEnd', function() {
  initSlider();
});

Furthermore, it might not be wise to reset the carousel settings every time there is a resize event. It would be beneficial to have an option within the carousel where you can adjust the values without needing to initialize it again. Unfortunately, upon reviewing the Feature carousel, it appears that such functionality is lacking.

One potential solution could involve utilizing css media queries since certain classes like centerImage, imageRight, and imageLeft are added to elements. By leveraging these classes, you should be able to exert more control over the carousel display.

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

Exploring a JSON file to generate a customized array for implementing autocomplete functionality in jQuery

I'm currently working on developing an autocomplete feature for a search bar that will display names of places in Norway. The data is being fetched from a REST API URL provided by a third party. As an example, when the input is "st" there are two res ...

Is there a method to redirect and display a JavaScript message efficiently?

Once the user has updated a record, I want to automatically redirect them to another page where they will be notified with a JavaScript message (alertify.notify()) confirming that the update was successful. I am unsure if it's possible to dynamically ...

Exploring the power of TypeScript within the useContext hook

I recently started following a React tutorial on YouTube and decided to convert the project from JavaScript to TypeScript. However, I am facing difficulties with implementing useContext in my code. Any help or guidance would be greatly appreciated. If yo ...

What is the most efficient way to print multiple JSON arrays simultaneously?

When looping through a database using an array, the following code snippet is used: $checkedProducts = $request->input('products'); $p = null; foreach($checkedProducts as $checkedProduct){ $p .= DB::table('products')->where( ...

Forwarding images from a server to a client using socket.io and node.js

I am encountering an issue where I am trying to receive an image via socket.io in node.js and then forward it to a client (browser). However, the image sent through the message to the browser is not being recognized or displayed. Interestingly, if I save ...

Assigning controller variables within an ajax request

I'm new to AngularJS and I have a question about controller attributes. I've created an attribute called 'anuncio', which contains an array of objects as shown below: var anuncioModule = angular.module('anuncioModule',[]); a ...

Value binding with conditional rendering in VueJS 2

My goal is to utilize VueJS 2 to render an inline condition while simultaneously adding a value to a DOM element. I am aware that I can use v-if to control the visibility of elements based on conditions, but how can I achieve an inline condition? For exam ...

Saving JSON data into an HTML element using Handlebars templating

Is there a way to save the entire JSON object within an HTML element as a data attribute? let a = {name : "sample", age : "34"} $.find('#someDiv').data('adata', a); Is it possible to achieve the same result using Handlebars when creat ...

Error: The function `res.status` is unsupported

I've been working on a function to allow uploading images to imgur through my express API (nodejs), but I'm running into an issue when calling a function that returns a promise: TypeError: res.status is not a function at uploadpicture.then T ...

Ways to send a list with dictionaries in an HTML template and retrieve their data

Within my Django project, I am dealing with a specific list: my_list=[{'id':1,'username':'sometext'},{'id':2,'username':'someothertext'}] I have passed this list to the template using the followi ...

Print Vue page with the same styling as the original

How can I add a print button to my application that prints the page with the original CSS styles? I am currently using window.print() function and have a separate file called print.scss with specific print styles. @media print { header {display:none; ...

What is the best way to ensure the remaining PHP script is executed after using json_encode()?

My current project involves creating a form with four input fields: name, email, phone, and message. To handle the submission process, I am utilizing JavaScript in conjunction with Ajax to send the user inputs to a PHP file for validation and mailing using ...

Tips on switching the positions of two links when they are clicked using jQuery

<div class="applications-wrapper"> <div class="eye" id="onetwo"><a class="fa fa-eye" href="#"></a></div> <div class="bar" id="twothree"><a class="fa fa-bars" href="#"></a></div> </div> My ...

Troubleshooting: Unable to modify value with function in AngularJS

Why can't I change a value using a function in AngularJS? html: <div ng-controler='TestCtrl' id='TestCtrl'> <h1>Test: {{test.name}}</h1> <div ng-hide='showTest'> <div class=&a ...

Retrieving specific data through a Jquery-Ajax fetch

I'm currently working on a webform that utilizes knockout.js. In one section of the form, I need to retrieve a value based on the selected item in a select list. Here's the snippet of code from my viewModel: self.discoveryForms = ko.obse ...

What is the best way to pause the previous timer and display only the upcoming timer in a React application?

When comparing the scheduled times with the current date-time, I noticed that the values for upcoming schedule times are displayed correctly. However, when the time has already passed, it shows negative values. Instead of this, I would like to display &apo ...

Hide additional tabs on the page when the width of the tabs exceeds the page width

I am currently working on a Google module tab application. I have successfully added the tab control with drag and drop functionality within a specific area. However, I now need to limit the number of tabs displayed on the page. Regardless of the total cou ...

What causes the shift in the position of the div element?

I have been struggling with a problem in my code. Whenever I hover over this element, it rotates as intended but it also changes its position. I can't figure out why this is happening. Can someone please help me debug this? I would greatly appreciate ...

Is it possible to swap the src of the Next.Js Image Component dynamically and incorporate animations in the

Is there a way to change the src of an image in Nextjs by using state and add animations like fadein and fadeout? I currently have it set up to change the image source when hovering over it. const [logoSrc, setLogoSrc] = useState("/logo.png"); <Image ...

How can I generate a checkbox in a database with a field that allows for enabling and disabling?

I am looking to design a table that includes questions, checkboxes, and text boxes. All the questions are stored in a database and called through an array. In addition, I want to create disabled textboxes that become enabled when their corresponding checkb ...