I've got an object generator. It works properly.
我有一个对象生成器。它工作正常。
'use strict';
function Div(isim) {
this.loc = document.getElementById(isim);
var style = window.getComputedStyle(this.loc);
this.width = style.getPropertyValue('width');
this.height = style.getPropertyValue('height');
this.left = style.getPropertyValue('left');
this.top = style.getPropertyValue('top');
}
But later I am updating the properties of the element
但后来我正在更新元素的属性
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
console.log(d.left); //gives auto
console.log(d.width); //gives the right value
and console.log(d.left)
is wrong. I have already found a way to fix it but it is a bit dirty, I think:
和console.log(d.left)是错误的。我已经找到了修复它的方法,但它有点脏,我想:
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
d = new Div("d");
console.log(d.left); //gives the right value
console.log(d.width); //gives the right value
Is there an another way(one line I prefer)? And Unfortunately, I am not good at English and if there are mistakes in question, title, please edit them.
还有另一种方式(我更喜欢一条线)吗?不幸的是,我不擅长英语,如果有问题,标题,请编辑它们。
2 个解决方案
#1
The value is cached so you need to recompute.
该值已缓存,因此您需要重新计算。
function Div(isim) {
this.loc = document.getElementById(isim);
var style = window.getComputedStyle(this.loc);
this.width = style.getPropertyValue('width');
this.height = style.getPropertyValue('height');
this.left = style.getPropertyValue('left');
this.top = style.getPropertyValue('top');
this.getStyle = function (prop) {
return style.getPropertyValue(prop);
}.bind(this);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
console.log(d.getStyle('left'));
console.log(d.getStyle('width'));
#2
In your function change this.left to
在你的函数中将this.left更改为
this.left = function () {
return window.getComputedStyle(this.loc).getPropertyValue('left');
}
then in your call change it to
然后在你的通话中将其更改为
console.log(d.left());
#1
The value is cached so you need to recompute.
该值已缓存,因此您需要重新计算。
function Div(isim) {
this.loc = document.getElementById(isim);
var style = window.getComputedStyle(this.loc);
this.width = style.getPropertyValue('width');
this.height = style.getPropertyValue('height');
this.left = style.getPropertyValue('left');
this.top = style.getPropertyValue('top');
this.getStyle = function (prop) {
return style.getPropertyValue(prop);
}.bind(this);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
console.log(d.getStyle('left'));
console.log(d.getStyle('width'));
#2
In your function change this.left to
在你的函数中将this.left更改为
this.left = function () {
return window.getComputedStyle(this.loc).getPropertyValue('left');
}
then in your call change it to
然后在你的通话中将其更改为
console.log(d.left());