Open a popup window in the center of the monitor - javascript

I am trying to open a popup window in the center of the monitor. As I know screen size of each desktop or laptop will vary. So that is the reason I am looking for some way so that whenever my code tries to open a popup window it should get opened in the center of the browser.
Below is my code-
<html>
<head>
<style>
* { font-family: Trebuchet MS; }
#containerdiv {width:90%; height: 90%; display: none; position: fixed;margin-top: 5%; margin-left: 5%; background:#FFF; border: 1px solid #666;border: 1px solid #555;box-shadow: 2px 2px 40px #222; z-index: 999999;}
/*#containerdiv iframe {display:none; width: 100%; height: 100%; position: absolute; border: none; }*/
#blockdiv {background: #000; opacity:0.6; position: fixed; width: 100%; height: 100%; top:0; left:0; display:none;}
ul { padding:10px; background: #EEE; position: absolute; height: 200px; overflow: scroll;}
ul li {color: #222; padding: 10px; font-size: 22px; border-bottom: 1px solid #CCC; }
h3 { font-size: 26px; padding:18px; border-bottom: 1px solid #CCC; }
#close { top: 13px;position: absolute;right: 13px; padding: 10px; background: #EEE; border: 1px solid #CCC;}
#close:hover { cursor: pointer; background: #E5E5E5 }
#apply { top: 13px;position: absolute;left: 13px; padding: 10px; background: #EEE; border: 1px solid #CCC;}
#apply:hover { cursor: pointer; background: #E5E5E5 }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
function open(url) {
$('#blockdiv').fadeIn();
$('#iframe').attr('src', url);
$('#containerdiv').fadeIn();
}
function close() {
$('#blockdiv').fadeOut();
$('#containerdiv').fadeOut();
}
$(document).ready(function() {
$('ul').css({width: $('#containerdiv').width()-20,height: $('#containerdiv').height()-90})
$('#close').click( function() { close() })
$('#apply').click( function() { open('http://www.google.com/') })
});
</script>
</head>
<body>
<span id="apply">ApplyOnline</span>
<div id="blockdiv"></div>
<div id="containerdiv">
<iframe id="iframe" style="width:100%; height: 100%; outline: 1px solid red;"></iframe>
<span id="close">Close</span>
</div>
</body>
</html>
But somehow the above code, doesn't gets opened in the center of the monitor. Can anybody help me on this? What changes I need to make in the above code so that it always open the popup window in the center of the circle.

you can center you popup in css like
this is only css method to make it center so no js will be required to center it
#containerdiv {
width:90%;
height: 90%;
display: none;
position: fixed;
left:50%;
top:50%;
margin:-45% 0 0 -45%;
background:#FFF;
border: 1px solid #666;
border: 1px solid #555;
box-shadow: 2px 2px 40px #222;
z-index: 999999;
box-sizing:border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
}

It appears centered within the browser window to me.
If you want to center it based on screen resolution of the client's monitor you can use:
var centerDiv=function(){
var popUpHeight=$('#containerdiv').height();
var popUpWidth=$('#containerdiv').width();
var yPos=(window.screen.availHeight/2)-(popUpHeight/2)-window.screenY-(window.outerHeight-window.innerHeight);
var xPos=(window.screen.availWidth/2)-(popUpWidth/2)-window.screenX-(window.outerWidth-window.innerWidth);
$('#containerdiv').css({position:"absolute",top: yPos+"px",left: xPos+"px"});
}
window.onload=centerDiv();
var lastX = window.screenX;
var lastY = window.screenY;
var lastHeight = window.innerHeight;
var lastWidth = window.innerWidth;
setInterval(function(){
if(lastX != window.screenX || lastY != window.screenY || lastHeight != window.outerHeight || lastWidth != window.outerWidth){
centerDiv();
}
oldX = window.screenX;
oldY = window.screenY;
}, 33);

Related

HTML, how do I know the height of a "textbox"?

I have a piece of html like this:
<!doctype html>
<html>
<head>
<style type="text/css">
body {
margin: 0px;
width: 250px;
border: 1px dashed gray;
}
.mypara {
font-family: "Segeo UI";
margin: 0px;
font-size: 24px;
line-height: 120px; /* same as line-height:5 */
}
.myspan {
border: 6px solid #8f8;
}
.alignmark {
position: fixed;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
left: 0px;
width: 250px;
top: 48px;
height: 24px;
}
</style>
</head>
<body>
<p class="mypara"><span class="myspan">My paragraph .</span></p>
<div class="alignmark"></div>
</body>
</html>
How do I know the exact height(pixel count) of a textbox of a text line, either with JavaScript or by Chrome/Firefox DevTools(F12)?
The textbox height is the height of the area surrounded by a <span> element's border (the "border" in HTML box model).
From my experiments, the difference between textbox height and font-size value varies according to font-family I choose, some may be 5~6 pixels, some maybe 7~8 pixels.
I would use the .getBoundingClientRect().height and take away the top and bottom border size (as it's hard coded at 6 each just take away 12 - or you can use getComputedStyle and get the computed values if you wish, as below)
let e = document.querySelector('.myspan');
let css = window.getComputedStyle(e);
console.log(
e.getBoundingClientRect().height
- parseFloat(css.borderTopWidth)
- parseFloat(css.borderBottomWidth)
);
<!doctype html>
<html>
<head>
<style type="text/css">
body {
margin: 0px;
width: 250px;
border: 1px dashed gray;
}
.mypara {
font-family: "Segeo UI";
margin: 0px;
font-size: 24px;
line-height: 120px; /* same as line-height:5 */
}
.myspan {
border: 6px solid #8f8;
}
.alignmark {
position: fixed;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
left: 0px;
width: 250px;
top: 48px;
height: 24px;
}
</style>
</head>
<body>
<p class="mypara"><span class="myspan">My paragraph .</span></p>
<div class="alignmark"></div>
</body>
</html>
Use Window.getComputedStyle() and access fontSize property of that.
let p = document.querySelector('.mypara')
let height = window.getComputedStyle(p).fontSize
console.log(height)
<!doctype html>
<html>
<head>
<style type="text/css">
body {
margin: 0px;
width: 250px;
border: 1px dashed gray;
}
.mypara {
font-family: "Segeo UI";
margin: 0px;
font-size: 24px;
line-height: 120px; /* same as line-height:5 */
}
.myspan {
border: 6px solid #8f8;
}
.alignmark {
position: fixed;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
left: 0px;
width: 250px;
top: 48px;
height: 24px;
}
</style>
</head>
<body>
<p class="mypara"><span class="myspan">My paragraph .</span></p>
<div class="alignmark"></div>
</body>
</html>
you can also use clientHeight
let alignmark = document.getElementsByClassName('alignmark')[0];
console.log(alignmark.clientHeight);
or open Chrome Developer Tools, Select your Element via 'Elements' tab, and click the 'Properties'. You can check various DOM properties as well.
This works if i have understand correctly what do you want to do.
<script>
window.onload = function(){
console.log("Element Height:"+document.getElementById('myspan').offsetHeight);
}
</script>

Horizontal Div Scroll

I'm quite new to Jquery, I'm trying to implement horizontal scrolling on a div using the scrollLeft property, I'm not getting any error messages and nothing seems to be working, could someone explain why? Thanks in advance
this is my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js">
$(document).ready (function(){
var pos = $("#container").scrollLeft();
$(".prev").click(function(){
$("#container").animate({
scrollLeft: pos - 200;
});
});
});
</script>
<style>
#container{
max-width: 938px;
height: 500px;
border: 2px solid black;
margin-left: auto;
margin-right: auto;
overflow: scroll;
/*display: table;*/
}
.picsdiv{
display: table-cell;
min-width: 286px;
padding-right: 80px;
height: 508px;
border: 2px solid pink;
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 250px;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
border: 2px solid pink;
}
.next {
right: 170px;
border-radius: 3px 0 0 3px;
}
.prev{
left: 170px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
</style>
<body>
<div id="container">
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
</body>
take out the semi-colon on this line and it should work
scrollLeft: pos - 200;
firstly you must close script tag that include jquery file and also open another script tag for your jquery code
I just added a seperate script tag for the Jquery code and used the first one to only refer to the library and it now works. Thanks for all your help
You have done 2 mistakes in your code:
scrollLeft: pos - 200; should be like scrollLeft: (pos - 200), because you are passing an object as parameter
Write two <script> tags, one for jQuery, like below
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js" type="text/javacsript"></script>
And another for your custom java-script
<script type="text/javascript">
$(document).ready (function(){
var pos = $("#container").scrollLeft();
$(".prev").click(function(){
$("#container").animate({
scrollLeft: (pos - 200)
});
});
});
Because you can not write code inside element if you are using ths element as external reference.
Here is your complete running code
<html>
<head>
<style>
#container{
max-width: 938px;
height: 500px;
border: 2px solid black;
margin-left: auto;
margin-right: auto;
overflow: scroll;
/*display: table;*/
}
.picsdiv{
display: table-cell;
min-width: 286px;
padding-right: 80px;
height: 508px;
border: 2px solid pink;
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 250px;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
border: 2px solid pink;
}
.next {
right: 170px;
border-radius: 3px 0 0 3px;
}
.prev{
left: 170px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
</style>
</head>
<body>
<div id="container">
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<div class="picsdiv">
</div>
<a class="prev"><</a>
<a class="next">></a>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready (function(){
var pos = $("#container").scrollLeft();
$(".prev").click(function(){
$("#container").animate({
scrollLeft: (pos - 200)
});
});
});
</script>
</html>

iframe gets displaced on iOS when dragged

I have an iframe on my website that is populated when you click the Make an Appointment button. The iframe contains a contact form with several input fields.
Everything works fine, but for some reason on iOS (iPad and iPhone 5/6 - I tested Safari and Chrome) the form is draggable beyond its container's width and height. It should only be scrollable in the y-axis; like it is on Android devices. See screenshot below.
I've looked at numerous posts on S/O, but have yet to find any Q/A that pertains to this particular nuance of iOS devices/browsers.
Here is the code:
HTML:
<div id='button'><button id='contact'>MAKE AN APPOINTMENT</button></div>
<div id="block"></div>
<div id="iframecontainer">
<a id='close' href='#'>X</a>
<div id="loader"></div>
<iframe></iframe>
</div>
JQuery:
$('document').ready(function() {
$('#contact').click(function () {
$('#block').fadeIn();
$('#iframecontainer').fadeIn();
$('#header-wrapper').css("visibility", "hidden");
var width = $(window).width();
$('#iframecontainer iframe').attr('src', 'http://a-link-to-my-iframe.html');
if (width > 850) {
$('#iframecontainer').css('width', '790px');
$('#iframecontainer').css('margin-left', '-395px');
}
else {
$('#iframecontainer').css('width', '310px');
$('#iframecontainer').css('margin-left', '-155px');
}
$('#iframecontainer iframe').load(function() {
$('#loader').fadeOut(function() {
$('iframe').fadeIn();
});
});
});
And, the CSS:
#contact {
color: #c2c2c2;
background: #151515;
border: 1px solid #c2c2c2;
padding: 13px 26px;
text-decoration: underline;
font-family: inherit;
letter-spacing: 2px;
font-size: 18px;
margin: 0 auto;
}
#iframecontainer {
width:75%;
height: auto;
display: none;
position: fixed;
-webkit-overflow-scrolling:touch;
overflow-y: auto;
height:600px;
top: 10%;
background:#FFF;
border: 1px solid #666;
border: 1px solid #555;
box-shadow: 2px 2px 40px #222;
z-index: 999999;
left: 50%;
margin-left: -395px;
}
#iframecontainer iframe {
width: 100%;
height: 600px;
position: absolute;
border: none;
}
#loader {
width: 250px;
height: 250px;
margin:auto;
}
#block {
background: #000;
opacity:0.6;
position: fixed;
width: 100%;
height: 100%;
top:0;
left:0;
display:none;
}​
And here is a screen shot of what I am referring to:
Is there a specific way to disable this on iOS devices?
Try to add scrolling="no" on the iframe and change some iframe CSS like,
#iframecontainer iframe {
width: 1px;/* Make it a very small for your viewport */
min-width:100%;/* Overwrite width. Let it decides the actual width of iframe */
height: 600px;
position: absolute;
border: none;
}

js to determine responsive element width

I am new to js and have hit a brick wall with altering how I calculate an element width depending on the screen size. I started with only calculating the width by how many of the list items were set to "plan-visible". It works great on a laptop screen, but doesn't break at css media query widths when the screen is smaller. I then had the brilliant idea (haha) of attempting to this directly in the javascript with an if/else statement, which I have little experience with. The result was that it did absolutely nothing. I researched and researched and it looks (to me) like it should work, but I am obviously missing something. I have included the html, the original js that worked for a static element, and then my failed attempt at making it responsive. Thanks!
html:
<div id="product-table" class="clear">
<div class="plan-visible">
<h3>BALANCE<em>aap</em><img class="image aap" src="http://localhost:1400/images/baap.png"></h3>
<a class="sbutton aap" href="">Go to AAP</a>
<ul>
<li>Point and click your way through the preparation of your organization’s Affirmative Action plan.<br> </li>
</ul>
</div>
<div class="plan-visible">
<h3>BALANCE<em>trak</em><img class="image trak" src="http://localhost:1400/images/btrak.png"></h3>
<a class="sbutton trak" href="">Learn More</a>
<ul>
<li>Manage your organization’s hiring process from start to finish.<br> <br> </li>
</ul>
</div>
<div class="plan-visible">
<h3>BALANCE<em>hub</em><img class="image hub" src="http://localhost:1400/images/bhub.png"></h3>
<a class="sbutton hub" href="">Go to HUB</a>
<ul>
<li>Access a centralized compliance information center to view, publish, and share information from your BALANCEworks applications.</li>
</ul>
</div>
<div class="plan-visible">
<h3>BALANCE<em>pay</em><img class="image pay" src="http://localhost:1400/images/bpay.png"></h3>
<a class="sbutton" href="">Go to PAY</a>
<ul>
<li>Conduct detailed compensation analyses, identify potential pay discrimination, and design a compliant pay structure</li>
</ul>
</div>
</div>
original js:
$(document).ready(function() {
var width = 0;
$('.plan-visible').each(function() {
width += $(this).outerWidth( true );
});
$('#product-table').css('width', width);
});
new, broken js:
$(document).ready(function() {
var mq = window.matchMedia( "(max-width: 500px)" );
if (mq.matches) {
$('#product-table').css('446', width);
} else {
var width = 0;
$('.plan-visible').each(function() {
width += $(this).outerWidth( true );
});
$('#product-table').css('width', width);
}
});
css:
#product-table {
margin: 100px auto;
text-align: center;
.plan-visible {
font: 12px 'Lucida Sans', 'trebuchet MS', Arial, Helvetica;
text-shadow: 0 1px rgba(255,255,255,.8);
background: #fff;
border: 1px solid #ddd;
color: #333;
padding: 20px;
float: left;
position: relative;
-moz-border-radius: 5px ;
-webkit-border-radius: 5px;
border-radius: 5px ; }
.plan-visible:nth-child(1) {width: 180px;}
.plan-visible:nth-child(2) {width: 180px;}
.plan-visible:nth-child(3) {width: 180px;}
.plan-visible:nth-child(4) {width: 180px;}
.plan-hidden {display: none;}
}
/* --------------- */
#product-table h3 {
font-size: 20px;
color: $mainBlue;
font-weight: normal;
padding: 20px;
margin: -20px -20px 50px -20px;
background-color: $lightGrey;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
/* product image settings */
#product-table .plan-visible .image {
display: block;
height: 100px;
width: 100px;
margin: 15px auto -65px;
-moz-border-radius: 100px;
-webkit-border-radius: 100px;
border-radius: 100px;
}
#product-table .plan-visible .image.aap {
border: 3px solid $aap;
}
#product-table .plan-visible .image.trak {
border: 3px solid $trak;
}
#product-table .plan-visible .image.hub {
border: 3px solid $hub;
}
#product-table .plan-visible .image.pay {
border: 3px solid $pay;
}
/* --------------- */
#product-table ul {
margin: 20px 0 0 0;
padding: 0;
list-style: none;
}
#product-table li {
border-top: 1px solid $lightGrey;
padding: 20px 0 10px 0;
}
/* --------------- */
/* product table specific buttons */
.sbutton {
position: relative;
font-family: 'Roboto', Arial, sans-serif;
padding: 8px 20px;
margin: 20px 0 0 0;
display: inline-block;
background-color: $mainBlue;
color: white;
border-radius: 4px;
border: 1px solid $mainBlue;
text-align: center;
text-decoration: none;
font-size: 14px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}
.sbutton:hover {
background-color: #94ae3c;
color: #fff;
border: 1px solid #94ae3c;
text-decoration: none;
}
.sbutton.aap:hover {
background-color: #6f2784;
border: 1px solid #6f2784;
}
.sbutton.trak:hover {
background-color: #5c89b4;
border: 1px solid #5c89b4;
}
.sbutton.hub:hover {
background-color: #b58f2e;
border: 1px solid #b58f2e;
}
.sbutton.pay:hover {
background-color: #94ae3c;
border: 1px solid #94ae3c;
}

Flickering Lines when moving div from Javascript; pixel perfect CSS?

Please take a look at this JSFiddle:
https://jsfiddle.net/a08vkmew/light/
I created a compass with CSS/Html/Javascript that reacts to horizontal mouse movement on the page.
If you move the mouse slowly you will see that the lines change their width slightly which results in a flickering appearance of the compass.
I think this effect occurs when a line does not exactly match up with the according pixels on the screen, so that only half the width of the line can be shown.
In some GUI frameworks we can choose to display the GUI as pixel perfect. Is something like this possible within CSS?
HTML
<div id="compass-container">
<div class="arrow down"></div>
<div class="arrow up"></div>
<div id="viewport">
<div id="compass-scale">
</div>
</div>
</div>
CSS
div#compass-container {
position:relative;
height: 6em;
}
div#viewport{
position:relative;
height:40%;
width:50%;
left:50%;
top:1.2em;
margin-left:-25%;
overflow: hidden;
}
div#compass-scale {
position:relative;
width: auto;
height: 100%;
}
.mini-container {
width:1em;
height:95%;
top:0em;
border: 0px solid black;
float:left;
}
.line {
position: relative;
left:45%;
width:.1em;
background-color:black;
}
.line.small {
height: 15%;
}
.line.medium {
height: 30%;
}
.line.big {
height: 45%;
}
.compass-text {
position:relative;
height:100%;
width:100%;
margin-top:.4em;
text-align: center;
font-family: sans-serif;
color:dodgerblue;
}
.compass-text.small {
font-size: .6em;
}
.compass-text.big {
font-size: .8em;
}
.arrow {
position:absolute;
width: 0;
height: 0;
left:50%;
}
.arrow.up {
margin-left:-1em;
border-left: 1em solid transparent;
border-right: 1em solid transparent;
border-bottom: 2em solid dodgerblue;
bottom: 0em;
}
.arrow.down {
margin-left:-0.5em;
border-left: 0.5em solid transparent;
border-right: 0.5em solid transparent;
border-top: 1em solid dodgerblue;
top: 0em;
}

Categories