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

Place in the Middle of a Bootstrap Container

Struggling with aligning the elements of a Bootstrap well in the center? The badge is off to the side instead of below the arrows, and the vote buttons are not centered within the well. Check out the code snippet below: HTML: <div class="col-md-1 ...

Tips for utilizing the .clone() method within a loop or for each individual element of an array

Due to certain requirements, I find myself in a situation where I need to customize the invoice template within the software I use. The invoices are generated in HTML format, prompting me to consider utilizing Stylish and Grease Monkey for this task since ...

Error found in jQuery validation - TypeError: undefined

I am currently working on implementing the jQuery validation functionality into my form, following the default example provided on the official jQuery validation page. However, I have encountered an error that reads: Uncaught TypeError: undefined is not a ...

PHP Ajax login form to instantly authenticate without redirecting to a new page

After attempting to implement Ajax in my login form by following two different tutorials, I am still facing the issue of error messages redirecting to another page instead of displaying on the same page. I have tried to troubleshoot and compare my code wit ...

How to turn off pinch to zoom feature in Mozilla Firefox on a Microsoft tablet or touch screen display

My goal is to enable zooming and panning for an SVG across all devices and browsers. I've successfully implemented this using panzoom.js. However, I'm encountering an issue specifically with Mozilla Firefox on tablet devices and Windows touch sc ...

Placing a div directly beneath an anchor tag

I'm trying to create a dropdown menu that appears directly under the URL that triggers it. I have successfully made the menu appear, but I am struggling with positioning it correctly. Here is my HTML code: <div id="container"> Content here ...

What is the best way to send a POST request to my Rails controller using Ajax?

I'm facing an issue trying to transmit data from my JavaScript game-state on the front end to my controller and ultimately store it in the database. Unfortunately, I haven't received any response so far, and I'm struggling to pinpoint the re ...

Footer panel in Bootstrap not aligning to the bottom of the page

I'm experiencing an issue with my footer getting stuck in the middle of the page. Strangely, this problem only occurs on one specific page, and not on any other pages. I've tried various methods to fix it, but none of them have been successful. ...

AngularJS - issue with directive functionality on dynamically inserted classes

Check out this plnkr - I've got a button that toggles the class toggled on the body element. I've also developed a directive to trigger an alert when the toggled class is applied to the body element, but for some reason it's not working. H ...

using jquery, how can you send multiple parameters in an ajax request

Hello and welcome! I'm having trouble passing parameters through an ajax URL. I am attempting to send multiple data using the jQuery $.ajax method to my PHP script, but I can only pass a single data item when concatenating multiple data entries toget ...

Styling Columns with Borders and Gutters in Bootstrap

My goal is to create 4 divs, each with a width of 3 columns and a 3px border. I have successfully added the columns with borders, but I can't seem to get the border-box property to work as intended. I believe that I need the border to be an inner bord ...

What is the best way to ensure the checkbox functions correctly in this specific situation?

I am utilizing PHP, Smarty, and jQuery in the development of my website. Currently, I am working on implementing checkboxes within a Smarty template. Below is a snippet of my Smarty code related to checkboxes: <table cellpadding="5" cellspacing="0" bor ...

I am trying to figure out how to style each box individually, but I'm running into some confusion. There are three different classes to choose from, with the child class

Struggling to style the ten boxes individually? Renaming each class of the box child didn't work as expected? Wondering how to select and style each box with a unique background color? Also facing issues with displaying the logo image on the navigatio ...

Problem with flags series in Highcharts/Highstock

Can anyone help me figure out why not all the flags in the withdrawals series are displaying? For reference, you can view the following JS fiddle: https://jsfiddle.net/lucianpurcarea/5zxa0jsm/13/ The snippet of code below is responsible for creating the d ...

Fetching the duplicated data from a submitted form using serialize() method

When submitting form inputs using the serialize() function in jQuery and retrieving the data in PHP through a POST request, I encountered an issue where only the multi-select dropdown data was getting duplicated. The rest of the input data was coming in co ...

Enhance Your Jekyll Site with the Minimal Mistakes Theme Plugin

Hi there! I'm relatively new to website programming and could really use some assistance. I've been attempting to integrate the jekyll-lunr-js-search (https://github.com/slashdotdash/jekyll-lunr-js-search) into the minimal-mistakes theme, but I&a ...

Resolving problems with PHP source and JSON in jQuery autocomplete feature

My goal is to implement a jquery autocomplete feature with PHP, but I am facing an issue with the data structure required for each record in the autocomplete. Currently, I have successfully achieved this without PHP using the following code: $(function() ...

Sass compilation failed due to an error in the CSS code provided

Just starting out with sass, I recently downloaded bulma, installed gem, and sass. My attempt to compile a default sass file using sass style.scss style.css resulted in errors. Here is my directory structure: However, I encountered errors like the followi ...

Ways to transform a 4-column CSS table into a 2-column layout for mobile and tablet viewing

For my latest project, I am faced with the challenge of creating a responsive table that transforms from 5 columns to 2 columns on smaller screens. While using complex jQuery is an option, I prefer a more semantic approach. Additionally, I need to incorpor ...

The jQuery function that clears the form data

I have a webpage where clicking on a link changes the content of a div using the following code. The content is filled out using page1.php, which includes a form with dropdown lists. However, whenever I try to select something from a dropdown list, it res ...