嘿伙计们下载了一些用JS编码的简单效果.该插件名为classie.js,该家伙编写了一些与此插件classie.js交互的自定义代码
一段时间前问过一个类似的问题classie.js问题和这个家伙真的完美地回答了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 );
Run Code Online (Sandbox Code Playgroud)
与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' ); } );
})();
Run Code Online (Sandbox Code Playgroud)
很多与classie.js交互的代码实际上是直接从stackoverflow派生的:如何禁用和启用scroll
现在所有上述内容仅仅是为了您清楚的理解,我的问题是什么,我是不是很清楚在与classie.js API交互的代码中使用add方法,它在某种程度上没有任何意义对我和MDN doc说的这个方法很少.那个方法到底真的在做什么?.
编辑::令人困惑的部分:
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 );
}
Run Code Online (Sandbox Code Playgroud)
以上是令我困惑的部分,我猜对了,如果必须使用classie.js中的任何函数,则必须使用如下:
classie.functionname(); ?? 而且不能直接评估?例如.使用functionName();
我的第二大问题(理解classie.js的JS语法):
另外作为一个补充问题,你可以选择不回答,但是与classie.js交互的代码的某些部分有很多令人困惑的语法,让我指出来.
在disable_scroll函数中有这样的:
window.onmousewheel = document.onmousewheel = wheel;
document.onkeydown = keydown;
Run Code Online (Sandbox Code Playgroud)
并在启用滚动功能中有这样的:
window.onmousewheel = document.onmousewheel = document.onkeydown = null;
Run Code Online (Sandbox Code Playgroud)
现在我明白了
A = B;
你给A分配了B的值;
但上面的Syntex更像是,A = B = C; 这完全超出了我的想法.
请有人澄清一下
如果有人可以详细说明并解释,那就太好了.
谢谢.
亚历山大.
小智 6
还没有足够的代表发表评论.add()方法不是'本机'js函数.如果你看一下classie.js代码,在它的末尾是一个对象'classie',它定义了内部函数addClass的公共快捷方式:
var classie = {
// full names
hasClass: hasClass,
addClass: addClass,
removeClass: removeClass,
toggleClass: toggleClass,
// short names
has: hasClass,
add: addClass,
remove: removeClass,
toggle: toggleClass
};
Run Code Online (Sandbox Code Playgroud)
这些shorcuts将允许您通过调用classie.publicFunctionName(args)或window.classie.publicFunctionName(args)调用内部函数(无法从全局范围访问),其中"publicFunctionName"是定义的shorcut,这正是第二块代码确实:
...
classie.remove( container, 'modify' );
...
classie.add( container, 'modify' );
Run Code Online (Sandbox Code Playgroud)
所有addClass()方法都会向调用它的html元素添加一个类.
我相信这被称为'揭示模块'设计模式,但不是100%肯定.
希望至少有一点帮助.如果你想了解js设计模式,我热烈推荐阅读Addy Osmani这本非常好(和免费)的书:http://addyosmani.com/resources/essentialjsdesignpatterns/book/
| 归档时间: |
|
| 查看次数: |
5665 次 |
| 最近记录: |