Reputation: 536
For starters... I have no sinister intention of subjecting users to popups or anything like that. I simply want to prevent a user from resizing the browser window of a webpage to which they've already navigated (meaning I don't have access to / don't want to use window.open();). I've been researching this for quite a while and can't seem to find a straightforward answer.
I felt like I was on track with something along the lines of:
$(window).resize(function() {
var wWidth = window.width,
wHeight = window.height;
window.resizeBy(wWidth, wHeight);
});
...to no avail. I have to imagine this is possible. Is it? If so, I would definitely appreciate the help.
Thanks
Upvotes: 15
Views: 68248
Reputation: 1
After struggling for about three hours trying to fix this exact problem, I came up with this hack:
let dims = [];
function getDims ()
{
return [
window.visualViewport?.width ||
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth,
window.visualViewport?.height ||
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight
];
}
function fixViewport (e)
{
const newdims = getDims();
if (newdims[0] != dims[0])
{
dims = newdims;
main.height = dims[1];
main.style.height = dims[1] + "px";
}
}
window.addEventListener("resize", fixViewport);
fixViewport();
body, main
{
position: fixed;
width: 100vw;
}
main { overflow-y: auto; }
<head>
<meta id=viewportMeta name=viewport content="height=device-height, width=device-width, initial-scale=1.0">
</head>
<body>
<main id=main>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at vehicula lectus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius leo sit amet felis egestas eleifend. Nulla efficitur magna libero, vitae facilisis libero consequat a. Praesent a justo sit amet mi viverra pulvinar. Pellentesque vehicula viverra nisl ut imperdiet. Sed at dignissim dolor. Maecenas sollicitudin purus nibh, non convallis mi mollis quis. Nulla facilisi. Nullam semper nisl nec eros ornare fermentum. Nunc nec tortor in turpis semper iaculis.</p>
</main>
</body>
A few disclaimers:
Upvotes: 0
Reputation: 2160
It can be done with preventDefault
document.addEventListener("wheel", (event) =>
{
if (event.ctrlKey)
event.preventDefault();
});
It can be done for some particular element, but keep everywhere else:
document.getElementById("my_element").addEventListener("wheel", (event) =>
{
if (event.ctrlKey)
event.preventDefault();
});
Upvotes: 0
Reputation: 19561
I needed to do this today (for a panel opened by a chrome extension) but I needed to allow the user to change the window height, but prevent them changing the window width
@Derek's solution got me almost there but I had to tweak it to allow height changes and because of that, an endless resizing loop was possible so I needed to prevent that as well. This is my version of Dereck's answer that is working quite well for me:
var couponWindow = {
width: $(window).width(),
height: $(window).height(),
resizing: false
};
var $w=$(window);
$w.resize(function() {
if ($w.width() != couponWindow.width && !couponWindow.resizing) {
couponWindow.resizing = true;
window.resizeTo(couponWindow.width, $w.height());
}
couponWindow.resizing = false;
});
Upvotes: 2
Reputation: 94299
You can first determine a definite size.
var size = [window.width,window.height]; //public variable
Then do this:
$(window).resize(function(){
window.resizeTo(size[0],size[1]);
});
Demo: http://jsfiddle.net/DerekL/xeway917/
Q: Won't this cause an infinite loop of resizing? - user1147171
Nice question. This will not cause an infinite loop of resizing. The W3C specification states that resize
event must be dispatched only when a document view has been resized. When the resizeTo
function try to execute the second time, the window will have the exact same dimension as it just set, and thus the browser will not fire the resize event because the dimensions have not been changed.
Upvotes: 27