所谓的“图片预加载”就是先加载一张不清晰的图片,然后加载一张高清图片,实现由模糊到清晰的渐变。防止页面空白影响用户体验。具体效果请看在线演示。
原理其实很简单,使用两张图片实现:一张小的缩略图,一张大的高清图,将缩略图拉大到高清图的尺寸(这就是模糊的效果),然后加载高清图片,加载完成修改<img>标签的src。
html代码如下:
- <img src="img_s.jpg" style="width: 409px; height: 307px;" />
js代码如下:
- var img = new Image();
- $(img).load(function () {
- $('img').attr('src', 'img.jpg');
- });
- img.src = 'img.jpg';
上面是核心代码。至于进度条等代码,可以右键查看在线演示的源代码。
2 | * 带滚动条的图片预加载效果 |
03 | * ======================================= |
04 | * @author keenwon |
05 | * Full source at http://keenwon.com |
06 | */ |
07 |
08 | ( function () { |
09 | var timing = function () { |
10 | this ._timer = null ; |
11 | this ._percent = 0; |
12 | this ._isLoading = false ; |
13 | this ._isPause = false ; |
14 | this ._step = 2; |
15 | }; |
16 |
17 | timing.prototype = { |
18 | start: function (url, obj) { |
19 | var self = this ; |
20 | self._percent = 0; |
21 |
22 | self._loadImg(url); |
23 |
24 | self._timer = setInterval( function () { |
25 | self._judge(url, obj); |
26 | self._scroll(); |
27 | }, 30); |
28 | }, |
29 | _pause: function () { |
30 | this ._isPause = true ; |
31 | this ._step = 0; |
32 | }, |
33 | _restart: function () { |
34 | this ._isPause = false ; |
35 | this ._step = 3; |
36 | }, |
37 | _scroll: function () { |
38 | $( '.loading-progress' ).width( this ._percent + "%" ); |
39 | this ._percent += this ._step; |
40 | }, |
41 | _loadImg: function (url) { |
42 | var self = this ; |
43 | var img = new Image(); |
44 | $(img).load( function () { |
45 | setTimeout( function () { //模拟加载 |
46 | self._isLoading = true ; |
47 | }, 2000); |
48 | }); |
49 | img.src = url; |
50 | }, |
51 | _judge: function (url, obj) { |
52 | if ( this ._percent >= 70 && ! this ._isLoading) { |
53 | this ._pause(); |
54 | } |
55 | if ( this ._percent >= 70 && this ._isLoading && this ._isPause) { |
56 | this ._restart(); |
57 | } |
58 | if ( this ._percent >= 100) { |
59 | obj.attr( "src" , url); |
60 | this .destroy(); |
61 | } |
62 | }, |
63 | destroy: function () { |
64 | this ._percent = 0; |
65 | clearInterval( this ._timer); |
66 | $( '.loading' ).hide(); |
67 | } |
68 | } |
69 |
70 | if ( typeof window.timing == 'undefined' || window.timing == null ) { |
71 | window.timing = new timing(); |
72 | } |
73 | })(); |
74 |
75 | $( function () { |
76 | timing.start( '../img/load-image.jpg' , $( "img" ).eq(0)); |
77 | }); |
[代码][HTML]代码 跳至 [1] [全屏预览]
01 | <!DOCTYPE html> |
02 | < html > |
03 | < head > |
04 | < title >Voicewo jQuery Plugin Demo</ title > |
05 | < link rel = "stylesheet" type = "text/css" href = "voicewo.css" > |
06 | < script src = "jquery-1.11.0.min.js" type = "text/javascript" ></ script > |
07 | < script src = "jquery.voicewo.js" type = "text/javascript" ></ script > |
08 | < script type = "text/javascript" > |
09 | $(document).ready(function(){ |
10 | $('#btn').voicewo({ |
11 | 'width' : 92, |
12 | 'height' : 25, |
13 | 'outputId' : 'txt', |
14 | 'buttonImage' : 'btn.png', |
15 | 'swf' : 'voicewo.swf' |
16 | }); |
17 | }) |
18 | </ script > |
19 | </ head > |
20 | < body > |
21 | < textarea id = "txt" cols = "60" name = "txt" rows = "6" ></ textarea > |
22 | < div id = "btn" ></ div > |
23 | </ body > |
24 | </ html > |