- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的示例中,我使用@media 查询在屏幕分辨率发生变化时调整“布局”的大小。
/* styles for small screen and preview */
@media only screen and (min-width: 600px) {
#layout { width: 500px; height: 500px; }
}
/* styles for large screen */
@media only screen and (min-width: 900px) {
#layout { width: 800px; height: 800px; }
}
一切正常,但我还想使用 jQuery 访问 smaller 布局的 css 属性以创建它的预览。因为预览将需要一些操作(例如,它将是原始高度和宽度的较小百分比),所以我想使用 jQuery 以编程方式计算预览的尺寸(而不是简单地对其进行硬编码)。
所以我的问题是,有没有办法使用 jQuery 访问较小布局的 css 属性,而不管屏幕分辨率如何?
注意:上面的例子是我在更广泛的用例中需要做的事情的简化,所以我很清楚这样做的必要性。我还需要为不同的布局(每个布局都有自己的样式表)执行数百次此操作,因此理想情况下需要一种编程方式来执行此操作。
非常感谢任何帮助!
编辑
出于兴趣,我根据 Björn 的建议附上了一个示例。可能有更优雅的方法来做到这一点,但它对我有用。
<html>
<head>
<title>Example: accessing @media css properties</title>
<style>
/* default styles for tiny screen */
#preview { background: #ccf; }
#layout { width: 300px; height: 300px; background: #eee; }
/* styles for small screen and preview */
@media only screen and (min-width: 600px) {
#layout { width: 500px; height: 500px; background: #cdf; }
}
/* styles for large screen */
@media only screen and (min-width: 900px) {
#layout { width: 800px; height: 800px; background: #ffc; }
}
</style>
<script src="jquery.js"></script>
<script>
$(function() {
// first style sheet in page (only one in this example)
var styles = document.styleSheets[0];
for (i=0;i<styles.cssRules.length;i++) {
theRules = styles.cssRules[i];
// type 4 = media rules; media.mediaText = we can pick the media styles we want
if (theRules.type == 4 && theRules.media.mediaText == 'only screen and (min-width: 600px)') {
// media rules have their own cssRules collection
// cssRules[0] = the first rule, i.e. #layout
var layoutwidth = theRules.cssRules[0].style.width;
var layoutheight = theRules.cssRules[0].style.height;
var previewscale = 5;
var previewwidth = Math.round(parseInt(layoutwidth )/previewscale);
var previewheight = Math.round(parseInt(layoutheight)/previewscale);
// now I can set my preview dimensions
$('#preview').width(previewwidth).height(previewheight);
console.log('Preview dimensions: ' + previewwidth + ' x ' + previewheight);
}
}
});
</script>
</head>
<body>
<div id="layout">layout
<div id="preview">preview</div>
</div>
</body>
</html>
控制台会显示:
预览尺寸:100 x 100
最佳答案
您可以通过 document.stylesheets
访问文档的样式表对象。
例如,这将输出在页面上出现的第一个样式表中找到的第一个 css 规则:
console.log(document.styleSheets[0].cssRules[0].cssText);
您可以在这些对象上使用正则表达式或其他东西来获取您想要的声明的规则集(一旦您确定了它们在哪个文档的 styleSheet 对象中声明),并将这些规则应用于新创建的 styleSheet ( $("<style />")
应该有效。
我不太确定您将如何预览较小的版本,所以我不清楚如何将这个新样式表应用到预览中,但我希望这足以让您入门。
有关 styleSheet
的更多信息对象,参见 http://www.javascriptkit.com/domref/stylesheet.shtml
关于jquery - 无论屏幕分辨率如何,都使用 jQuery 访问@media css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6474404/
当我想用这个名为 Asfbin 的神奇工具拆分教程视频文件( .wmv )时,查看了媒体播放器中的所有设置,但徒劳地找不到一个... 有没有办法用milliseconds设置windows media
在我的应用程序API级别targetSdkVersion 23中,我添加了以下权限:
出于某种原因,当我发送电子邮件时,我的媒体查询没有应用。我将它从 Outlook 2007 发送到 gmail 并使用 iphone 4s 查看它。我试过使用更简单的@media 查询规则(即仅更改字
我看了一本学习python编程的书,它显示了代码: import media 所以我从链接http://pragprog.com/titles/gwpy/source_code 下载了gwpy-cod
我正在尝试使用一个小的 from,只有 3 个元素。这是我正在使用的,有人可以让我知道我哪里出错了吗?我已尝试同时使用最小宽度和最大宽度,但似乎没有任何效果。 html,body{margin:0p
要修复的网站问题: 我想弄清楚为什么我的一些@media 查询是重叠的。如果您查看我的代码,您会看到 @media 查询被标记为每个设备维度。 @media SCREEN SIZE: MASSIVE
我正在编辑一个预先存在的样式表。它目前有 0-319px 和 320-479px 的@media 部分,以及其他一些用于更大屏幕的部分。我正在尝试添加特定于 iPhone 的样式。但是,我的iphon
我读到在其他@media 查询中嵌套@media 查询在CSS3 中是完全有效的。 但是,我通过 CSS 验证器收到以下解析错误: @media screen and (max-width: 768p
我面临的情况是,对于我的一个类(class),@media 查询只是没有被读取,相反,即使 @media 标准正在被读取,类也没有围绕 @media 查询被读取的情况遇见了。 这是代码: @media
如果我从 android webrtc SDK(由 antmedia 提供)流式传输到 Ant Media 的社区版,则保存的最终视频会出现拉伸(stretch)(以手机纵向模式拍摄)。 Check
我想知道 Mobicent Media 服务器如何使用 URL 播放音频的详细信息?它在哪种方法中使用 URL 进行流式传输。对于本地存储,我了解但对于远程存储(URL),我不知道它是如何工作的。我在
简单是辉煌的关键。 我看到了 @media print 和 @media screen 标签背后的一些意义。但是将 @media screen 与(@media all 而不是打印)进行比较让我感到困
我有以下 sass: .branded @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (m
我都试过了,得到的结果相似。一直找不到答案。 最佳答案 开头和结尾的斜杠分别对匹配的内容添加了限制。 media:没有斜线,匹配当前目录和所有子目录下的文件和目录。 media/:尾部加斜杠表示只匹配
我正在尝试让 Enquire.js 为旧版浏览器工作。我正在使用文档中的深度支持方法: Modernizr.load([ { test: window.matchMedia,
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我希望将 H264 视频流式传输到 Windows Phone 8 应用程序。 那么我该如何使用 ShardDX 呢? (或者可能来自媒体基金会的 native 功能)。 我找到了用于播放视频的 Me
我想使用 cordova 录制用户的音频。在研究中,我发现了两个似乎可以完成这项工作的插件。第一media plugin及其他media capture plugin .现在我的问题是我可以从两个插件
我正在尝试弄清楚如何构建我的 CSS 文件(现在已经很多了),我想知道拥有以下内容之间是否有任何实际区别: 或拥有: @media print { definitions } 我是从浏览器的
这个问题在这里已经有了答案: Media=All vs Media=Screen (2 个答案) 关闭 8 年前。
我是一名优秀的程序员,十分优秀!