js Flashcards
如果想让事件由上至下
xx.addEventListener(‘click’, fn, true);
如果上下都有事件,想要执行上面事件后阻止下面事件?
event.stopPropagation();
open a link in js?
same window:window.location = url;new window:window.open(url);
how to break out of a forEach loop?
you can’t.but you could break out of a some/every by returning false.
zepto在requirejs引用的时候……
不能带$配对:define([‘zepto’], function() { console.log($)});
how to detect browser:
create a test element to see if there exists such a property, so we could detect if the browser is -webkit or -moz, and only use this one prefix instead of writing four to cover all:var testEl = document.createElement(‘div’);$.each(vendors, function(vendor, event) { if (testEl.style[vendor + ‘TransitionProperty’] !== undefined) { prefix = ‘-‘ + downcase(vendor) + ‘-‘; eventPrefix = event; return false; }});
how to use jQuery’s animation?
- 四个参数:properties(object), duration(1000), ease(string), callback2. 两个参数:properties(object), 其他设置参数(object), 比如duration,epecialEasing,complete……
how to test if an argument is plain object or not?
var a = [], d = document, o = {};typeof a; //objecttypeof b; //objecttypeof c; //objectjQuery.isPlainObject( a ); //falsejQuery.isPlainObject( b ); //falsejQuery.isPlainObject( c ); //true
tweenMax’s fromto?
TweenLite.fromTo(xx, 1.5, {width:0, height:0}, {width:100, height:200});
how to stop setInterval?
with clearInterval:var nameOfInterval = setInterval(function(){ if (xxx) { clearInterval(nameOfInterval); }, 100);
what to pay attention on tablet and mobile?
- notouch(roll over), -webkit-tap-highlight-color: rgba(0,0,0,0); * mobile横屏文字变大禁止:-webkit-text-size-adjust: none; * 视频不要太大,宽高,不然播放不了
String.replace?
“someString”.replace(/(xx)(yy)/, function(match, p1, p2, offset, string) { return [p1, p2].join(“ “);}match: 符合搜索条件的字段p1: 第一个括号里的符合字段p2: 第二个括号里的符合字段offset: p1的index numberstring: 整个string
用事件传送数据:
//set an anchor for future event binds (in order to transmit cross page data)window.anchor = {};$(anchor).on(“transmit”, function(){ return {};});$(anchor).trigger(“transmit”);或者这个锚点就可以直接用$(‘html’),避免冲突,而且哪里都能取到。
requirejs grunt build, what would be compressed, and what would not?
只有写在main的define里,才会被压缩进来,在main里require的,文件会单独载入。
tweenMax有时候会闪一下
可能是因为某个动画设置了delay
what to be careful about with for..in loop?
- 用在object上:有可能会把继承来的property也iterate出来,所以若想只用这个object自己的property,就用hasOwnProperty。2. 它的iterate顺序是随机的,所以,用在array上可能会有很神奇的效果。array用forEach(e, i, a)for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for…in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited. Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.forEach or the non-standard for…of loop) when iterating over arrays where the order of access is important.