gpt4 book ai didi

javascript - 如何动态检测浏览器视口(viewport)大小的变化?

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:10 24 4
gpt4 key购买 nike

我在缩放方面遇到了问题……自动……动态……自适应……等等……

我在网站上看到过将窗口变小的情况,例如。捕获一个 Angular ,让窗口变小,字体变大,东西四处移动……

我得到了移动部分,我不知道如何动态检测浏览器视口(viewport)......这是一个令人耳目一新的事情还是?......

正如您在这里看到的,我使用的这些 Action 仅在屏幕刷新时起作用,特别是我有平板电脑,如果我横向访问页面,则不适用;如果我旋转它然后刷新,它适用。

<script language="JavaScript" type="text/javascript">
var x = screen.width;
var y = screen.height;
function scale() {
var z = x/y;
if (z<1) {
document.getElementById('header').style.width = "100%";
document.getElementById('menu').style.marginLeft = "0";
document.getElementById('menu').style.width = "20%";
document.getElementById('menu2').style.width = "30%";
document.getElementById('menu3').style.width = "20%";
document.getElementById('menu4').style.width = "20%";
document.getElementById('properties').style.width = "30%";
document.getElementById('properties').style.marginLeft = "35%";
}
}
scale();
</script>

这是Stack Overflow的人提供的关于字体的代码,我想它可能和我要去的方向相似

$( window ).resize(function() {
$( "#container" ).css("font-size",function() {
var value = $( "#container" ).css("width");
console.log(parseInt(value)/24 + "pt");
return parseInt(value)/24 + "pt";
}) ;
});

什么部分是动态的?

这个console.log,我没用过

任何帮助将不胜感激

最佳答案

在阅读了您的问题和评论后,我仍然有些困惑,但也许这会有所帮助。

首先,学习使用 Chrome's Dev Tools .他们很棒!您现在需要了解的最重要的事情是:

要使用 console.log:

  1. 在您的 JavaScript 中,编写类似 console.log('Hello World!');
  2. 的行
  3. Google Chrome 中打开该页面
  4. F12 切换(打开/关闭)DevTools
  5. 它可能位于 Resources 选项卡上,这是检查所有资源是否已加载的好地方,但不是您要查找的资源。
  6. 点击标签为Console的标签
  7. 中提琴!如果该行 JS 已运行,您应该看到 Hello World!
  8. 欢迎使用 awesomsauce!

现在关于调整大小测试的更简单的视觉效果,请尝试以下 jQuery 增强的 JavaScript:(评论以获取解释,请删除评论)

//  This first line is the same as JavaScript's `document.ready = function() {}
// This simply starts your code running as soon as the document is loaded and ready to go
// It's safe to use in <head> or in <body> and will always ensure your JS is running with the page load
$(function() {
// Here I use .on instead of direct reference to the event as this is just good practice4 with jQuery
// You'll later learn you can use this with prewritten methods to "turn on/off" methods asigned to specific events
$(window).on('resize', function(e) {
// Little JS Tip, No need to write `var` 50thousand times. Just use a comma and write your new variable
var x = $(this).width(), // variable for window width assigned to `x`
y = $(this).height(), // variable for window height assigned to `y`
z = x/y, // your conversion you were using in the code you have in your question, there's plenty of different ways to test for size changes, do some Googling on "aspect ratio" as it's usually best way to go
// this next line is a little complicated
// It's assigning a variable based on result of an `inline if statement`
// What it says: if (element with id 'myOutput' exist) then assign that element and empty it of all it's HTML, else build the element using jQuery's element object method and append it to the <body>
// jQuery's element object building is easy, just remember it like this: jQuery("<tagName />", { object of attributes })
output = $('#myOutput').length ? $('#myOutput').empty() : $('<div />', { id: 'myOutput', style: 'background: #ffa; padding: 10px; position: fixed; right: 30px; top: 30px; z-index: 999;' }).appendTo('body'),
// the following lines build the inner HTML for showing the results of our window dimensions
pX = $('<p />', { text: 'x: '+x }).appendTo(output),
pY = $('<p />', { text: 'y: '+y }).appendTo(output),
pZ = $('<p />', { text: 'z: '+z.toFixed(5) }).appendTo(output);
});
})

如果您现在正在使用 Google Chrome,那么您现在就可以测试它!只需按 F12 并单击 Console 选项卡。复制下面的最小化代码,然后单击 Console。您应该看到一个闪烁的光标准备好输入。粘贴代码,然后按回车!然后只需调整窗口大小并观看右上角! *注意在最右上角,您可能会看到 Chrome 自己的尺寸框。我的背景是黄色的

精简代码:

$(function(){$(window).on("resize",function(a){a=$(this).width();var c=$(this).height(),d=a/c,b=$("#myOutput").length?$("#myOutput").empty():$("<div />",{id:"myOutput",style:"background: #ffa; padding: 10px; position: fixed; right: 30px; top: 30px; z-index: 999;"}).appendTo("body");$("<p />",{text:"x: "+a}).appendTo(b);$("<p />",{text:"y: "+c}).appendTo(b);$("<p />",{text:"z: "+d.toFixed(5)}).appendTo(b)})});

关于javascript - 如何动态检测浏览器视口(viewport)大小的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com