JavaScript代码
- //第一种写法
- var a;
- function scroll( fn ) {
- var beforeScrollTop = document.body.scrollTop,
- fn = fn || function() {};
- window.addEventListener("scroll", function() {
- var afterScrollTop = document.body.scrollTop,
- delta = afterScrollTop - beforeScrollTop;
- if( delta === 0 ) return false;
- fn( delta > 0 ? "down" : "up" );
- beforeScrollTop = afterScrollTop;
- }, false);
- }
- scroll(function(direction) {
- // console.log(direction);
- a = direction;
- });
- // 第二种写法
- var a;
- function scroll( fn ) {
- var beforeScrollTop = document.body.scrollTop,
- fn = fn || function() {};
- window.addEventListener("scroll", function() {
- var afterScrollTop = document.body.scrollTop,
- delta = afterScrollTop - beforeScrollTop;
- if( delta === 0 ) return false;
- a = fn( delta > 0 ? "down" : "up" );
- beforeScrollTop = afterScrollTop;
- }, false);
- }
- scroll(function(direction) {
- // console.log(direction);
- return direction;
- });
- // 第三种写法-----=是执行完事件后的回调函数
- var a;
- var beforeScrollTop = document.body.scrollTop,
- fn = fn || function() {};
- window.addEventListener("scroll", function() {
- var afterScrollTop = document.body.scrollTop,
- delta = afterScrollTop - beforeScrollTop;
- if( delta === 0 ) return false;
- fn( delta > 0 ? "down" : "up" );
- beforeScrollTop = afterScrollTop;
- }, false);
- function fn(direction) {
- // console.log(direction);
- a = direction;
- };
- window.onscroll=function(){
- console.log(a);
- }