嗨,你好,
这是我的问题。我有这个Javascript和我的变量叫"位置"当我进入
时,不会保留指定的值under[i].addEventListener("click", function(){
alert(position.y + " " + position.x);
score[i].setAttribute("style","-webkit-transform: translateY(-" + position.y + "px); -webkit-transition: .5s ease-in-out .01s;");
under[i].classList.add("popout");
score[i].classList.add("popScore");
under[i].classList.remove("underlayer_score");
score[i].classList.remove("scores_teams");
});
这很奇怪,因为当我在事件监视器之前或之后检查时,仍然会在该变量中分配值。我仍然是Javascript的初学者,所以很抱歉,如果我的解释不那么清楚。
感谢您的帮助。
以下是Javascript:
<script>
var under = new Array(10);
score = new Array(10);
var position = 0;
for(var i = 0; i <= 2; i++){
under[i] = document.getElementsByClassName('underlayer_score')[i];
score[i] = document.getElementsByClassName('scores_teams')[i];
}
function getPosition(element) {
var xPosition = 0;
var yPosition = 0;
while(element) {
xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}
for(var i = 0; i <= 2; i++){
position = getPosition(score[i]);
under[i].addEventListener("click", function(){
alert(position.y + " " + position.x);
score[i].setAttribute("style","-webkit-transform: translateY(-" + position.y + "px); -webkit-transition: .5s ease-in-out .01s;");
under[i].classList.add("popout");
score[i].classList.add("popScore");
under[i].classList.remove("underlayer_score");
score[i].classList.remove("scores_teams");
});
alert(position.y);
position.x = 0;
position.y = 0;
}
</script>
答案 0 :(得分:1)
也许你应该尝试检查什么是insIde window.position?难道这个位置不是全球性的吗?