简体   繁体   English

浏览器调整大小时的 JavaScript 重新加载到不同的页面

[英]JavaScript when brower resize reload to different page

我最近制作了一个网站来玩得开心。我有两个显示器,一个 15 英寸,一个 24 英寸。我在网站上遇到了问题,在 15 英寸显示器上时,它在登录页面上看起来很奇怪。我想我会做一些 javascript,所以当它检测到 15"英寸以下的东西时,它会重新加载页面并转到移动版本。它可以工作,但有人可以帮助我在调整大小时自动重新加载到移动页面吗?我会很好。请不要判断,因为我对 JS 一无所知,但这就是我想出来的。如果有人能帮助我,我会很高兴的。

if (screen.width <= 1440) { reload(); document.location = "mobileindex.html"; }

So what you're looking for is called an EventListener this will trigger on certain event such as resizing of the browser window.因此,您要查找的内容称为EventListener,它将在某些事件上触发,例如调整浏览器窗口的大小。
Also you may want to look at https://www.w3schools.com/jsref/ as it is a really good resource for beginners.此外,您可能还想查看https://www.w3schools.com/jsref/,因为它对初学者来说是一个非常好的资源。
As for the code to do this :至于执行此操作的代码:

window.addEventListener("resize", function(){
    var width  = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    if (width <= 1440) window.location.replace("example.html"); 
});

[EDIT] [编辑]

So working out screen size in Inches is a little more complex, the standard way is to use pixels but if you are set on using In here is a resource that you can use.因此,以英寸为单位计算屏幕尺寸稍微复杂一些,标准方法是使用像素,但如果您设置为使用 In, 这里有一个您可以使用的资源
So it would be about picking usually 3 breaking points for you code, 3 screen widths.因此,这通常是为您的代码选择 3 个断点,3 个屏幕宽度。
Popular resolutions are 360x640 ( mobile ), 768x1024 ( tablet ) and 1366x768 ( desktop ).流行的分辨率是 360x640( 移动)、768x1024( 平板)和 1366x768( 桌面)。
So you would the end up with something like this:所以你最终会得到这样的结果:

window.addEventListener("resize", function(){
    var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var currentPage = window.location.origin; 
    if (width <= 420 && !currentPage.includes("mobile")) window.location.replace("mobile.example.html"); 
    else if(width >= 1280 && !currentPage.includes("desktop")) window.location.replace("desktop.example.html"); 
    else if(!currentPage.includes("tablet")) window.location.replace("tablet.example.html"); 
});

The currentPage.includes("tablet") is to stop a redirect loop once you're on the correct page. currentPage.includes("tablet")用于在您进入正确页面后停止重定向循环。

Can you not reload to a different page?你不能重新加载到不同的页面吗? Cause you said you want to switch to the mobile version... If my understandings are correct, the mobile version and the desktop version should have the same content, just different CSS styling.因为你说你要切换到移动版...如果我的理解是正确的,移动版和桌面版应该有相同的内容,只是CSS样式不同。 If that is so, you don't even have to use javascript.如果是这样,您甚至不必使用 javascript。

//css file

//this is just an example
.body{
    margin: 10px;
    font-size: 100px;
}

//when the page is resized to 15inch or below, the bottom code will run
@media(max-width: 1440px //15inch){
    .body{
        margin: 0px;
        font-size: 50px;
    }
}

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

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