Say*_*ohe 4 javascript function onscroll
我在这里面临一个小问题.我有关于Javascript的基本知识,我想做以下事情:
现在,当您向下滚动时,菜单会变小.当你回来时,它会恢复正常.我用window.onscroll调用这个事件:
var diff = 0;
function effects(){
var topDistance = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
var clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
if(topDistance > diff){
diff = topDistance;
if(clientWidth > 1024){
if(topDistance > 300){
document.getElementById("header").style.marginTop= "-100px";
}
}
}else if(topDistance < diff){
diff = topDistance;
if(clientWidth > 1024){
if(topDistance > 300){
document.getElementById("header").style.marginTop= "0";
}
}
}
}
window.onscroll = effects();
Run Code Online (Sandbox Code Playgroud)
现在我想要另一个函数对我的调用操作按钮有一些效果,让我们调用函数"test",但如果我想以同样的方式执行此操作,效果函数将不再起作用:
function test(){
//do something
}
window.onscroll = test();
Run Code Online (Sandbox Code Playgroud)
欢迎任何帮助!我认为这样做不会是一个很大的挑战,但我猜错了.(PS:NO JQUERY PLEASE)
Ste*_*tis 17
您可以通过执行覆盖onscroll功能 window.onscroll = blabla
你可以做 :
window.onscroll = function() {
effects();
test();
}
Run Code Online (Sandbox Code Playgroud)
要么
window.addEventListener('scroll', effects);
window.addEventListener('scroll', test);
Run Code Online (Sandbox Code Playgroud)
您可以为滚动事件使用多个侦听器.
window.addEventListener('scroll', effects);
window.addEventListener('scroll', test);
Run Code Online (Sandbox Code Playgroud)
这样你就不会覆盖window.onscroll