小编Say*_*ohe的帖子

Javascript - 执行多个不同的window.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 …

javascript function onscroll

4
推荐指数
2
解决办法
7848
查看次数

标签 统计

function ×1

javascript ×1

onscroll ×1