What steps can I take to ensure that the full tingle modal, which includes an image, is visible when the

Upon loading the page, I noticed that if the browser width is greater than 540px, the modal displaying an image gets cut off (see figure below). What steps should I take to ensure that the vertical scroll bar appears immediately?

https://i.sstatic.net/cJvvl.jpg

The current project I am working on utilizes the tingle modal plug-in. Here is how it is linked:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="tingle.min.css">
    <style>
        html, body {
            height: 100%;
        }

        img {
            height: auto;
            width: 100%;
        }
    </style>
    <title>Tingle Modal</title>
</head>
<body>
    
    <script src="tingle.min.js"></script>
    <script src="modal.js"></script>
</body>
</html>

modal.js is responsible for creating the modal with the image:

function createModal(content) {
    let $modal = new tingle.modal({
        footer: false,
        stickyFooter: false,
        closeMethods: ["overlay", "button", "escape"],
        closeLabel: "Close",
        cssClass: ["modal"],
        beforeClose: function() {
            return true; // close the modal
        },
        onClose: function() {
            $modal.destroy();
        }
    });
    $modal.setContent(content);
    $modal.open();
}

createModal("<div id='modal'><img id='sample' src='sample.jpg' /></div>");
console.log(document.getElementById("sample").offsetHeight);

I have observed that when zooming in or out, or resizing the browser window, the expected behavior of having a scroll bar occurs. On initial load, the image height is set to 0. Setting a pixel value for the image height is not feasible as multiple modals contain images of varying sizes.

To view the behavior firsthand, visit this link: CodeSandbox. Ensure you adjust the embedded browser width in CodeSandbox and then refresh to see the issue.

I attempted the following solution, but it resulted in the scroll bar appearing even when the entire modal fits within 100vh, which is unwanted:

.tingle-modal {
    overflow-y: scroll;
}

Answer №1

Consider applying the following styles

.modal-container {
    overflow-y: auto;
}

If you want to keep the close button in place, you can use:

.modal-close-button {
    position: sticky;
    align-self: end;
}

Instead of

 createModal("<div id='modal'><img src='example.jpg' /></div>");

try this approach:

const image = new Image();
image.onload = () => {
  createModal("<div id='modal'><img src='example.jpg' /></div>");
};
image.src = "example.jpg";

Answer №2

After examining the code within tingle.min.js, I discovered that it checks for modal overflow by adding a tingle-modal--overflow class if necessary. However, because the image hadn't finished loading when the height check was performed, I had to revise the process by rechecking after invoking the createModal function as shown below:

document.getElementsByTagName("img")[0].onload = function() {
    const $modal = document.getElementsByClassName("tingle-modal")[0];
    if (window.innerHeight <= $modal.clientHeight)
        $modal.classList.add("tingle-modal--overflow");
};

For those using jQuery, here's an alternative solution:

$(".tingle-modal-box img").on("load", function() {
    const $modal = $(".tingle-modal");
    if (window.innerHeight <= $modal.height())
        $modal.addClass("tingle-modal--overflow");
});

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

Xap or js Silverlight media player

Currently developing a Silverlight video player, I stumbled upon the JW Player for inspiration. I know that Silverlight apps are typically contained within .xap files, but JW Player utilizes JavaScript scripts to implement Silverlight logic. Can you plea ...

Ensuring props are resilient even when an incorrect type is provided during testing

In my development using the MERN stack and Redux, I encountered an issue while testing props on one of my components. Despite defining all types and running tests, they still pass even with incorrect data entered. I have tried specifying the shape of each ...

Split into two lines, the Bootstrap carousel includes 28 indicators for easy navigation

I am currently using a Bootstrap 4 carousel with 28 pictures. However, I am facing an issue where the indicators on small and medium devices are not displaying properly (some indicators seem to be missing). They are not breaking into new lines as expected. ...

Using Javascript to Swap the Content of an Element

Recently, I've been delving into the world of javascript and have hit a roadblock. I understand how to instruct javascript to set a specific value for an ID, but now I have a new challenge: I want to create javascript code that can retrieve informati ...

Replacing the tbody element in React with centered text using inline styles ---If you want

I am working with an empty array in React that I translate into state. When the array is empty, I want to insert a text that says "no match events yet..." into a react-bootstrap Table's tbody. In the current setup, I am struggling to center the text ...

Issue: Catching errors in proxy function calls

I am currently using Vue 3 along with the latest Quasar Framework. To simplify my API calls, I created an Api class as a wrapper for Axios with various methods such as get, post, etc. Now, I need to intercept these method calls. In order to achieve this ...

generate code to automatically output the content of a website

I'm in the process of creating a program that automatically navigates to a website and prints a page. Unfortunately, I'm encountering difficulties in getting it to function properly. I've attempted using the selenium chrome driver, but with ...

What is the best way to transform a string into emojis using TypeScript or JavaScript?

Looking to convert emoji from string in typescript to display emoji in html. Here is a snippet of the Typescript file: export class Example { emoji:any; function(){ this.emoji = ":joy:" } } In an HTML file, I would like it to dis ...

Delete the gridline of the column

I'm struggling to remove the border that is encircling my image thumbnail label. I attempted to create a new class "noborder" and added it under my .col-md-4.noborder css rule, setting the border to 0 none and box-shadow to 0 none but unfortunately it ...

What situations warrant the choice of an Isomorphic JavaScript application?

Recently, there has been an increase in tutorials and examples discussing Isomorphic web applications with react.js. As a beginner myself, I find it challenging to determine whether this is necessary or not. For instance, my application will primarily be ...

Tips for managing JavaScript within PHP code and vice versa

Usually, I include a lot of JavaScript on the website. However, the client informed me that they were unable to view the website correctly as they had disabled JavaScript on their end. After investigating, I discovered this issue. Now, my dilemma is wh ...

Does the default input default to text format?

I have a somewhat peculiar question. I'm currently experimenting with Javascript for a plugin and came across an interesting scenario. Let's say I have an input element that is structured like this: <input type='cc' id='cc&apo ...

Adding <meta> tag to the <head> section of a particular page in WordPress and HTML

I need to insert a meta tag for the site description within the head section of a specific page, before the opening body tag. The website is created using WordPress & Elementor. I prefer not to include it globally in the theme's header option like sh ...

Tutorial on integrating jquery ui's '.droppable' feature with the jQuery '.on' method

I have a main div with three inner divs labeled 1, 2, and 3 as shown below: <div id="main"> <div style="height:30px; background-color:yellow;" class="bnr">Banner</div> <div style="height:30px; background-color:yellow;" class=" ...

Navigating Struts - The art of breaking free from encoded symbols in a text field

I'm having trouble displaying the trademark symbol in a default text within a Struts text field. Despite trying various methods, the symbol does not appear and only outputs the code instead. Can someone please provide guidance on how to properly displ ...

Creating a key-shaped design with CSS: A step-by-step guide

Looking to create a unique shape within an HTML div? By utilizing custom CSS in an HTML table, I can design a layout where one column features a rounded avatar while the other displays a rectangular box. However, I am struggling to make the circle overlap ...

Is it possible to utilize gulp to eliminate all require.js define([...]) wrappers throughout the codebase?

Is it possible to test my app without using require.js in order to assess the performance and file size if all files were concatenated into a single one? I'm contemplating using gulp to gather all *.js files in the app, running a gulp-replace to elim ...

How can you pause a YouTube video using jQuery?

My custom jQuery slider consists of three panels that slide smoothly by adjusting their left CSS values. Everything works perfectly, except for one issue - I have a YouTube video embedded in one of the slides, and it continues playing even when the slide i ...

Problem with loading images on an HTML webpage

https://i.sstatic.net/aQ0oD.png After refreshing the page, I'm encountering an issue where the image is not loading. However, if I switch tabs from MEN to WOMEN and back again, the image will display without any problem. Even making changes in the CS ...

Is there a way to make an accordion close from a nested component in react?

I'm in the process of developing a page to update product information on an e-commerce platform using NextJS. On the individual item page, I have structured the image upload section within an accordion. After the images are uploaded, I want to reset t ...