我希望检测哪个媒体查询是活动的-我使用的是Bootjack,因此我使用的是默认断点
在下面的示例中,我希望能够使用getComputedStyle()来获取“content”属性的值--但我似乎没有得到正确的语法。我可以很高兴地获得一个元素的值-比如说字体-在主体上,但不是伪元素……
下面是我正在做的事情:
鉴于此css..
/* tablets */
@media(min-width:768px){
body::after {
content: 'tablet';
display: none;
}
}
@media(min-width:992px){
body::after {
content: 'desktop';
display: none;
}
}
@media(min-width:1200px){
body::after {
content: 'large-screen';
display: none;
}
} 我的dart文件里有这个:
String activeMediaQuery = document.body.getComputedStyle('::after').getPropertyValue('content');但是activeMediaQuery总是空的。
我试过('after')和(':after')和任何其他奇怪和奇妙的东西,但都没有用。
String activeMediaQuery = document.body.getComputedStyle().getPropertyValue('font-family');将变量activeMediaQuery设置为我正在使用的字体系列的值(虽然对我没有多大用处!)
我应该做些什么?
发布于 2014-05-26 23:54:16
您还可以订阅MediaQuery更改事件
有关更多详细信息,请参阅https://github.com/bwu-dart/polymer_elements/blob/master/lib/polymer_media_query/polymer_media_query.dart
Dart中存在错误,解决方法是使用dart-js-interop。
这是聚合物媒体查询元素中的代码。我不知道评论not suppored in Dart yet是否仍然有效。我已经有几个月没试过了。
下面是一个示例页面,它展示了如何使用元素。https://github.com/bwu-dart/polymer_elements/blob/master/example/polymer_media_query.html
var _mqHandler;
var _mq;
init() {
this._mqHandler = queryHandler;
mqueryChanged(null);
if (_mq != null) {
if(context['matchMedia'] != null) {
_mq.callMethod('removeListener', [_mqHandler]);
}
// TODO not supported in Dart yet (#84)
//this._mq.removeListener(this._mqHandler);
}
if (mquery == null || mquery.isEmpty) {
return;
}
if(context['matchMedia'] != null) {
_mq = context.callMethod('matchMedia', ['(${mquery})']);
_mq.callMethod('addListener', [_mqHandler]);
queryHandler(this._mq);
}
// TODO not supported in Dart yet (#84)
// Listener hast to be as MediaQueryListListener but this is and abstract
// class and therefor it's not possible to create a listner
// _mq = window.matchMedia(q);
// _mq.addListener(queryHandler);
// queryHandler(this._mq);
}
void queryHandler(mq) {
queryMatches = mq['matches'];
//fire('polymer-mediachange', detail: mq);
}对于你在问题中提供的CSS,这对我有效,但只有当窗口宽于768像素时才有效。对于max-width: 768px,您可能会错过一个规则
import 'dart:html' as dom;
void main () {
dom.window.onResize.listen((e) {
var gcs = dom.document.body.getComputedStyle('::after');
print(gcs.content);
});
}https://stackoverflow.com/questions/23873593
复制相似问题