简体   繁体   English

如何为不同的屏幕尺寸编写不同的 HTML

[英]How to write different HTML for different screen sizes

I understood how I change CSS via media queries (such as media="screen and (max-width:640px)")我了解如何通过媒体查询更改 CSS(例如 media="screen 和 (max-width:640px)")

but let's say I want to write (just for example)但是让我们说我想写(例如)

<div>
[if screen resolution is lower then 960 px]
    <div>
    some new text only for lower resolution
    </div>
[end of condition]
</div>

What is the condition I need to write to get it right?我需要写什么条件才能正确?

As far as i have experienced, you cannot do media queries inside HTML pages.据我所知,您不能在 HTML 页面内进行媒体查询。 You need to do it from within your CSS.您需要在 CSS 中执行此操作。

But if you want to show some special text only when it is below a certain resolution, why not only make it visible when the resolution is lower than 960px?但是如果你想在低于某个分辨率时才显示一些特殊的文字,为什么不只在分辨率低于960px时才显示呢?

Creating responsive designs is very different from a regular design, because you have to think a lot more (which is haaard)创建响应式设计与常规设计非常不同,因为您必须考虑更多(haaard)

you can check it via using javascript screen object :您可以通过使用 javascript screen 对象来检查它:

screen.width 

or you can do this with css或者你可以用 css 做到这一点

<link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" />

http://css-tricks.com/6206-resolution-specific-stylesheets/ http://www.javascriptkit.com/howto/newtech3.shtml http://css-tricks.com/6206-resolution-specific-stylesheets/ http://www.javascriptkit.com/howto/newtech3.shtml

I am actually going through the same situation and found that if you want to do this you could really add all the text in the HTML page, and then hide it on screen widths that you don't want it to show.我实际上正在经历同样的情况,发现如果你想这样做,你真的可以在 HTML 页面中添加所有文本,然后将它隐藏在你不希望它显示的屏幕宽度上。 For example:例如:


    <div>
    [text that will be shown for screens less or equal to 960px in width]
        <div class="smallScreen">
        some new text only for lower resolution
        </div>
    [end of condition for small screens]

    [text that will be shown for other screens that are greater in width]
        <div class="largeScreen">
        some new text only for higher resolution
        </div>
    </div>

And then you could add CSS:然后你可以添加CSS:

    /* On smaller resolutions, hide the text for Large screens */
    @media only screen and (max-width: 960px) {
        .largeScreen {display: none;}
   }

   /* On larger resolutions, hide the text for Small screens */
    @media only screen and (min-width: 960px) {
        .smallScreen {display: none;}
   }

I hope this works out fine :)我希望这一切顺利:)

You need to assign an id (or a class or any other way of finding your element from CSS) to the <div> and then you can set a media query definition like this:您需要为<div>分配一个 id(或一个类或任何其他从 CSS 查找元素的方式),然后您可以像这样设置媒体查询定义:

<div id="mydiv">...</div>
<style type="text/css">
@media screen and (min-width: 961px) {
   div#mydiv { display: none }
}
</style>

Or for better readability: Make it hidden on default and visible if max-width: 960px .或者为了更好的可读性:如果max-width: 960px则使其在默认情况下隐藏并可见。

You can add jQuery function to change style dynamically as per scree resolution.您可以添加jQuery函数以根据屏幕分辨率动态更改样式。

if(screen.width==1600)
       { 
        jQuery('#hb-gotop').removeAttr("margin-left", "-0.9");
        jQuery('#hb-gotop').attr('style', 'margin-left: -0.7%');
       }
        else if(screen.width==1280)
       { 
        jQuery('#hb-gotop').removeAttr("margin-left", "-0.9");
        jQuery('#hb-gotop').attr('style', 'margin-left: -0.9%');
       }    
       else if(screen.width==1024)
       { 
        jQuery('#hb-gotop').removeAttr("margin-left","-0.9");
        jQuery('#hb-gotop').attr('style', 'margin-left: -1.1%');
       }
        else if(screen.width==800)
        { 
        jQuery('#hb-gotop').removeAttr("margin-left","-0.9");
        jQuery('#hb-gotop').attr('style', 'margin-left: -1.3%');
       }

Answere was helpful from: Answere 从以下方面提供了帮助:

if screen resolution is less than x append css 如果屏幕分辨率小于 x 追加 css

You can do this entirely with CSS 3 using the @media command.您可以使用 @media 命令完全使用 CSS 3 来完成此操作。

**@media (max-width:960px) { css... } //nothing with screen size bigger than 960px

@media (min-width:960px) { css... } //nothing with screen size smaller than 960px** @media (min-width:960px) { css... } //屏幕尺寸小于960px**

Jason Whitted makes a good point, this is CSS 3 only, so it won't work with older browsers (it should work with all modern browsers though). Jason Whitted 提出了一个很好的观点,这只是 CSS 3,所以它不适用于旧浏览器(尽管它应该适用于所有现代浏览器)。 You can as well do screen or device edit您也可以进行屏幕或设备编辑

@media screen { .nomobile { display: block; @media screen { .nomobile { display: block; } } //desktops/laptops } } //台式机/笔记本电脑

@media handheld { .nomobile { display: none; @media 手持 { .nomobile { 显示:无; } } //mobile devices Or you could assume mobile devices will have a smaller width, and go on that. } } //移动设备 或者你可以假设移动设备的宽度会更小,然后继续。

I could be wrong, but I think css selection by resolution would need a little help from javascript.我可能是错的,但我认为按分辨率选择 css 需要 javascript 的一些帮助。

Here is a quick example of what that js could look like, embedded in jquery:这是嵌入在 jquery 中的 js 外观的快速示例:

$(document).ready(function() {

  if ((screen.width>=1024) && (screen.height>=768)) {
   alert('Screen size: 1024x768 or larger');  
   $("link[rel=stylesheet]:not(:first)").attr({href : "detect1024.css"});
 }
  else  {
    alert('Screen size: less than 1024x768, 800x600 maybe?');
    $("link[rel=stylesheet]:not(:first)").attr({href : "detect800.css"});
  }
});

Hope that helps.希望有帮助。

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

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