Adjust the size of the div to match its parent's size when resizing

Can a div be resized to match its parent's size on window resize?

Here is the current HTML code:

<div class="sliderContainer">
    <div id="cyler">
        <div class="cy1" style="background-image:url(images/Keggy_Banner_Ie.jpg); background-position:center center; background-size:2000px 390px;  height:390px;"></div>
        <div class="cy1" style="background-image:url(images/Bravissimo_Banner_ie.jpg); background-position:center center;  background-size:2000px 390px; height:390px;"> </div>
        <div class="cy1" style="background-image:url(images/Radley_Banner_ie.jpg); background-position:center center;  height:390px;"> ></div>
        <div class="cy1" style="background-image:url(images/Tiger_Banner_ie.jpg); background-position:center center; height:390px;"></div>
    </div>
</div>

The sliderContainer class has a max width of 2000px and a min width of 1000px (overflow hidden)

Currently, the .cy1 divs load at full window size and the image is centered - but if I resize the window, the .cy1 divs remain at the original width.

I have tried using the following JavaScript to detect screen resizing and apply the new width to the div to make it stretch to its parent's size. However, I am facing issues with this approach as well. Any suggestions?

var sizl = $(window).width();
var delay = (function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

$(window).resize(function() {
    delay(function(){
        $("#cycler .cy1").width(sizl);
    }, 500);
});

Answer №1

Implement dynamic window width calculation in your resize event

$(window).resize(function () {
    debounce(function () {
        var newSize = $(window).width();
        $("#slideshow .slide1").width(newSize);
    }, 500);
});

Answer №2

To achieve this effect, you can follow these steps:

  let screenWidth = $(window).width();
  $('#carousel .slide1').css({'width':screenWidth});

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

Looping through an array of JSON objects in Javascript results in finding instances, however, the process records them

Currently, I am executing a script inside a Pug template. The script commences by fetching an array of JSON objects from MongoDB. I then stringify the array (data) and proceed to loop through it in order to access each individual JSON object (doc). Subsequ ...

Adding a drop shadow effect to SVG elements when hovered over

I am curious if it is feasible to have a drop shadow on my SVG element, but only make it visible when hovered over (:hover). I utilized the filter tag for the SVG and this is somewhat the effect I am aiming for. From what I have read, inline :hover is no ...

Methods for dynamically populating dropdown lists with JavaScript and Bootstrap

I have collected all 387 regions for the current date and now I want to dynamically populate a bootstrap dropdown with these regions using JavaScript. Below is the basic HTML code for a bootstrap dropdown: <div class="dropdown"> <button class ...

Limitations on Embedding Videos with YouTube Data API

I have been using the Youtube Data API to search for videos, but I am encountering an issue with restricted content appearing in the results. Specifically, music videos from Vevo are showing up even though I only want videos that can be embedded or placed ...

Refreshing the v-model in a child component

Within my parent component, the code structure is similar to this: <template> <ProductCounter v-model="formData.productCount" label="product count" /> </template> <script setup> const initialFormData = { ...

Access information via ajax from a php script

I am currently working on an AJAX code that sends data to PHP and then receives data from PHP... function updateDatabase(syncData){ $.ajax({ type : 'POST', async: false, url : 'php/syncData.php', ...

How can I determine if a page is being rendered in Standards mode or Quirks mode by IE7?

Having some issues with CSS on a webpage. The code is valid and adheres to Strict HTML standards. It displays correctly in most browsers, but I'm experiencing problems in IE (specifically version 7). Is there a way to determine if the page is being re ...

Managing PHP and AJAX: Strategies for handling and transmitting error responses

There are three main components involved in this process: An HTML form The AJAX connection that transmits the form data and processes the response from the PHP script The PHP script, which evaluates the data received, determines if it is valid or not, an ...

Creating a Select2 Dropdown Menu using jQuery

I am utilizing the kartik-v/yii2-widget-select2 in the following manner: use kartik\select2\Select2; echo $form->field($model, 'state_1')->widget(Select2::classname(), [ 'data' => $data, 'options' = ...

No content found in jQuery .text() values within iframe

I am experiencing an unusual issue with assigning a value to a variable using jQuery. I am unable to retrieve the values from .text() specifically on Spotify, while other functions on different sites are working fine. Even after the elements have loaded, i ...

Using Puppeteer to Retrieve Data from a Web Page with Multiple Divs of the Same Class

I am in the process of creating a puppeteer script to scrape an announcements website. The challenge I am facing is how to extract the content of each page element with the same class using a loop. Upon inspecting the DOM, it's evident that all the co ...

Using jQuery to pass variables within a single file

In my Javascript code, I have set some variables that I need to use in PHP later on. I know that I have to transfer these values using jQuery POST or GET methods, but despite reading the manuals, I still struggle to fully grasp how they work. Could someo ...

Terminate a targeted recipient following the completion of the on event

I am currently running a node service with socket.io and utilizing an event emitter to send updates based on user context. For example, context A may have users 1, 2, and 3 while context B has users 4 and 5. Once a successful connection is established wit ...

How can I alter the div once the form has been submitted?

Is there a way to change the background of a form and its results after submitting the form? I need to switch the image background to a plain color. I attempted to use a solution I found, but it doesn't seem to be working as expected. You can view my ...

Implementing Jquery: Show a class after clicking the button and then remove it

I am looking for a way to display a class when a button is clicked and then remove it when the same button is clicked again, repeatedly. I have multiple buttons so I cannot use the toggle function as it does not provide the desired behavior. I have tried v ...

Activating the Mobile Menu Function when the Window is Resized

I've developed a JavaScript function that triggers on window resize. When switching between landscape and portrait orientation on mobile devices or tablets, the window dimensions change. This functionality is also useful for browser testing on desktop ...

Exploring the integration of Firebase with Next.js using getServerSideProps

I'm trying to retrieve the 'sample' document from Firestore using getServerSideProps only if a user is signed in. However, the current code I have isn't working and just returns 'can't read'. Is there a better solution or ...

Is there a method available to incorporate a scroller into an nvd3 chart?

I am encountering an issue with my nvd3 chart. When I have a large amount of data that exceeds the width of the chart container, there is no scroll bar present and I'm struggling to figure out how to add one. I attempted to include overflow:scroll wi ...

Having difficulty centering an image in HTML?

I've been struggling to get the image (logo) centered on the top bar! I've tried using align-self: center;, display:block;, but nothing seems to work. It feels like I'm missing something here! Click here for the codesandbox link to view the ...

Unable to access the ajaxComplete function while using .ajaxComplete with a datepicker change function

I'm facing an issue with my datepicker where I need to trigger a query as soon as a date is selected. I've been trying to implement the functionality using .ajaxComplete but seem to be unable to access it properly. Any suggestions on how to resol ...