【问题标题】:Change div content if screen resolution is less than?如果屏幕分辨率小于,更改 div 内容?
【发布时间】:2011-05-07 21:47:57
【问题描述】:

我的网站有一些浮动元素,如果屏幕分辨率为 1024X768 及以下,这些元素将无法显示。因此,我想为使用该屏幕的人们提供该内容的不同位置,并且内容的代码也会略有不同。

假设 1024 及以上的人会在页面左侧得到一个大正方形,而下面的人会得到一个位于主要内容上方的矩形。

我猜可能有一些 javascript 项目可以做到这一点,但我还没有找到任何...

这是我要更改的内容主div id也会更改。

这是一个不需要做的事情的例子。

函数 shareStuff() {

if ($(window).width() < 1024) {
   <div id="sharepost"><div class="sharer"><a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink(); ?>" data-text="<?php the_title(); ?>" data-count="vertical" data-via="code_love" data-related="love:code">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<div class="sharer"><a name="fb_share" type="box_count" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript" defer="defer"></script></span>
</div><div class="sharer"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js" defer="defer"></script></div><div class="sharer"><script src="http://www.stumbleupon.com/hostedbadge.php?s=3"></script></div><span class="st_email" ></span>
</div>

} else {
  <div id="sharepost"><div class="sharer"><a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink(); ?>" data-text="<?php the_title(); ?>" data-count="horizontal" data-via="code_love" data-related="love:code">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<div class="sharer"><a name="fb_share" type="horizontalk" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript" defer="defer"></script></span>
</div><div class="sharer"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js" defer="defer"></script></div><div class="sharer"><script src="http://www.stumbleupon.com/hostedbadge.php?s=3"></script></div><span class="st_email" ></span>
</div>
} }

提前致谢

【问题讨论】:

  • 正方形和长方形的内容一样吗?只是定位问题?
  • 在这种情况下使用流畅的布局通常是个好主意
  • 矩形和正方形的内容是完全不同的HTMl代码。谢谢

标签: javascript jquery html css


【解决方案1】:

刚刚遇到同样的问题。我混合了上面的一些答案。

<div class="someclass" id="hidden_1" style="display:none"> Something very big for big screen</div> 
<div class="someclass" id="hidden_2" style="display:none"> small</div> 


 @media only screen and (min-width: 1450px)
{
#hidden_1 {display: block !important}
}
@media only screen and (min-width:1281) and (max-width:1450)
{
#hidden_1 {display: block !important}
}

@media only screen and (min-width:981px) and (max-width:1024px)
{
#hidden_2 {display: block !important}
}

您只需要确保填写所有可能的屏幕!否则将没有文字。我的问题是 1024px 屏幕....

我试图让一个 div 正常,然后用 CSS !important 隐藏它,但它在 chrome 上不起作用。我是计算机新手...

最后,我必须为 (min1450px) 创建一个媒体查询。如果我放置了应该被特定媒体查询覆盖的一般代码部分,那么这个一般部分(没有媒体查询)将覆盖更具体的部分。所以我在媒体查询中添加了隐藏的 div,该查询将针对所有具有最小查询@media only screen 和(最小宽度:1450px)的计算机。比操作系统/浏览器开始应用正确的媒体查询。

【讨论】:

    【解决方案2】:

    如果没有必要,请不要使用 javascript,媒体查询会有所帮助

    <!-- here provide css for all (also > 1024px) cases -->
    <link rel='stylesheet' type='text/css' href='allresolution.css' />
    
    <!-- here overwrite css for small devices -->
    <link rel='stylesheet' type='text/css' media="all and (max-width:1024px)" href='lessthan1024.css' />
    

    【讨论】:

    • 哦,但它不仅仅是样式表,它也是页面上的内容,因为 div 将根据屏幕分辨率保存不同的数据。谢谢
    • 您可以将两个内容放在 2 个不同的 div 或放在一边的元素中,然后只显示正确的一个
    • 就性能和文档大小而言,您显然应该只提供正确的内容,但至少您将避免通过 ajax 调用检索内容,从而使您的页面无法访问。
    • 是的,内容有不同的html内容,也有不同的div,并且包含调用外部javascript文件的javascript标签。
    • @fcalderan - 你如何用 CSS 样式更改 div 的内容?对于 1024 屏幕,我需要将标题更改为更短的标题,我希望能够实现您的想法。谢谢。
    【解决方案3】:

    您需要为每种情况加载不同的 css 样式表,您可以通过在加载和调整大小时检查 screen.width 来做到这一点。

    类似的东西。

      if(screen.width==1024)
         {
            document.write("<link rel='stylesheet' type='text/css' href='style_1024.css' />"); // this css runs in only 1024 resulation
         }
      else
        {
            document.write("<link rel='stylesheet' type='text/css' href='style_other.css' />"); // for other resulatiom
        }
    

    如果窗口从一个更改为另一个,您将需要删除另一个样式表。

    【讨论】:

    • 如果我在文档中添加这个,它会起作用吗?
    【解决方案4】:

    无法检测屏幕分辨率,但可以检测浏览器大小:

    if ($(window).width() < 1024) {
        // code for small viewports
    } else {
        // code for large viewports
    }
    

    这应该足以满足您的需求。

    【讨论】:

    • 我需要把它放在脚本标签中吗?谢谢
    • @Mark 那是Javascript——你需要把它放在脚本标签和document.onready处理程序中。然后,您需要在其中使用 Javascript 来更改页面的布局。最好的方法是将一个类应用于body 元素,然后根据body 的类设置两组CSS。
    • 我也可以在里面放脚本吗?因为这包含来自 Facebook Twitter 等的共享按钮,其中包含 javascript...谢谢。
    • @Mark 我不确定你的意思。您需要显示您的代码以使其清晰。
    • 谢谢。以下是部分代码:
      twitter.com/share" class="twitter-share-button" data-url="" data- text="" data-count="vertical" data-via="brainy" data-related="hopes4thebest">Tweet
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2016-12-25
    • 1970-01-01
    相关资源
    最近更新 更多