JavaScript - 在点击时递增地重新定位 DIV

标签 javascript css onclick scroll

非常简单的代码,非常简单的问题。按下链接时,它会向上或向下移动一个 div。但是,我无法让它逐步移动。我知道这是一个简单的语法错误,但谷歌并没有透露我的方式的错误。有谁愿意赐教吗?

<a class="galsel" onclick="document.getElementById('innerscroll').style.bottom -='167px';">&laquo;</a>

<a class="galsel" onclick="document.getElementById('innerscroll').style.bottom +='167px';">&raquo;</a>

我已经有了它,所以 div 会垂直平铺,所以我不担心它会“太高”或“太低”

这是现在的样子:drainteractive.com/FBD/projects.php

最佳答案

您必须从包含 px 的字符串中解析值

// Increase by 167
document.getElementById('innerscroll').style.bottom = (parseInt(document.getElementById('innerscroll').style.bottom, 10) + 167) + ' px'

// Decrease by 167
document.getElementById('innerscroll').style.bottom = (parseInt(document.getElementById('innerscroll').style.bottom, 10) - 167) + ' px'

// Abstracted
function addToBottom(el, amount) {
   // You probably add lower and upper bound check conditions
   el.style.bottom = (parseInt(el.style.bottom) + amount) + ' px';
}

var el = document.getElementById('innerscroll');
addToBottom(el, 167);
addToBottom(el, -167);

还要确保它适用于最初未设置底部的情况

var currentBottom = parseInt(document.getElementById('innerscroll').style.bottom) || 0;

关于JavaScript - 在点击时递增地重新定位 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13130522/

相关文章:

javascript - 使用 Javascript 在点击时显示 1 个 div 并隐藏所有其他的

javascript - 为什么我的 onclick""不起作用?

javascript - 单击除一个元素之外的 HTML,不使用 jQuery

javascript - ScrollTop 无法更新到 jQuery 1.11.1

javascript - 分段按钮 OpenUI5 setSelectedButton

php - 未加载 css,因为它的 MIME 类型 “text/html” 不是 “text/css”

css 悬停图像交换和链接?

javascript - ClojureScript core.match 中匹配 false (== undefined, null)

javascript - 如何根据索引数组更改元素的状态?

html - 为什么我的图片将我的文字推到标题下方?