javascript - 理解 classie.js 中的 js 代码

标签 javascript

大家好,我下载了一些用 JS 编码的简单效果。该插件名为 classie.js,该人员编写了一些与此插件 classie.js 交互的自定义​​代码

前一段时间有人问过类似的问题classie.js Question这个人真的完美地回答了 classie.js 中的代码是如何运行的。太好了,所以现在我明白了 classie.js 中的代码是如何工作的,现在我的问题是,有很多编写的代码实际上与这个名为 classie.js 的插件交互,我理解起来有些困难。所以让我详细解释一下我的问题是什么:

classie.js 代码:

( function( window ) {

'use strict';

var hasClass, addClass, removeClass;


if ( 'classList' in document.documentElement ) {

  // console.log(document.documentElement);

  hasClass = function( elem, c ) {
    // cons100%ole.log("elem is : " + elem + " c is " + c);
    return elem.classList.contains( c );
  };

  addClass = function( elem, c ) {
    console.log('elem Logged');
    console.log(elem);
    elem.classList.add( c );
  };

  removeClass = function( elem, c ) {
    console.log('removeClass function got used in if statement')
    elem.classList.remove
    ( c );
  };
}
 else {
       // I have deleted this part as its only a fallback for older browsers. :)
 }

function toggleClass( elem, c ) {
  var fn = hasClass( elem, c ) ? removeClass : addClass;
  fn( elem, c );
}

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

// transport
if ( typeof define === 'function' && define.amd ) {
  // AMD
  define( classie );
} else if ( typeof exports === 'object' ) {
  // CommonJS
  module.exports = classie;
} else {
  // browser global
  window.classie = classie;
}

})( window );

与classie.js交互的代码:

(function() {


                // disable/enable scroll (mousewheel and keys) from https://stackoverflow.com/a/4770179                 
                // left: 37, up: 38, right: 39, down: 40,
                // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
                var keys = [32, 37, 38, 39, 40], wheelIter = 0;

                function preventDefault(e) {
                    e = e || window.event;
                    if (e.preventDefault)
                    e.preventDefault();
                    e.returnValue = false;  
                }

                function keydown(e) {
                    for (var i = keys.length; i--;) {
                        if (e.keyCode === keys[i]) {
                            preventDefault(e);
                            return;
                        }
                    }
                }

                function touchmove(e) {
                    preventDefault(e);
                }

                function wheel(e) {
                    // for IE 
                    //if( ie ) {
                        //preventDefault(e);
                    //}
                }

                function disable_scroll() {
                    window.onmousewheel = document.onmousewheel = wheel;
                    document.onkeydown = keydown;
                    document.body.ontouchmove = touchmove;
                }

                function enable_scroll() {
                    window.onmousewheel = document.onmousewheel = document.onkeydown = document.body.ontouchmove = null;  
                }

                var docElem = window.document.documentElement,
                    scrollVal,
                    isRevealed, 
                    noscroll, 
                    isAnimating,
                    container = document.getElementById( 'container' ),
                    trigger = container.querySelector( 'button.trigger' );

                function scrollY() {
                    return window.pageYOffset || docElem.scrollTop;
                }

                function scrollPage() {
                    scrollVal = scrollY();

                    // console.log(scrollVal);  

                    if( classie.has( container, 'notrans' ) ) {
                        classie.remove( container, 'notrans' );
                        return false;
                    }

                    if( isAnimating ) {
                        return false;
                    }

                    if( scrollVal <= 0 && isRevealed ) {
                        toggle(0);
                    }
                    else if( scrollVal > 0 && !isRevealed ){
                        toggle(1);
                    }
                }

                function toggle( reveal ) {
                    isAnimating = true;

                    if( reveal ) {
                        classie.add( container, 'modify' );
                    }
                    else {
                        noscroll = true;
                        disable_scroll();
                        classie.remove( container, 'modify' );
                    }

                    // simulating the end of the transition:
                    setTimeout( function() {
                        isRevealed = !isRevealed;
                        isAnimating = false;
                        if( reveal ) {
                            noscroll = false;
                            enable_scroll();
                        }
                    }, 600 );
                }

                // refreshing the page...
                var pageScroll = scrollY();
                noscroll = pageScroll === 0;

                disable_scroll();

                if( pageScroll ) {
                    isRevealed = true;
                    classie.add( container, 'notrans' );
                    classie.add( container, 'modify' );
                }

                window.addEventListener( 'scroll', scrollPage );
                // trigger.addEventListener( 'click', function() { toggle( 'reveal' ); } );
            })();

很多与 classie.js 交互的代码实际上是直接从 stackoverflow 的线程派生的:how to disable and enable scroll

现在以上所有内容只是为了让您清楚地理解,我的真正问题是,我不太了解 add 方法在与 classie.js 交互的代码中的用法API,它在某种程度上对我没有任何意义,MDN 文档对这种方法说得很少。这个方法到底在做什么?? .

编辑::令人困惑的部分:

function toggle( reveal ) {
                    isAnimating = true;

                    if( reveal ) {
                        classie.add( container, 'modify' );
                    }
                    else {
                        noscroll = true;
                        disable_scroll();
                        classie.remove( container, 'modify' );
                    }

                    // simulating the end of the transition:
                    setTimeout( function() {
                        isRevealed = !isRevealed;
                        isAnimating = false;
                        if( reveal ) {
                            noscroll = false;
                            enable_scroll();
                        }
                    }, 600 );
                }

以上是让我感到困惑的部分,我猜对了吗,如果要使用 classie.js 中的任何函数,则必须按如下方式使用:

classie.functionname(); ??并且不能直接评估??例如。函数名();

我的第二个大问题(理解classie.js的JS语法):

同样作为一个补充问题,你可以选择不回答,但是与classie.js交互的代码中的某些部分有很多令人困惑的语法,让我指出来。

在 disable_scroll 函数中是这样的:

  window.onmousewheel = document.onmousewheel = wheel;
  document.onkeydown = keydown;

在启用滚动功能中有这个:

window.onmousewheel = document.onmousewheel = document.onkeydown = null;

现在我明白了

A = B;

其中你将 B 的值赋给 A ;

但是上面的 Syntex 更像是 A = B = C ;这完全超出了我的理解范围。

有人可以澄清一下吗

如果有人能详细解释一下,那就太好了。

谢谢。

亚历山大。

最佳答案

还没有足够的代表发表评论。 add() 方法不是“ native ”js 函数。如果您查看 classie.js 代码,在它的末尾是一个对象“classie”,它定义了内部函数 addClass 的公共(public)快捷方式:

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

这些快捷方式将允许您通过调用 classie.publicFunctionName(args) 或 window.classie.publicFunctionName(args) 来调用内部函数(无法从全局范围访问),其中“publicFunctionName”是定义的快捷方式,正是第二段代码所做的:

...
classie.remove( container, 'modify' );
...    
classie.add( container, 'modify' );

addClass() 方法所做的就是将一个类添加到调用它的 html 元素。

我相信这被称为“揭示模块”设计模式,但不是 100% 肯定。

希望至少能有所帮助。 如果您想了解一些 js 设计模式,我强烈建议您阅读 Addy Osmani 的非常好的(免费)书籍:http://addyosmani.com/resources/essentialjsdesignpatterns/book/

关于javascript - 理解 classie.js 中的 js 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28027332/

相关文章:

javascript - 使用 Flask 表单发送额外数据?

javascript - 将 amMap 中选定区域的颜色更改为与相邻区域不同的颜色,

javascript - jQuery addClass 定位正确的元素 - 但有时只应用类

javascript - 在不破坏响应能力的情况下添加滚动条

javascript - 将语言更改为 Bootstrap Datetimepicker

javascript - 获取具有最高值的变量并将该变量的名称插入到输入中

javascript - 如何从 iframe 中获取元素

Javascript 变量在不应该为 true 时变为 true

Javascript:删除由 ajax 响应设置的 cookie

javascript - 无法在javascript中创建变量