html - 为什么 css 样式会这样?
问题描述
我基于 Pluralsight 上的“Scott Allen 的 RxJS 反应式编程入门”使用 angular-cli 创建了小型应用程序。它从鼠标创建 Observable 事件流。没什么特别的——数据流很好用。唯一的问题是样式对我来说以一种奇怪的方式应用。他们以未知的方式仅更新 div 2 次。当我在圆形区域中移动鼠标时,它们更改为 0px - 一次用于 x,一次用于 y 轴。一次,就是这样。我在开发人员工具中手动更改了这些属性,它的工作方式应该如此。但不能自动影响这种行为。
html
#circle {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: red;
position: absolute;
}
<div id="circle"></div>
打字稿
ngOnInit() {
let circle = document.getElementById('circle')
let mouse = fromEvent(document, 'mousemove')
.pipe(
map((e: MouseEvent) => {
return {
x: e.clientX,
y: e.clientY
}
}),
)
function onNext(value) {
circle.style.left = value.x
circle.style.top = value.y
console.log("xy", value.x, value.y)
}
mouse.subscribe(
value => onNext(value),
e => console.log(`error: ${e}`),
() => console.log('complete.')
)
}
解决方案
将“px”添加到值:
function onNext(value) {
console.log(circle.style.left)
circle.style.left = value.x + 'px'
circle.style.top = value.y + 'px'
console.log("xy", value.x, value.y)
}
和圆圈将通过鼠标改变位置。演示
推荐阅读
- html - css剪辑路径部分圆圈
- ada - Ada 抱怨我在函数调用中添加了一个 volatile 对象而不是 volatile 的泛型类型
- r - cronR add_cron 在所有 Mac OS High Sierra 中都不执行脚本
- django - 如何在 Django Rest Framework 中保存外键字段
- javascript - 反应自定义钩子:无法获得异步功能
- date - 如何使用 unix 在一行 csv 文件中将 23/1/17 转换为 23/01/2017?
- r - 如何使用 list.files 对目录中的 .Rmd 文件进行子集化
- python - 使用带有参数的 Pandas 从 QuestDb 查询时间序列
- docusignapi - 有没有办法在信封(不带扩展名)中获取文档的文件类型?
- hadoop - 列出 HDFS 上 zip 文件的内容