Recently, I've been playing around with jQuery and created a popup menu of sorts. The positioning of the popup div is calculated when it's shown or when the window is resized.
I've noticed that the positioning doesn't work correctly on mobile devices; the popup div appears too far to the right.
Why does this happen? Why is the popup div displayed differently on mobile devices compared to desktop?
Here's an example that works well on a desktop and two images showing how things go awry on a mobile device:
https://i.sstatic.net/zHylB.png https://i.sstatic.net/4MaXl.png
$(document).ready(function () {
$(".button").mouseover(function () {
showPopup();
});
var timeout;
$(".popup").mouseleave(function () {
timeout = setTimeout(function () {hidePopup(500);}, 500);
});
$(".popup").mouseenter(function () {
clearTimeout(timeout);
});
$(".button").click(function () {
togglePopup();
});
$(".closebutton").click(function () {
hidePopup(0);
});
$(window).resize(function () {
positionPopup();
});
});
function positionPopup() {
var pos = $(".button").offset();
var h = $(".button").height();
var w = $(".button").width();
var widthPopUp = $(".popup").width();
var heightPopUp = $(".popup").height();
$(".popup").css({left: pos.left - widthPopUp - 20 - 3 + w, top: pos.top + h + 10});
}
function showPopup() {
positionPopup();
$(".popup").fadeIn(300);
}
function hidePopup(delay) {
if (typeof(delay) === 'undefined') {
delay = 1000;
}
$(".popup").fadeOut(delay);
}
function togglePopup() {
positionPopup();
$(".popup").toggle();
}
* {
box-sizing: border-box;
}
html, body {
margin: 0px;
padding: 0px;
}
.container {
width: 500px;
border: 1px solid black;
margin: 50px auto;
height: auto;
position: relative;
border-radius: 2px;
padding: 10px;
padding-top: 25px;
}
.buttonholder {
height: 20px;
width: 20px;
text-align: center;
line-height: 15px;
right: 0px;
top: 0px;
position: absolute;
margin: 3px;
display: block;
}
.button {
cursor: pointer;
}
.popup{
border: #DDDDDD 1px solid;
width: 200px;
height: 40px;
display: none;
position: absolute;
background-color: white;
padding: 10px;
border-radius: 2px;
}
.closebtnholder {
position: absolute;
top: 0px;
right: 0px;
margin: 3px;
text-align: center;
}
.closebutton {
cursor: pointer;
font-size: 10pt;
}
.popup::after {
border-bottom: 8px solid #DDDDDD;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
width: 0;
height: 0;
content: "";
display: block;
position: absolute;
top: -8px;
left: 184px;
}
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="layout.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="buttonholder">
<div class="button">[?]</div>
</div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the
industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five centuries but also the leap
into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the
release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<div class="popup">
<div class="closebtnholder">
<div class="closebutton">[x]</div>
</div>
<a href="http://google.be">google</a>
</div>
<div class="triangle">
</div>
</body>
</html>