简体   繁体   English

根据视口大小更改HTML元素的宽度

[英]Change HTML element's width based on viewport size

I've been tasked with making a web application more mobile-friendly. 我的任务是使Web应用程序更适合移动设备。 I'm running the app through PhoneGap to get the mobile build, but testing its appearance beforehand using the Ripple Emulator. 我正在通过PhoneGap运行应用程序来获取移动版本,但是事先使用Ripple Emulator测试其外观。

PhoneGap works pretty well on the app, but there's a kind of "control panel" whose width does not change, and it makes it so that this control panel takes up the majority of the width of the mobile view, which is no good. PhoneGap在应用程序上运行良好,但是有一种“控制面板”,其宽度不会改变,并且它使得这个控制面板占据了移动视图的大部分宽度,这是不好的。

So essentially, I need to edit the current JavaScript file so that it detects whether the viewer is a mobile device, and adjust the width of this control panel element accordingly. 基本上,我需要编辑当前的JavaScript文件,以便检测查看器是否是移动设备,并相应地调整此控制面板元素的宽度。 Unfortunately, I am basically brand new to all web development.. 不幸的是,我对所有网络开发基本上都是全新的..

So as a general question, how would I go about doing this? 因此,作为一般性问题,我将如何做到这一点? I think I need to make these adjustments before the page is actually loaded, but I'm not sure of where in the JS file this would happen. 我认为我需要在实际加载页面之前进行这些调整,但我不确定JS文件中会发生什么。 The client is using JQuery Mobile and a few other libraries. 客户端正在使用JQuery Mobile和其他一些库。 The original developer is already using the 原始开发者已经在使用了

<meta name="viewport" content="width=device-width, initial-scale=1" />

tag, but it has no effect on this control panel section, and the width of the control panel's elements are hard-coded in. 标签,但它对此控制面板部分没有影响,控制面板元素的宽度是硬编码的。

This is a pretty vague question, but I'd appreciate any tips or guidance. 这是一个非常模糊的问题,但我很感激任何提示或指导。

You may want to check out "responsive" design. 您可能想要查看“响应式”设计。 Specifically, I've really liked Bootstrap's implementation. 具体来说,我真的很喜欢Bootstrap的实现。 It is all CSS controlled and is based on the viewport pixel width. 它全部由CSS控制,基于视口像素宽度。

You can create responsive CSS by using the following "@media" css code: 您可以使用以下“@media”css代码创建响应式CSS:

@media (max-width: 240px) {
   /* really tiny screens */
}
@media (min-width: 241px) and (max-width: 319px) {
   /* a little bit bigger screens */
}
@media (min-width: 320px) and (max-width: 767px) {
   /* Basically up to, but not including an iPad */
}
@media (min-width: 768px) {
   /* iPad and bigger */
}

Inside each @media tag, you can place your custom CSS for each size, so at 240px, you might have a title class with a font size of 16px: 在每个@media标记内,您可以为每个大小放置自定义CSS,因此在240px时,您可能有一个字体大小为16px的标题类:

@media (max-width: 240px) {
   /* really tiny screens */
  .title {
    font-size: 16px;
  }
}

Then rinse and repeat to change the font sizes for each subsequent viewport size. 然后冲洗并重复以更改每个后续视口大小的字体大小。

Use orientation specific CSS such as: 使用特定方向的CSS,例如:

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">

Set the viewports setting to the devices max width and take care of the actual width of the content using CSS: 将视口设置设置为设备最大宽度,并使用CSS处理内容的实际宽度:

<meta id="viewport" name="viewport" content="initial-scale=1.0;minimum-scale=1.0; maximum-scale=1.0" />

In this case, I'd recommend using Media Queries . 在这种情况下,我建议使用媒体查询

@media (min-width: X) and (max-width: Y) {
    /* CSS rules for screens of width between X and Y. */
}

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

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