Leveraging the CSS-Element-Queries Library for emulating the functionality of CSS media queries

Recently, I used a nifty CSS-Element-Queries tool to perform basic element manipulations that trigger whenever the window is resized.

In simple terms, my goal was to dynamically adjust an element's attribute based on the current width of the window – essentially, I wanted to respond to every resize event by modifying a specific element accordingly.

Following the tutorial instructions precisely, I implemented the code as shown below. However, despite my efforts, it seems like something is amiss as the functionality does not seem to be working at all:

<script src="css-element-queries/src/ResizeSensor.js"></script>
<script src="css-element-queries/src/ElementQueries.js"></script>

<script>
    new ResizeSensor(jQuery(window), function(){ 
        var windowWidth = $(window).width();

        if (windowWidth < 1024 && windowWidth > 768) {
            $(".slideshow").attr("data-cycle-carousel-visible", 4);
        }
        if (windowWidth <= 768 && windowWidth > 480) {
            $(".slideshow").attr("data-cycle-carousel-visible", 3);
        }
        if ((windowWidth <= 480) && (windowWidth > 320)) {
            $(".slideshow").attr("data-cycle-carousel-visible", 2);
        }
        if (windowWidth <= 320) {
            $(".slideshow").attr("data-cycle-carousel-visible", 1);
        }
    });
</script>

Answer №1

It is important to monitor the resizing of the window and then trigger your resizer function upon each resize event.

// Keep an eye on window resize
 $( window ).resize(function() {
   // Invoke function every time window is resized
   resizr();
 });

 var resizr = new ResizeSensor(jQuery(window), function(){ 
    var screenWidth = $(window).width();

    if (screenWidth < 1024 && screenWidth > 768) {
        $(".slideshow").attr("data-cycle-carousel-visible", 4);
    }
    if (screenWidth <= 768 && screenWidth > 480) {
        $(".slideshow").attr("data-cycle-carousel-visible", 3);
    }
    if ((screenWidth <= 480) && (screenWidth > 320)) {
        $(".slideshow").attr("data-cycle-carousel-visible", 2);
    }
    if (screenWidth <= 320) {
        $(".slideshow").attr("data-cycle-carousel-visible", 1);
    }
});

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

Update all website links to new domains

Looking for advice on solving a persistent issue. Our forum has existed on multiple domains throughout the years. One domain we used 5-6 years ago got lost, and now some posts in our forum still have links that direct to this old domain. What is the most e ...

Having difficulty viewing the images clearly

My JavaScript codes for scrolling images are not displaying all the images when viewed on Google Chrome. Is there a solution available? Below is the included JS code and CSS. data=[ [" images/img1.jpg"," Image1","pic01.jpg"], [" images/img2.jpg","Image2 " ...

Is your MJML column layout not stacking properly on mobile devices?

I've been struggling with a basic 2-column design created using the online editor at mjml.io. I can't seem to get it to stack on mobile devices. The email looks identical on desktop (Outlook 365 for PC) and mobile (Outlook for iOS on iPhone 13) ...

Is it feasible to set a default value in an HTML input field that is not editable using JavaScript?

Is there a way to set a default value in an input field of HTML that is not editable, and then allow users to add additional text to it? For example, having 'AB' as the default and uneditable starting characters, followed by any numbers such as A ...

Simplest method for defining an associative array

Looking to create an array in the format shown below: [0: 0, 1: 1, 2: 2] This is achieved using the following code snippet: var arr = []; for(i=0; i<3; i++){ arr[i] = i; } Upon execution, my array appears as follows: [0, 1, 2] The values with ...

Facing an issue with AngularJS where I am able to retrieve data.data, but struggling to assign it to a scope variable

Currently, I am developing a website using AngularJS that retrieves questions and multiple-choice answers from an Amazon Web Services table. This data is used to dynamically create a prelab questions page. The http get request is functioning properly; when ...

Image renders only on a single page in Express.js

I am facing an issue while attempting to display an image on multiple pages using Pug and Express. The image should be visible on the routes '/', 'data', and 'update'. While it is successfully displayed on the root '/&ap ...

What is the best way to handle responses in axios when dealing with APIs that stream data using Server-Sent Events (S

Environment: web browser, javascript. I am looking to utilize the post method to interact with a Server-Send Events (SSE) API such as: curl https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H ...

Focus on a specific div element while bypassing its parent

Can I directly target the element with .div-3 class, even if its parent does not have an id or class? <div class="wrapper"> <div> <div> <div class="div-3"> </div> </div> </div> </div> I am aware ...

The blur filter in Safari causes colors to appear distorted

I am having an issue trying to add a blur filter to an SVG element. It appears that Safari is not rendering the colors correctly. Take a look at this example: <svg height="110" width="110"> <defs> <filter id="f1" x="0" y="0"&g ...

How should I refer to this style of font?

After trying to implement a font from bulletproof @font-face as a template for my website, I am facing issues. My goal is to utilize the perspective-sans black regular font. Despite uploading the necessary files - including the css style sheet provided in ...

The colorbox division refuses to be shown

Having trouble revealing a hidden div within a colorbox upon click I'm stumped on how to make the div visible inside the colorbox The div is currently hidden, but I need it to be shown when displayed in the colorbox <div class="test">test< ...

Fusing JQuery Image Enlargement with Dynamic Bootstrap 5 Carousel

I have a website showcasing Wordpress galleries where images pop up in a Bootstrap 5 Modal, leading to a Bootstrap Carousel. Using a php foreach loop, the images are displayed within a post loop, one image per slide. My goal is to implement hover magnifi ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

Display loader until the document body has finished loading

Many developers have shared solutions for displaying a loader while an element is being loaded. However, my challenge is unique. I want to display a loader before the document body is fully loaded. The issue arises from an AJAX call in my code: var url = ...

Adjust image size dynamically while keeping the original aspect ratio

Is there a way to scale variable portrait and landscape images dynamically to fit proportionally within a browser window? You can find my current image resizing attempt here: http://jsfiddle.net/6pnCH/4/ I would like the image to be already scaled vertic ...

EaseSlide 'Basic Slideshow' created by Ameenullah Fails to Function

I'm trying to embed a carousel into my webpage. I copied the code from Bootsnips but have been unable to solve this issue with other solutions. This is my HTML Code: <div class="container"> <div class="row> <div class="col ...

populating all available space in layouts using bootstrap columns

I am attempting to create a grid layout using bootstrap, but I am facing an issue with positioning one of the columns. I have tried adjusting the placement of the divs and using offsets/pulls, but so far I have not been successful. To explain what I am a ...

Are the commands overlapping?

Currently, I am in the process of developing a dynamic discord chat bot using JavaScript and node.js. One of my goals is to implement a specific command without interfering with an existing one. The bot functions flawlessly across all servers where it is ...

Angular.js and DAO design pattern

Admitting my newness to Angular.js and lack of experience with frameworks like Backbone or Knockout, I am venturing into building an application that interacts with a server using a RESTful API. After delving deep into the angular documentation and blog po ...