-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
678 lines (442 loc) · 36.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>mackxu的博客</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="mackxu的博客">
<meta property="og:url" content="http://mackxu.github.io/index.html">
<meta property="og:site_name" content="mackxu的博客">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mackxu的博客">
<meta name="twitter:description">
<link rel="alternative" href="/atom.xml" title="mackxu的博客" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link rel="stylesheet" href="/css/style.css" type="text/css">
</head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">mackxu的博客</a>
</h1>
<h2 id="subtitle-wrap">
<a href="/" id="subtitle">学而时习之不亦说乎</a>
</h2>
</div>
<div id="header-inner" class="inner">
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"></a>
<a class="main-nav-link" href="/">Home</a>
<a class="main-nav-link" href="/archives">Archives</a>
</nav>
<nav id="sub-nav">
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
<a id="nav-search-btn" class="nav-icon" title="Search"></a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://mackxu.github.io"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="post-js-pattern-strategy2" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/04/04/js-pattern-strategy2/" class="article-date">
<time datetime="2016-04-04T03:49:58.000Z" itemprop="datePublished">Apr 4 2016</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/04/04/js-pattern-strategy2/">JS设计模式之策略模式(二)</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>利用策略模式来校验用户是否输入了合法的数据<br>
<p class="article-more-link">
<a href="/2016/04/04/js-pattern-strategy2/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2016/04/04/js-pattern-strategy2/" data-id="cimlhivl40011fyzq9izysf3v" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/js/">js</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/pattern/">pattern</a></li></ul>
</footer>
</div>
</article>
<article id="post-js-pattern-strategy" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/04/04/js-pattern-strategy/" class="article-date">
<time datetime="2016-04-04T00:20:38.000Z" itemprop="datePublished">Apr 4 2016</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/04/04/js-pattern-strategy/">JS设计模式之策略模式</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>策略模式定义:定义一系列的算法,把他们一个个封装起来,并且使它们可以相互交换<br>
<p class="article-more-link">
<a href="/2016/04/04/js-pattern-strategy/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2016/04/04/js-pattern-strategy/" data-id="cimlhic5t000nfyzq4x65ualh" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/js/">js</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/pattern/">pattern</a></li></ul>
</footer>
</div>
</article>
<article id="post-transition-display" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/01/17/transition-display/" class="article-date">
<time datetime="2016-01-17T02:56:51.000Z" itemprop="datePublished">Jan 17 2016</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/01/17/transition-display/">当transition遇到display</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>实现淡入淡出时,可以使用jQuery的fadeIn()/fadeOut()。使用css3的transition(过渡)也能实现。<br>现在我们借助transition模仿jQuery实现盒子的淡入淡出:<br>通过button的click事件,给div#box添加或删除<code>classname</code>实现效果<br>HTML结构:</p>
<pre><code><button>动画按钮</button>
<<span class="keyword">div</span> <span class="type">class</span>=<span class="string">"box"</span> <span class="property">id</span>=<span class="string">"box"</span>></<span class="keyword">div</span>>
</code></pre><h2 id="过渡改变opacity属性值">过渡改变opacity属性值</h2><p>css代码:</p>
<pre><code><span class="class">.box</span> <span class="rules">{
<span class="rule"><span class="attribute">background-color</span>:<span class="value"> goldenrod</span></span>;
<span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">300px</span></span></span>; <span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">200px</span></span></span>;
<span class="rule"><span class="attribute">margin</span>:<span class="value"> <span class="number">100px</span> auto</span></span>;
<span class="rule"><span class="attribute">transition</span>:<span class="value"> all <span class="number">1s</span> linear</span></span>;
}</span>
<span class="class">.hide</span> <span class="rules">{
<span class="comment">/* display: none; */</span>
<span class="rule"><span class="attribute">opacity</span>:<span class="value"> <span class="number">0</span>
}</span></span></span>
</code></pre><p>JS代码:</p>
<pre><code>var <span class="variable">$box</span> = $(<span class="string">'#box'</span>);
$(<span class="string">'button'</span>).on(<span class="string">'click'</span>, <span class="keyword">function</span>() {
if(<span class="variable">$box</span>.hasClass(<span class="string">'hide'</span>)) {
<span class="variable">$box</span>.removeClass(<span class="string">'hide'</span>);
}else {
<span class="variable">$box</span>.addClass(<span class="string">'hide'</span>);
}
});
</code></pre><p>在点击按钮后可以看到淡入淡出效果,但是在css代码中解除对display属性注释后,在刷新页面,淡出淡入效果没有了。究其原因在于transition不支持display的none值与其它值之间的过渡,进而影响到盒子对opacity的过渡效果。<br>我们可以按照下面的步骤,实现效果: </p>
<ul>
<li>当盒子可见时,先实现opacity动画,动画结束后添加<code>display: none</code></li>
<li>当盒子不可见时,首先使其可见<code>display: block</code>,然后执行opacity动画。</li>
</ul>
<h2 id="解决办法">解决办法</h2><p>把<code>opacity: 0</code>从.hide抽离。<br>css代码: </p>
<pre><code><span class="class">.hide</span> <span class="rules">{ <span class="rule"><span class="attribute">display</span>:<span class="value"> none</span></span>; }</span>
<span class="class">.opacity0</span> <span class="rules">{ <span class="rule"><span class="attribute">opacity</span>:<span class="value"> <span class="number">0</span></span></span>; }</span>
</code></pre><p>JS代码如下:</p>
<pre><code>var <span class="variable">$box</span> = $(<span class="string">'#box'</span>);
$(<span class="string">'button'</span>).on(<span class="string">'click'</span>, <span class="keyword">function</span>() {
if(<span class="variable">$box</span>.hasClass(<span class="string">'hide'</span>)) {
<span class="variable">$box</span>.removeClass(<span class="string">'hide'</span>);
requestAnimationFrame(function() { // 合适时机执行淡入动画
<span class="variable">$box</span>.removeClass(<span class="string">'opacity0'</span>);
});
}else {
<span class="variable">$box</span>.addClass(<span class="string">'opacity0'</span>);
<span class="variable">$box</span>.one(<span class="string">'transitionEnd, webkitTransitionEnd'</span>, function() {
<span class="variable">$box</span>.addClass(<span class="string">'hide'</span>); // 动画结束后, 隐藏盒子
});
}
});
</code></pre><p>参考:<br><a href="http://ued.ctrip.com/blog/when-on-transition-display.html" target="_blank" rel="external">http://ued.ctrip.com/blog/when-on-transition-display.html</a><br><a href="http://www.impressivewebs.com/animate-display-block-none/" target="_blank" rel="external">http://www.impressivewebs.com/animate-display-block-none/</a><br><a href="http://oli.jp/2010/css-animatable-properties/" target="_blank" rel="external">http://oli.jp/2010/css-animatable-properties/</a></p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2016/01/17/transition-display/" data-id="cimlhic5d0003fyzqu66p8nue" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/CSS3/">CSS3</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/transition/">transition</a></li></ul>
</footer>
</div>
</article>
<article id="post-async-script" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/08/20/async-script/" class="article-date">
<time datetime="2015-08-20T02:31:33.000Z" itemprop="datePublished">Aug 20 2015</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/08/20/async-script/">比较JS文件加载:async和DOM Script</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>async与script动态加载都能使文件异步加载,本文叙述它们对页面渲染和load加载的影响方面。<br>
<p class="article-more-link">
<a href="/2015/08/20/async-script/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2015/08/20/async-script/" data-id="cimlhic62000vfyzq9cxdc3a8" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-nano-template" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/08/19/nano-template/" class="article-date">
<time datetime="2015-08-19T10:12:36.000Z" itemprop="datePublished">Aug 19 2015</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/08/19/nano-template/">templateJS解说</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>函数内巧用多行注释,实现js模板。<br>
<p class="article-more-link">
<a href="/2015/08/19/nano-template/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2015/08/19/nano-template/" data-id="cimlhic5n000ifyzqzafwin6c" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/iqiyi/">iqiyi</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/js/">js</a></li></ul>
</footer>
</div>
</article>
<article id="post-rem-simple-use" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/08/18/rem-simple-use/" class="article-date">
<time datetime="2015-08-18T13:24:59.000Z" itemprop="datePublished">Aug 18 2015</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/08/18/rem-simple-use/">rem 在项目中的简单使用</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>在项目中重度使用rem<br>
<p class="article-more-link">
<a href="/2015/08/18/rem-simple-use/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2015/08/18/rem-simple-use/" data-id="cimlhic5k000dfyzq6q0zyert" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/css/">css</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/经验/">经验</a></li></ul>
</footer>
</div>
</article>
<article id="post-image-overlay" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2014/09/04/image-overlay/" class="article-date">
<time datetime="2014-09-03T23:22:25.000Z" itemprop="datePublished">Sep 4 2014</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2014/09/04/image-overlay/">为图片添加半透明遮罩效果</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>平时为图片添加半透明遮罩效果,我的做法如下:<br>利用标签i实现背景半透明遮罩。当鼠标hover时, 提高i的背景色透明度值<code>background-color: rgba(0, 0, 0, .6)</code><br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">p</span> <span class="attribute">class</span>=<span class="value">"opacity-black-position"</span>></span><span class="tag"><<span class="title">a</span> <span class="attribute">href</span>=<span class="value">"#"</span>></span><span class="tag"><<span class="title">img</span> <span class="attribute">src</span>=<span class="value">"images/4601.jpg"</span> <span class="attribute">alt</span>=<span class="value">""</span>></span><span class="tag"><<span class="title">i</span>></span><span class="tag"></<span class="title">i</span>></span><span class="tag"></<span class="title">a</span>></span><span class="tag"></<span class="title">p</span>></span></span><br></pre></td></tr></table></figure></p>
<p>为html结构添加如下css样式:<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 利用标签i实现背景半透明遮罩, 兼容性不好 */</span></span><br><span class="line"><span class="class">.opacity-black-position</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">460px</span></span></span>; <span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">460px</span></span></span>; <span class="rule"><span class="attribute">position</span>:<span class="value"> relative</span></span>; }</span></span><br><span class="line"><span class="class">.opacity-black-position</span> <span class="tag">i</span> <span class="rules">{ </span><br><span class="line"> <span class="rule"><span class="attribute">position</span>:<span class="value"> absolute</span></span>; <span class="rule"><span class="attribute">top</span>:<span class="value"> <span class="number">0</span></span></span>; <span class="rule"><span class="attribute">right</span>:<span class="value"> <span class="number">0</span></span></span>; <span class="rule"><span class="attribute">bottom</span>:<span class="value"> <span class="number">0</span></span></span>; <span class="rule"><span class="attribute">left</span>:<span class="value"> <span class="number">0</span></span></span>; </span><br><span class="line"> <span class="rule"><span class="attribute">background-color</span>:<span class="value"> <span class="function">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span></span>; </span><br><span class="line"> <span class="rule"><span class="attribute">transition</span>:<span class="value"> background-color .<span class="number">5s</span></span></span>; </span><br><span class="line">}</span></span><br><span class="line"><span class="class">.opacity-black-position</span> <span class="rule"><span class="attribute">a</span>:<span class="value">hover i { background-color: <span class="function">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, .<span class="number">6</span>)</span></span>; }</span><br></pre></td></tr></table></figure></p>
<h2 id="利用opacity简单实现">利用opacity简单实现</h2><p>昨天看到京东商品图片上的半透明黑色遮罩,是这样实现的。 </p>
<p>当hover时设置图片父容器a的半透明度0.4<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">p</span> <span class="attribute">class</span>=<span class="value">"opacity-black"</span>></span><span class="tag"><<span class="title">a</span> <span class="attribute">href</span>=<span class="value">"#"</span>></span><span class="tag"><<span class="title">img</span> <span class="attribute">src</span>=<span class="value">"images/4601.jpg"</span> <span class="attribute">alt</span>=<span class="value">""</span>></span><span class="tag"></<span class="title">a</span>></span><span class="tag"></<span class="title">p</span>></span></span><br></pre></td></tr></table></figure></p>
<p>为html结构添加如下css样式:<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class">.opacity-black</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">460px</span></span></span>; <span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">460px</span></span></span>; <span class="rule"><span class="attribute">background-color</span>:<span class="value"> <span class="hexcolor">#000</span></span></span>; }</span></span><br><span class="line"><span class="class">.opacity-black</span> <span class="tag">a</span> <span class="rules">{ <span class="rule"><span class="attribute">transition</span>:<span class="value"> opacity .<span class="number">5s</span></span></span>; <span class="rule"><span class="attribute">opacity</span>:<span class="value"> <span class="number">1</span></span></span>; }</span></span><br><span class="line"><span class="class">.opacity-black</span> <span class="rule"><span class="attribute">a</span>:<span class="value">hover { opacity: <span class="number">0.4</span></span></span>; }</span><br></pre></td></tr></table></figure></p>
<p>通过降低自身及子标签img的透明度来显示父辈容器的背景色, 达到的效果和上面是一样一样的。</p>
<p><strong>狠狠滴点这里(演示):</strong> <a href="http://codepen.io/mackxu/pen/mJtvh" target="_blank" rel="external">http://codepen.io/mackxu/pen/mJtvh</a></p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2014/09/04/image-overlay/" data-id="cimlhic5x000rfyzqgofylcjm" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/css/">css</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/工作经验/">工作经验</a></li></ul>
</footer>
</div>
</article>
<article id="post-sequential-search" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2014/09/01/sequential-search/" class="article-date">
<time datetime="2014-09-01T09:27:21.000Z" itemprop="datePublished">Sep 1 2014</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2014/09/01/sequential-search/">顺序表查找算法及其优化</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>顺序查找算法实现如下:</p>
<pre><code>var arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">1</span>]
, sequentialSearch = function(arr, val) {
var i = <span class="number">0</span>
, len = arr.length;
<span class="keyword">for</span> ( ; i < len; i++) { <span class="comment">// 比较一次</span>
<span class="keyword">if</span> (arr[i] === val) { <span class="comment">// 比较二次</span>
<span class="keyword">return</span> i;
}
}
<span class="keyword">return</span> i; <span class="comment">// 返回len,则说明查找失败</span>
}
</code></pre><p>这里并不是足够完美, 因为每次循环时都需要对i是否越界, 即是否小于len做判断。事实上,还可以有更好的办法,设置一个哨兵, 就可以解决不需要每次让i与len做比较了。</p>
<h2 id="顺序表查找优化">顺序表查找优化</h2><pre><code><span class="keyword">var</span> arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">1</span>]
<span class="comment">// 有哨兵的顺序表查找</span>
, sequentialSearchOpt = function(arr, <span class="function"><span class="keyword">val</span>) {</span>
<span class="keyword">var</span> i = <span class="number">0</span>
, arr = arr.slice(<span class="number">0</span>) <span class="comment">// 保证函数内操作不影响外部的arr数组 </span>
, len = arr.length;
arr[len] = <span class="function"><span class="keyword">val</span>;</span> <span class="comment">// 设置arr[len]为关键字值作为哨兵</span>
<span class="comment">// arr.push(val);</span>
<span class="keyword">while</span> (arr[i] !== <span class="function"><span class="keyword">val</span>) {</span> <span class="comment">// 比较一次</span>
i++;
}
<span class="keyword">return</span> i; <span class="comment">// 返回len,则说明查找失败</span>
}
</code></pre><p>如果arr[i]中有值等于val则返回i,否则一定会在循环结束处等于val, 此时返回len值。说明数组中没有关键字等于val,查找失败。</p>
<blockquote>
<p>在测试过程中, 发现因为由于对象是按地址传递的,在函数中改变数组<code>arr[len] = val</code>会影响到外部的数组,因此需要在函数内clone一份数组出来</p>
</blockquote>
<p>这种在查找方向的尽头设置‘哨兵’免去了在查找过程中每一次比较后都要判断查找位置是否越界的小技巧,看似与原先差别不大,但在总数较多时,效率提高很大,是非常好的编程技巧。</p>
<p><strong>参考:</strong> 大话数据结构 </p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2014/09/01/sequential-search/" data-id="cimlhic5h0008fyzq0gmgaw4s" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/优化/">优化</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/算法/">算法</a></li></ul>
</footer>
</div>
</article>
<article id="post-analyticsjs" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2014/08/22/analyticsjs/" class="article-date">
<time datetime="2014-08-22T08:16:07.000Z" itemprop="datePublished">Aug 22 2014</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2014/08/22/analyticsjs/">google-analytics的使用 解析页面引入代码</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="代码整理和注释">代码整理和注释</h2><pre><code><span class="comment">// 创建ga()方法, 加载analytics.js文件</span>
<span class="comment">// a, m 作为形参,确保下面的执行不会修改外部的同名变量</span>
(function(<span class="keyword">win</span>, doc, o, <span class="keyword">g</span>, ga, a , <span class="keyword">m</span>){
<span class="keyword">win</span>['GoogleAnalyticsObject'] = ga; <span class="comment">// 向外暴露出ga对象</span>
<span class="comment">// 确保只执行一次analytics.js的插入</span>
<span class="keyword">win</span>[ga] = <span class="keyword">win</span>[ga] || function() {
(<span class="keyword">win</span>[ga].q = <span class="keyword">win</span>[ga].q || []).push(arguments)},
<span class="keyword">win</span>[ga].<span class="keyword">l</span> = 1 * new <span class="literal">Date</span>(); <span class="comment">// 把时间格式转换成数字型</span>
<span class="comment">// 动态加载analytics.js</span>
a = doc.createElement(o),
<span class="keyword">m</span> = doc.getElementsByTagName(o)[0]; <span class="comment">// script</span>
a.async = 1;
a.src = <span class="keyword">g</span>;
<span class="keyword">m</span>.parentNode.insertBefore(a, <span class="keyword">m</span>);
})(<span class="keyword">window</span>, document, 'script','<span class="comment">//www.google-analytics.com/analytics.js','ga');</span>
</code></pre><p>短短的代码, 还是蛮有货的,呵呵。 </p>
<h2 id="压缩的代码">压缩的代码</h2><pre><code><span class="list">(<span class="keyword">function</span><span class="list">(<span class="keyword">i</span>,s,o,g,r,a,m)</span><span class="collection">{i<span class="collection">['GoogleAnalyticsObject']</span>=r;i<span class="collection">[r]</span>=i<span class="collection">[r]</span>||function<span class="list">()</span><span class="collection">{<span class="list">(<span class="keyword">i</span><span class="collection">[r]</span>.q=i<span class="collection">[r]</span>.q||<span class="collection">[]</span>)</span>.push<span class="list">(<span class="keyword">arguments</span>)</span>}</span>,i<span class="collection">[r]</span>.l=1*new Date<span class="list">()</span><span class="comment">;a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');</span></span></span>
</code></pre><p>参考: <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=zh-TW" target="_blank" rel="external">https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=zh-TW</a></p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2014/08/22/analyticsjs/" data-id="cimlhic4o0000fyzqkhgigijv" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/工具/">工具</a></li></ul>
</footer>
</div>
</article>
<article id="post-array-unique" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2014/07/21/array-unique/" class="article-date">
<time datetime="2014-07-20T23:58:53.000Z" itemprop="datePublished">Jul 21 2014</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2014/07/21/array-unique/">JS数组去重比较</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>数组去重复是一个常见的需求,我们暂时考虑同类型的数组去重复。主要是理清思路和考虑执行性能。<br>
<p class="article-more-link">
<a href="/2014/07/21/array-unique/#more">Read More</a>
</p>
</div>
<footer class="article-footer">
<a data-url="http://mackxu.github.io/2014/07/21/array-unique/" data-id="cimlhic66000wfyzqnd72p7ke" class="article-share-link">Share</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/javascript/">javascript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/性能优化/">性能优化</a></li></ul>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">Tags</h3>
<div class="widget">
<ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSS3/">CSS3</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/css/">css</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/iqiyi/">iqiyi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javascript/">javascript</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js/">js</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pattern/">pattern</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transition/">transition</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/优化/">优化</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/工作经验/">工作经验</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/工具/">工具</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/性能优化/">性能优化</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/算法/">算法</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/经验/">经验</a><span class="tag-list-count">1</span></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Tag Cloud</h3>
<div class="widget tagcloud">
<a href="/tags/CSS3/" style="font-size: 10px;">CSS3</a> <a href="/tags/css/" style="font-size: 15px;">css</a> <a href="/tags/iqiyi/" style="font-size: 10px;">iqiyi</a> <a href="/tags/javascript/" style="font-size: 10px;">javascript</a> <a href="/tags/js/" style="font-size: 20px;">js</a> <a href="/tags/pattern/" style="font-size: 15px;">pattern</a> <a href="/tags/transition/" style="font-size: 10px;">transition</a> <a href="/tags/优化/" style="font-size: 10px;">优化</a> <a href="/tags/工作经验/" style="font-size: 10px;">工作经验</a> <a href="/tags/工具/" style="font-size: 10px;">工具</a> <a href="/tags/性能优化/" style="font-size: 10px;">性能优化</a> <a href="/tags/算法/" style="font-size: 10px;">算法</a> <a href="/tags/经验/" style="font-size: 10px;">经验</a>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Archives</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">April 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/08/">August 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">July 2014</a><span class="archive-list-count">1</span></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Recents</h3>
<div class="widget">
<ul>
<li>
<a href="/2016/04/04/js-pattern-strategy2/">JS设计模式之策略模式(二)</a>
</li>
<li>
<a href="/2016/04/04/js-pattern-strategy/">JS设计模式之策略模式</a>
</li>
<li>
<a href="/2016/01/17/transition-display/">当transition遇到display</a>
</li>
<li>
<a href="/2015/08/20/async-script/">比较JS文件加载:async和DOM Script</a>
</li>
<li>
<a href="/2015/08/19/nano-template/">templateJS解说</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2016 John Doe<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">Home</a>
<a href="/archives" class="mobile-nav-link">Archives</a>
</nav>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>
<script src="/js/script.js" type="text/javascript"></script>
</div>
</body>
</html>