angular - 是否可以使用引导程序根据角度的屏幕尺寸设置变量值?
问题描述
我在这样的角度组件中有一个全局变量,
visibleDays = 7
我希望能够根据屏幕尺寸控制这个值。对于较小的设备,我希望这是 3 而不是 7。是否可以为“sm”、“xs”设备执行此操作?
解决方案
您可以跟踪窗口大小和 innwewidth 的基数更新 visibleDays 的值
应用程序组件
visibleDays = 7;
@HostListener("window:resize", []) updateDays() {
// lg (for laptops and desktops - screens equal to or greater than 1200px wide)
// md (for small laptops - screens equal to or greater than 992px wide)
// sm (for tablets - screens equal to or greater than 768px wide)
// xs (for phones - screens less than 768px wide)
if (window.innerWidth >= 1200) {
this.visibleDays = 7; // lg
} else if (window.innerWidth >= 992) {
this.visibleDays = 6;//md
} else if (window.innerWidth >= 768) {
this.visibleDays = 5;//sm
} else if (window.innerWidth < 768) {
this.visibleDays = 3;//xs
}
}
推荐阅读
- animation - 旋转文字动画和持续时间
- javascript - JS用一个实例创建不同的账户
- json - Firefox 插件:将加载的 json 解析/保存为 HAR 或文本文件
- pyspark - pySpark 数据框过滤方法
- shell - 我正在尝试通过在 osx 10.14 上制作可点击的 shell 脚本来启动“jupyter notebook”
- flutter - 更改文本相对于其他小部件的大小/位置
- sorting - 如何从 29 年的数据集中按小时计算一年的中值?
- c# - 这是 C# Monad,问题出在哪里?
- python - 将嵌套字典转换为数据框,键作为行名,值中的字典作为列?
- command-line-interface - 使用 spaCy 命令行训练器管理训练/开发拆分