繁体   English   中英

如果屏幕分辨率小于?则更改div内容?

[英]Change div content if screen resolution is less than?

我有一些浮动元素到我的网站,如果屏幕res是1024X768及以下将无法看到。 所以我想给那些屏幕上的人们提供不同的内容放置,内容的代码也会略有不同。

假设1024以上的人将在页面的左侧浮动一个大的正方形,下面的人将获得一个位于主要内容上方的矩形。

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

这是我希望改变主要内容的内容也将改变。

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

function 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>
} }

提前致谢

如果没有必要,请不要使用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' />

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

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

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

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

<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隐藏它!重要,它在chrome上不起作用。 我是新来的电脑......

最后,我必须为(min1450px)创建一个媒体查询。 如果我放置了应该由特定媒体查询覆盖的一般代码部分,则这个一般部分(没有媒体查询)覆盖了更具体的部分。 因此,我在媒体查询中添加了隐藏的div,该查询将针对所有具有最小查询@media屏幕并且(最小宽度:1450px)的计算机。 比OS /浏览器开始应用正确的媒体查询。

您需要为每种情况加载不同的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
    }

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

暂无
暂无

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

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