Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself:

  center: function() {
     var top=Math.max($window.height() - $modal.outerHeight(),0) / 2;
     var left=Math.max($window.width() - $modal.outerWidth(),0) / 2;

     $modal.css({
        top: top + $window.scrollTop(),
        left: left + $window.scrollLeft()
     });
  }

While this setup works well on desktop and mobile devices without zoom, it poses an issue when users zoom in on their screens. In such cases, the function miscalculates the position and the modal appears outside the viewport.

Is there a solution to ensure this function properly centers the modal even on zoomed-in mobile devices? Alternatively, I would be open to detecting a mobile device and displaying the modal within the zoomed part of the viewport to ensure visibility.

If anyone has insights or suggestions on how to address this challenge, your help would be greatly appreciated as I'm currently unable to make progress.

Answer №1

If you want the modal to always be centered on the screen, you can achieve this using CSS only:

/* Customize height/width properties as needed */
.modal {
  bottom: 0;
  left: 0;
  margin: auto;
  max-height: 400px;
  max-width: 700px;
  min-width: 250px;
  position: fixed;
  right: 0;
  top: 0;
}

I hope this solution works for you!

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

Why is Angular.orderBy not displaying any data on the Page?

Embarking on my first Angular project with Firebase Firestore, I am excited to showcase some of the code below. I recently learned Angular and here is what I have accomplished so far: Component.html <section class="rank"> <p class=& ...

Is it possible for amCharts to show the data properly?

Starting out with amCharts and javascript can be a bit overwhelming. Here is the structure of my html file: <!DOCTYPE html> <html> <head> <link rel="shortcut icon" href=""> <title>chart created with amCharts | amChar ...

Positioning elements in CSS using percentages allows for flexible and responsive

I am new to web design and I am trying to create a webpage similar to the one shown in this image. However, I want to set the height and width of the divs using percentage values instead of pixels. Whenever I attempt this, the boxes end up overlapping or ...

Issue encountered while trying to access PHP script within table data utilizing Ajax

I have a small project that involves displaying different news articles. I need to incorporate the news_all.php file into the table data in dashboard.php without using the include('news.php) function. Instead, I am utilizing Ajax methods to achieve th ...

CSS - Placeholder styling not being effective when selectors are grouped

Why is Chrome successful at executing this code: .amsearch-input::-webkit-input-placeholder { color: red; } <input class="amsearch-input" placeholder="It works!" /> ...while it doesn't work in other browsers: .amsearch-input::-we ...

Ways to ensure a file upload is not mandatory when processing data using jQuery Ajax

Here is a form I have created: <form name ="order" id="order" action="" method="POST" role="form" enctype="multipart/form-data"> <input class="checkbox pull-left" type="checkbox" name="check_list[]" value="p1">&nbsp;Product 1 <i ...

Alternative to updating object coordinates in Fabric JS 1.7.9 - seeking solutions for migration challenges

Update: JSFiddle: https://jsfiddle.net/Qanary/915fg6ka/ I am currently working on implementing the `curveText` function (found at the bottom of this post). It was functioning properly with `fabric.js 1.2.0`, but after updating to `fabric.js 1.7.9`, I not ...

Res.write inexpressively proceeding without waiting for the completion of an array of promises

snippet const processAll = async (tasks) => { let processes = []; tasks.forEach(task => { if (task === "1") processes.push(asyncProcess1); if (task === "2") processes.push(asyncProcess2); if (task === "3&q ...

Mysterious line break emerges upon displaying JavaScript through PHP

I have a situation where I am using PHP to output JavaScript for a WordPress shortcode. This is what my PHP code looks like: $output="<script type='text/javascript' > jQuery(document).ready(function() { jQuery('#photo{$photo_id}&a ...

React Select streamlines dropdown options for multi-selection by abbreviating names

Is there a way to shorten dropdown names when selected similar to the example shown in the image below https://i.sstatic.net/qUFP6.png This is the snippet of my code : multiValue: [ { value: "BUF", label: "BUF" }, ...

Tips for wrapping a function call that may occasionally involve asynchronous behavior to ensure it runs synchronously

I am dealing with a function that functions as follows: _setDataChunk: function (action) { var self = this; /* some code */ var data = self._getDataChunk(action); populateWidget(data); } Sometimes GetDataChunk cont ...

Synchronize JSON data with the Document Object Model (DOM

My current project is built using React, where I am rendering the page dynamically based on JSON data. The page consists of various component types, ranging from images to text content. Each component includes a delete option, allowing users to change im ...

Avoid Sequelize automatically appending 'Id' to column names caused by association configurations

When using Sequelize to query data, I've noticed that 'Id' is automatically added to the end of my column name. How can I prevent this from happening? Below is an example of the entity data Model for Sequelize that I have created: function ...

View additional products

I am currently developing an application that displays a variety of locations. My goal is to only show 10 places at a time when the user clicks on the "show more" button. Right now, I can display the initial 10 items successfully, but upon clicking for th ...

Exploring the concept of jQuery handler functions

Within my code, I have the following: <script type="text/javascript"> $(document).ready(function (e) { $('#EventCreate').click(function (e) { location.href = '@Url.Action("Create", "AEvents")'; }); ...

The dropdown function in Bootstrap seems to be malfunctioning

I have implemented a basic dropdown menu using the code below: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" ...

The function window.open when using the target '_blank' will launch a fresh browser window

I'm attempting to open a link in a new browser tab (not a new window). When I place a link on the page like this: <a href="#" onclick="window.open('http://www.google.com', '_blank');"> Click Here</a> when a user clic ...

Allowing Cross-Origin Resource Sharing on an Express Server hosted using Firebase Cloud Functions

Hey there, I have a simple Express setup on Firebase, shown below: import { onRequest } from 'firebase-functions/v2/https'; import express from 'express'; import bodyParser from 'body-parser'; import router from './router ...

Animate the CSS when the content is within the viewport using pagepiling.js integration

I'm currently working on animating content as it enters the viewport. However, I've encountered an issue where jQuery (used to check if the content is in the viewport) isn't functioning properly alongside pagepiling.js (). I suspect this mig ...

Tips for effectively utilizing Vuelidate to display errors selectively after the user has completed input:

I have implemented a form using Bootstrap-Vue with some Vuelidation code applied to it. <b-form @submit.prevent="onSubmit"> <input type="hidden" name="_token" :value="csrf" /> <transition-group name="fade"> <b-form ...