我试图进行单页滚动,但我遇到了一个小问题。我有
function classDirectory(className) {
let body = {
size: 20,
from: 0,
query: {
match: {
ClassName: {
query: className
}
}
}
};
return search('testclass', body).then(results => {
results.hits.hits.forEach((hit, index) =>
getDirectories(hit._source.JarFileName));
return messages;
})
.catch(function(err) {
// log the error, but keep the promise rejected
console.error(err);
throw err;
});
};
function getDirectories(jarName) {
let body = {
size: 20,
from: 0,
query: {
match: {
jarFileName: {
query: jarName
}
}
}
};
return search('testjar', body).then(results => {
results.hits.hits.forEach((hit, index) =>
messages.push({text: hit._source.jarFileName , owner: `\t${++nmb} -
${hit._source.directory}`})
);
return messages;
})
.catch(function(err) {
// log the error, but keep the promise rejected
console.error(err);
throw err;
});
};
现在这只能显示一次,所以我试图添加一个加法运算符,这样每次滚动时它都会减去100%。
var main = document.getElementById('main');
window.addEventListener('scroll', function(){
window.scrollTo(0, 0);
var top = -main.offsetTop;
main.style.top = -100 + '%';
});
但由于某种原因,这不起作用..
如果我这样做,则添加有效......
main.style.top -= 100 + '%';
但这会搞砸,因为top在技术上是px而不是%。那么任何想法如何使 main.style.top - = 100 +'%&#39 ;; 工作或者替代呢?
答案 0 :(得分:2)
让我以另一种方式编写表达式main.style.top -= 100 + '%';
,这意味着同样的事情:
main.style.top -= (100 + '%' /* this is a string now */)
如果您 - =来自任何东西的字符串会发生什么?您放置的第一个表达式(main.style.top = top - 100 + '%';
)更接近您想要的但仍然不正确。如果你想每次减去最高值的100%,就像你说你需要用像素而不是百分比来做。因此,首先您需要找到100%的像素数,因此查看代码可能只是main.offsetTop
的值。然后,您需要从顶部值中减去该值。所以你先得到的是关闭,但我认为你需要:
var main = document.getElementById('main');
window.addEventListener('scroll', function(){
window.scrollTo(0, 0);
var top = main.offsetTop;
main.style.top -= top;
});