When viewing a mobile page at the top, the div is centered vertically. However, as we scroll down the page, an error occurs

I recently started developing a Cordova app and encountered an issue. Whenever I click on a specific div, its components are supposed to be displayed in a popup located at the center of the screen. This is the relevant code snippet:

        var new_height = $('#' + id).height() + 20;
        var new_width = $('#' + id).width() * 0.97;

        $('#' + id + '_popup').width(new_width);

        var move_up = $('#' + id + '_popup').height() / 2;

        $('#' + id + '_popup').css({
            "overflow-y": 'auto',
            'transform': 'translateY(-' + $('#' + id + '_popup').height() / 2 + 'px)'
        });

        $('#' + id + '_popup').css("z-index", '3000');

However, I noticed that when I scroll down the page, the new div appears above the center position instead of staying centered. Can anyone suggest a solution for this issue?

Answer №1

To perfectly center an element, utilize CSS instead of unnecessary calculations. Use the transform property in this manner:

.red {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}
.green {
  position: absolute;
  height: 40%;
  width: 40%;
  background: green;
  top: 50%;
  left:50%;
  -webkit-transform: translate(-50%, -50%); /*safari iOS*/
  transform: translate(-50%, -50%);
}
<div class="red">
  <div class="green">
  </div>
</div>

Regardless of the box sizes, this code will always center the elements on the screen.

You can switch the position to fixed or relative, and it will still function correctly.

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 the conversion of rgba to hsla happening in Scss?

Currently, I am utilizing web pack for compiling my scss files into css and Bootstrap 4 is being used in my project. An unusual occurrence caught my attention, within my scss file, the following property is defined: color: rgba(255, 255, 255, 0.9); Once ...

How to retrieve and modify JSON data in Node.js using Stream with both Get and Post methods

Exploring Node.js for the first time, I'm currently tackling a project where I aim to utilize Request for streaming data from one endpoint to another. The objective is to leverage Request for extracting and posting an altered JSON body through a pipe ...

What could be the reason for my regex succeeding on the client side but failing on the server side

I have implemented a regex pattern to validate usernames, ensuring they only contain English letters, numbers, and underscores. The client-side code works perfectly, preventing any input other than these characters: <input type="text" name ...

What is the secret behind Node.js's ability to efficiently manage multiple requests using just one thread

After conducting some research on the topic, I noticed that most people tend to focus solely on Non-blocking IO. For instance, if we consider a basic application that simply responds with "Hello World" text to the client, there will still be some executio ...

The customary scroll bar located to the right side of the screen

I'm struggling to create a common scrollbar for both the left navigation div and right content div on a simple page, but all my attempts have failed. No matter what I try, the scrollbar only works for the content div. Is there any way to make them sha ...

Guide to importing firebase-functions and firebase-admin using ES6 syntax for transpilation with Babel in Node 10

I've been working on my cloud functions in ES6 and using Babel to transpile them for the Node v10 environment. But, I've come across an odd issue. It seems that when I import firebase-functions like this: import functions from 'firebase-fu ...

Issue with Firefox: dblclick() function not supported by jQuery!

Over the past few months, I have been working on developing a web application using JS, jQuery, and PHP. I implemented various tricks and scripts that I found here (thank you) and tested everything in Chrome. Everything was running smoothly until I decided ...

Issue encountered during JSON file extraction (Firefox)

Within my Node.js application, there is a specific route where I retrieve a large JSON dataset from a Postgres database and then send it in a compressed format as a response. To accomplish this, I utilize the Zlib module to compress the data using gzip. Be ...

Creating borders around Material UI grid items can be achieved by applying a border style to the

import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Paper from '@material-ui/core/Paper'; import Grid from '@material-ui/core/Grid'; const useStyles = makeStyles((theme) => ({ ...

Determine the number of unread messages in Office365 using SharePoint Online

Currently working on a project with the goal of enhancing the visual appeal of a SharePoint Online site, I've hit a roadblock. Users are requesting to see the number of unread messages in their Office365 inbox on the home page's left banner. To ...

Use two HTML forms, each one only submitting its own form for validation

Managing two forms on a single page can be tricky. One form is specific to the current page, while the other is a side form that appears on every page. I've successfully implemented a validator to highlight incomplete fields with red boxes for the fir ...

It seems that Ionic 2 does not support the registration of custom HTML tags

Encountering a problem with Ionic 2 and custom components. Developed a component to show in a list, serving as the list item. However, my app crashes when attempting to use the custom HTML tag. The stack trace is provided below. Uncertain about the issue. ...

Implement a unique feature for specific days using jQuery UI Datepicker with a customized class

Looking to highlight a range of days horizontally in jQuery UI Datepicker with the multiselect plugin. To achieve this, I am utilizing the :before and :after pseudoelements of the a tags. .ui-state-highlight a:before, .ui-state-highlight a:after { con ...

"Trouble with React JS redirection feature failing to redirect as intended

Could someone please help me troubleshoot why the redirect function is not working in my code? There are no error messages showing up, but it seems like the Redirect call from the react-router-dom library is causing a problem. The console log displays &apo ...

Obtain an Instance of a Class Using a Decorator

Delving deep into the world of decorators, I stumbled upon some fascinating ideas for incorporating them into my reflux implementation. My concept involves tagging a store's class method with an action, so that whenever that action is triggered, it au ...

guide on updating JQuery string with JavaScript to incorporate new parameters

Similar Question: How to replace only one parameter or fast with Jquery on Jquery String The website has a query string as follows: http://www.nonloso.html/?nome1=pollo&cognome1=chicken&nome2=a&cognome2=b&nome3=c&cognome3=d This ...

What is the procedure for adding a JavaScript function to an HTML element that was exclusively created in JavaScript?

I am trying to dynamically change the background color of a row in a table when selecting an object from a dropdown list, but only if the selected number is even. The challenge I am facing is that the objects are created using JavaScript and not directly i ...

Is there a way to extract the query string from a file in order to query the database using ExpressJS?

I am having trouble with this code snippet as it doesn't seem to be working properly. var content = fs.readFileSync('/home/diegonode/Desktop/ExpressCart-master/views/partials2/menu8xz.hbs', 'utf8' ); req.db.products.find( co ...

Having trouble with individuals gaining access to my gh-pages on Github

After creating a gh-pages repository on Github to showcase my portfolio, I encountered an issue where visitors were being prompted to log into Github in order to view the page. Gh-pages are intended to be public, so I am unsure why this is occurring. My ...

Trigger jQuery event on a table cell

I am looking to select the td element using jQuery <table width="60" height="60" cellpadding="0" cellspacing="0" border="0" style="float: left; margin: 1px" background="images/data/source_red.gif"> <tbody> <tr> <td ...