简体   繁体   English

如何使用内部宽度按比例缩放我的网页以适应任何显示尺寸?

[英]How can I scale my webpage proportionally using inner width to fit any display size?

I want to do the scaling depending on only width.我想仅根据宽度进行缩放。 I don't wanna mess with height.我不想搞砸身高。 I have a website which unfortunately I made it for a way bigger display size.我有一个网站,不幸的是我做了一个更大的显示尺寸。 I didn't know how the adobe xd artboard size was too big.我不知道 adobe xd 画板尺寸太大了。 I thought it was 1920px but actually the width of my website is 3554px.我以为是 1920 像素,但实际上我网站的宽度是 3554 像素。 I want to scale it down to fit smaller screens and scale it up for bigger screens.我想缩小它以适应较小的屏幕并扩大它以适应更大的屏幕。 I can only make it fit in my laptop screen but I don't know the calculation behind for how to make it work for any screen.我只能让它适合我的笔记本电脑屏幕,但我不知道如何让它适用于任何屏幕的计算。 Basically I want scale to fit width and center the page horizontally.基本上我希望缩放以适应宽度并将页面水平居中。 So everything is kinda based on width.所以一切都基于宽度。 The base width is 3554px.基本宽度为 3554 像素。 Please help me on how to do this using plain javascript?请帮助我如何使用普通的 javascript 做到这一点?

Please check this link.请检查此链接。 Want to do something with plain javascript but using only width and keeping it at center position.想用普通的 javascript 做一些事情,但只使用宽度并将其保持在中心 position。 Proportionally scale website to fit browser window 按比例缩放网站以适应浏览器 window

CSS CSS

#Web_1920__1 {
        position: absolute;
        width: 3554px;
        height: auto;
        border: 0px grey solid;
        background-color: rgba(255,255,255,0);
        overflow: visible;
        visibility: visible;
    }

This is the main div that should take the whole inner width.这是应该占据整个内部宽度的主要 div。

JAVASCRIPT JAVASCRIPT

const mypage = window.innerWidth;
document.getElementById("Web_1920__1").style.transform = "scale(0.432)";
document.getElementById("Web_1920__1").style.transformOrigin = "0% 0%";

I can get the inner width but those transform properties only work for my particular screen.我可以获得内部宽度,但这些变换属性仅适用于我的特定屏幕。 How can I get the transform values dynamically to fit the page on any screen and keep it at the canter?如何动态获取转换值以适应任何屏幕上的页面并将其保持在慢跑状态? No need to do anything with the height because there will be scrollbar.不需要对高度做任何事情,因为会有滚动条。

Abhay Srivastav's solution is working fine. Abhay Srivastav 的解决方案运行良好。 A lot of thanks to him.非常感谢他。 The solution is like below.解决方案如下所示。 It's proportional scaling depending on width only.它的比例缩放仅取决于宽度。 Everything will remain horizontally centered.一切都将保持水平居中。 Upscaling downscaling works for screen size.放大缩小适用于屏幕尺寸。 Window resize also works fine. Window 调整大小也可以正常工作。

JAVASCRIPT JAVASCRIPT

document.addEventListener('DOMContentLoaded', updateMe);
window.addEventListener('resize', updateMe);

function updateMe() 
{   
    const mypage = window.innerWidth;
    document.getElementById("Web_1920__1").style.transform = "scale("+mypage/3554+")";
    document.getElementById("Web_1920__1").style.transformOrigin = "0 0";
}

This piece of code can save you from putting additional thousand lines of plugin code.这段代码可以让您免于添加额外的数千行插件代码。 Page becomes lighter, loads faster and significant performance improvements.页面变得更轻、加载更快并且性能显着提高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM