如何根据更新的 x 位置值将此 div 向左移动?
How to move this div left based on updated x position value?
http://codepen.io/leongaban/pen/WvOmwL
这应该很简单,尝试回答 found here 无济于事。
d.style.left = x_pos+'px';
我有一个 div #carousel-list
我想根据 < > 导航按钮左右移动。
当前 javascript:
document.getElementById('move-right').addEventListener("click", moveRight, false);
var carousel = document.getElementById("carousel-list");
function getPosX(el) {
// yay readability
for (var lx=0;
el != null;
lx += el.offsetLeft, el = el.offsetParent);
return {x: lx};
}
function moveRight() {
var currentX = getPosX(carousel);
console.log(currentX.x); // returns 0
console.log(currentX - 20); // returns NaN for some reason
carousel.style.left = (currentX - 20)+'px';
console.log(currentX.x); // still returns 0 so div does not move
}
我的想法我动不了carousel
div?或者为什么 (currentX - 20)
returns NaN
而 currentX
是数字 0
?
糟糕,更改:
carousel.style.left = (currentX - 20)+'px';
至:
carousel.style.left = (currentX.x - 20)+'px';
http://codepen.io/leongaban/pen/WvOmwL
这应该很简单,尝试回答 found here 无济于事。
d.style.left = x_pos+'px';
我有一个 div #carousel-list
我想根据 < > 导航按钮左右移动。
当前 javascript:
document.getElementById('move-right').addEventListener("click", moveRight, false);
var carousel = document.getElementById("carousel-list");
function getPosX(el) {
// yay readability
for (var lx=0;
el != null;
lx += el.offsetLeft, el = el.offsetParent);
return {x: lx};
}
function moveRight() {
var currentX = getPosX(carousel);
console.log(currentX.x); // returns 0
console.log(currentX - 20); // returns NaN for some reason
carousel.style.left = (currentX - 20)+'px';
console.log(currentX.x); // still returns 0 so div does not move
}
我的想法我动不了carousel
div?或者为什么 (currentX - 20)
returns NaN
而 currentX
是数字 0
?
糟糕,更改:
carousel.style.left = (currentX - 20)+'px';
至:
carousel.style.left = (currentX.x - 20)+'px';