If the height of the window changes, then update the CSS height accordingly

Here is the code that I am using:

$(document).ready(function() {

    $('body').css('overflow', 'scroll');
    var heightWithScrollBars = $(window).height();
    $('body').css('overflow', 'auto');

    alert('heightWithScrollBars = ' + heightWithScrollBars);

    if ( heightWithScrollBars < 700 ){
       $(".vert, .simply-scroll-clip").height('270px'); 
   }        
});

In a linked stylesheet, it is defined that both .vert and .simply-scroll-clip elements should have a height of 400px.

Even though when the screen size is less than 700, the if {} block gets executed, the height change does not happen. Interestingly, if I directly run

$(".vert, .simply-scroll-clip").height('270px');
in the console, it works perfectly fine. But for some reason, it doesn't work when the page loads initially.

Can someone explain why this might be happening?

Thank you!

Answer №1

To achieve this, utilize the viewport-width property along with the resize event.

Set the height using vw units.

.vert, .scroll-clip {
    height: 3vw;
}

Then proceed to resize:

var updateZIndexOn = $(".vert, .scroll-clip");

$(window).resize(function() {
  updateZIndexOn.css("z-index", 1);
});

Reference: CSS-Tricks || Employed this technique in a past project.

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

I am having difficulty retrieving the JSON object that was sent by the servlet

$(document).ready(function() { var path = null; console.log('${pageContext.request.contextPath}/loadfile'); $.ajax({ dataType: "json", url: '${pageContext.request.contextPath}/loadfile&apos ...

Guide to dynamically refreshing a section of the webpage while fetching data from the database

I'm currently developing a website using PHP & MySQL where some queries return multiple records. Each record has its own dedicated page, allowing users to navigate between them using arrows. So far, everything is working smoothly. However, I've ...

What is the best way to calculate the total of all the prices listed in the span class="total"?

On my webpage, I have two sections - one for processors and the other for RAM. To retrieve prices from the database through AJAX, how can I total up all the prices listed within the span elements with the "total" class? <td> <span class="m ...

Refreshing an AJAX call automatically in Knockout JS

Greetings everyone! I'm currently working on setting up a simple setInterval function to automatically refresh my data every minute. The line that is giving me trouble is: setInterval(incidentViewModel.fetchdata,60000); I also attempted this: windo ...

Displaying specific data points

I am encountering an issue where I want to select multiple values and have each selected value displayed. However, when I make a selection, I only see one value from a single box. I do not wish to use append because it keeps adding onto the existing valu ...

Adjust input quantities for product variations in Woocommerce

In my WooCommerce store, I encountered an issue with a variable product that has 3 product variations. Unfortunately, the quantity to sell for each variation needs to be fixed and cannot be adjusted by the customer. For instance: We have 2 Red items ava ...

Ways to display the main image on a blog post

This piece of code is designed for displaying the relevant post associated with wp_ai1ec_events function display_event($atts ) { global $wpdb; $event = $wpdb->get_results("SELECT * FROM wp_ai1ec_events ORDER BY start"); ...

Retrieve the input value from a Bootstrap Form Input

When using a Bootstrap Form Input to allow the user to enter text, I am wondering how to save that text and use it as a parameter in a function. Below is the HTML snippet I have: <!--Directory Input Form--> <div class="row"> <div class= ...

Toggle Jquery feature will dynamically add the "required" attribute to the input field when the specified div is visible, and it will automatically remove the attribute when the div

I am new to using jQuery and I am facing an issue with my code. I want to make a checkbox act as a toggle with jQuery. When the checkbox is clicked and the toggle displays the div, I want to add the required attribute to the checkbox input. Similarly, when ...

Enhance your interface with stunning JQuery code snippets

We are in the process of developing a web application that will be fully 2.0 compliant (utilizing AJAX and more). Although I am primarily a rich-client and server-side developer, my knowledge of Javascript is limited to a functional level. However, based ...

Issue encountered when attempting to push jQuery AJAX requests into an array

I'm attempting to store ajax requests in an array called deferreds. However, I'm consistently encountering the error message below: Uncaught SyntaxError: missing ) after argument list As a reference, I've been using this guide: Pass in an ...

"Creating a duplicate of an element by utilizing the `next`

I have a dilemma involving two divs within a section of my project - one div is dynamically created while the other exists statically. My goal is to transfer the non-dynamically created div into the one that is generated dynamically. let adContainer = $ ...

The .ajaxSubmit function in jquery.form.js seems to be malfunctioning when using the data option

I'm currently working with the jQuery plugin jquery.form.js and I'm looking to programmatically submit a form while including a file. Although I've set up the code and options to work with $.ajax, it's not functioning with .ajaxSubmit. ...

Tips for implementing automatic line wrapping in a PDF document using React-PDF

I am experiencing an issue with a PDF rendering where very long words are not wrapping onto a new line, instead overflowing the container and disappearing off the page. Below is the setup causing this problem. The styles are listed followed by the actual ...

Retrieve data using AJAX and dynamically show it on an element using CodeIgniter

Just to clarify, I am brand new to AJAX and CodeIgniter. I came across this article which seems helpful for beginners like me: My goal is to update an Input Field in a form whenever the select html element changes. <select class="select" name="formul ...

Having trouble making an Ajax request using the Yahoo API?

Any thoughts on why this code is failing? $(document).ready(function () { doAjax("http://somedomain.com/page.aspx"); }); function doAjax(url) { if (url.match('^http')) { $.getJSON("http://query.yahooapis.com/v1/public/yql?" + ...

Toggle Visibility of Elements with Javascript and Html

I've been working on implementing a "Show All / Hide All" feature. Currently, clicking on the text opens the image and text individually. However, I am looking to add a functionality for expanding all divs at once. To see how it currently functions, ...

Creating a vertical scrollbar with CSS flexbox for columns set at 100% height in a row for FF, IE, and

I am currently working on an HTML application that needs to fit perfectly within a designated space without causing any page level scrollbars. I have utilized flexbox styles extensively to achieve this, but unfortunately, I am facing compatibility issues w ...

Form with multiple select options in Rails

I am working on developing a multi-select form that allows users to transfer IP addresses from the left select box to the right side (I'm not sure what that element is officially called). My current form code displays a dropdown instead of the desire ...

Adjusting Text Color on Buttons

I am looking for a simple button with clear color and black text, but the default button has white text. I need a way to change the text color. Check out my current app <View style={styles.fastlog}> <Button style={styles.bouton} title= "Con ...