Adjusting the image dynamically based on the height dimension

I am attempting to recreate that particular example as a practice exercise.

I am working on making the image div adjust dynamically when clicking the text above or below it.

Therefore, I created a sketch to experiment with. However, I am currently stuck because I am unsure how to calculate the remaining space when the expanded div is open so that the image fits within that space.

jQuery(document).ready(function($)
{
  $("#ex").click(function()
  {
    $("#expanded").slideToggle(200)
      if ($("#ex").text() == "infos")
      {         
         $("#ex").html("close")
         $("#fit-picture").css("height", "20px");
      }
      else 
      { 
         $("#ex").text("infos")
         $("#fit-picture").css("height", "100vh");
      }
  });  
});
body{margin:0}

.container{ 
    height: 100%;
  min-height: 100%;}

#expanded{
    margin-top: 0px;  
    background: gray;
    width: 50vw; height: 50vh;
}



#ex {
    display: block;
    width: 50vw;
    background-color:darkgrey;
    text-decoration:none;
}

#ex:hover {
  background:black;
}

.container{
height:100vh;
}

#fit-picture{
  width: auto;
  height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="container">
  <div class="expand">
  <a href="#" id="ex">infos</a>
</div> 
<div id="expanded" style="display: none;">the image is supposed to be reduced proportionally to fit the remaining space of the screen.
</div>
<img id="fit-picture"
     src="https://www.wanimo.com/veterinaire/wp-content/uploads/2018/11/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd8d3dacf96d1dad7d4cec396de8a8e8c8f8d8c898c89888a8282fb89c395d1cbdc">[email protected]</a>">
</div>

Answer №1

While it may not be the most adaptive solution since you need to define all heights beforehand, here is a potential workaround:

jQuery(document).ready(function($)
{
  
  $("#ex").click(function()
  {
    
    $("#expanded").slideToggle(200)

      if ($("#ex").text() == "infos")
      {         
        $("#ex").html("close")

        $("#fit-picture").animate({height:"45vh"}, 200) ;


       
        
      }
      else 
      { 
        $("#ex").text("infos")
        $("#fit-picture").animate({height:"95vh"}, 200);
      }
    
  });  
  
});

Answer №2

To change the image tag in HTML, you can use the code below:

<img id="fit-picture" src="https://www.example.com/image.jpg" class="center">

In CSS, add the following code:

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

It is recommended to specify the height and width in percentages rather than pixels. This allows the image to resize automatically when the window size changes, making it easier to manage.

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

"Using jQuery's animate() function to dynamically change text content

I'm currently venturing into the world of jQuery animate() and decided to create a presentation-style project for practice. However, I've hit a roadblock when attempting to alter the text within my div element in the midst of an animation utilizi ...

Selected selector failing to function properly

I have been diving into the world of jQuery selectors and wanted to share a small example I created. The idea is to display what has been selected from a dropdown menu, but unfortunately, my example isn't functioning properly. I must be making a small ...

Ways to create gaps among the links in the Bootstrap 4 navigation bar?

I recently put together a Bootstrap 4 navbar and now I'm looking to increase the spacing between the items on the right side of the navbar. <nav id="menu" class="navbar navbar-expand-md sticky-top"> <div class="site-branding navbar-brand c ...

Challenges with text wrapping in a continuously scrolling container in CSS

I am currently developing a project that requires infinite horizontal scrolling for cells containing thumbnails and text. While most of the functionality is in place, I am facing an issue with text wrapping. The CSS and HTML code are provided below. The fi ...

Increase the right border size by half when the image is being hovered over

Seeking assistance with creating a hover image with a right border of color #000 that covers only 80% of the image's full length. I have attempted adjusting other "half border" codes for this purpose without success. Any suggestions on how to achieve ...

Does the "too much recursion error" result from the if clause?

Before I send my form data to a PHP script, I am performing a jQuery check: $(document).ready(function(){ $("#submit").submit(function(e){ var vari1 = $("#inputhidden1").val(); var vari2 = $("#inputhidden2").val(); if(vari1 == 1) { ...

Turn off Grammarly on a WordPress Site

It has come to my attention that Grammarly is causing issues with the functionality of the forms on our website. I have come across information stating that adding the following attribute to all input fields can resolve this problem and block Grammarly fr ...

Script tags in PHP-Diff are ignored

I am currently utilizing the PHP library at https://github.com/rashid2538/php-htmldiff to compare two HTML pages. However, I am facing an issue where the content inside <script></script> tags is also being compared, which disrupts the functiona ...

The Bootstrap class "text-right or text-end" does not seem to be functioning properly when applied to the <a> element

I've been attempting to apply the text-right or text-end classes to the anchor element at the end, but for some reason, it's not working as expected. I even tried using style="text-align:right !important;", but it still doesn't pro ...

Using multiple instances of the Summernote Wysiwyg editor on a single page with identical placeholders

I have been using summernote successfully in my application, but I have encountered a minor issue where I have 2 editors on one page. The placeholder in the second editor seems to be inheriting the placeholder of the first editor. Is there a simple solut ...

Words spilling onto the picture

The issue of the text col-xs-9 col-md-9 overlapping with the image col-xs-3 col-md-3 has been persistent. Even after attempting to add the img-responsive class to the img src, it continues to not work as expected. Interestingly, CSS was intentionally avoid ...

What are some techniques for creating a responsive table in CSS that includes images?

After much searching, I am confident that using "Skeleton CSS" will help achieve my goal. The concept is simple, but implementation is proving to be difficult. I am trying to create a table with multiple images that will change width to 750px when display ...

Generate a Safari-compatible Bookmark URL that is functional

I've attempted to use the JavaScript "AddFavorite" function in my code, but unfortunately, it's not functioning properly in Safari. It seems to work in IE and perhaps Firefox from what I can recall, but all my efforts to make it work in Safari ha ...

Adjusting Width with Javascript

My goal is to specify the width of a RadNumericTextBox that appears as an input element on the page using JavaScript. I have managed to set the width, however, the telerik controls come with built-in JavaScript functions that reset the input element' ...

The toggle remains on even when the status is off

I'm working with both Angular and jQuery. Currently, I have a toggle button that is set to remain active when the page initially loads, even if the status has been changed and saved as inactive. Is there a way to ensure that the toggle button remains ...

Interacting with Watir and Cucumber, unfortunately the link is not functional

Being new to the world of Watir and Cucumber, I am currently in the process of running an automation script to create Live IDs. The link that I want to click on the webpage is labeled "New" and it should take me to a form where I can add a new contact for ...

The auto search feature seems to be malfunctioning after clicking the button

Despite my best efforts, I am still unable to resolve this issue. I have tried numerous links and code snippets, but I am encountering some difficulty in finding a solution. THE ISSUE: I have an input field with type 'Text' for searching employ ...

Avoid loading Google Maps JavaScript library multiple times to improve website performance

I'm facing an issue with loading Google Maps API script twice in a Bootstrap modal through AJAX. Initially, I have a button that, when clicked, triggers an AJAX call to load an HTML file into the modal. The HTML file contains the Google Maps API scrip ...

using jquery to retrieve the current time and compare it

This is the code I currently have: var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() aler ...

Tips for accessing the Spotify API using Ajax and Jquery

Apologies for my lack of experience, but I am struggling to call an API for my school project. I need to retrieve the Json data from this API, specifically information regarding the latest album releases. Here is the structure of the data I am looking for: ...