gpt4 book ai didi

dart - 通过dart中的getComputedStyle访问伪元素属性值

转载 作者:行者123 更新时间:2023-12-03 03:46:36 25 4
gpt4 key购买 nike

我希望检测哪些媒体查询处于 Activity 状态-我正在使用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设置为我正在使用的字体系列的值(不过,对我来说使用不多!)

我该怎么办?

最佳答案

您还可以订阅MediaQuery更改事件

有关更多详细信息,请参见https://github.com/bwu-dart/polymer_elements/blob/master/lib/polymer_media_query/polymer_media_query.dart

Dart中存在一个错误,解决方法使用dart-js-interop。

这是来自polymer-media-query元素的代码。我不知道注释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 px时才有效。您可能会错过 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);
});
}

关于dart - 通过dart中的getComputedStyle访问伪元素属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com