自己寫了一個圖片的馬賽克消失效果(jQuery)


其中的一個效果:

html代碼:

<h1>單擊圖片,產生效果</h1>
    <div class="box"></div>

插件代碼:

 1 ; (function ($) {
 2             var defaults = {
 3                 ani: 4, //動畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
 4                 delay: 3000, //動畫執行時間
 5                 url:"0",//圖片路徑
 6                 count: [20, 20]//馬賽克水平數量,豎直方向數量;數量不能過多,否則計算量太大,計算機執行不了,導致瀏覽器卡死
 7             }
 8             $.fn.gysMaSaiKe = function (opt) {            
 9                 opt = $.extend({}, defaults, opt);
10                 if(opt.url=="0"){alert("沒有填寫圖片路徑參數");return;}
11                 var obj = $(this);                               
12                 if (obj.css("position") == "static") obj.css({ "position": "relative" });
13                 obj.css("overflow","hidden");
14                 var objWidth = obj.width();
15                 var objHeight = obj.height();
16                 (function (count,url, obj) {
17                     var littleBoxWidth = Math.floor(objWidth / count[0]);
18                     var littleBoxHeight = Math.floor(objHeight / count[1]);                   
19                     var html = "";
20                     var littleBoxLeft = littleBoxWidth * (-1), littleBoxTop = littleBoxHeight * (-1);
21 
22                     for (var i = 0; i < count[1]; i++) {//
23                         littleBoxTop += littleBoxHeight;
24                         for (var j = 0; j < count[0]; j++) {//每一行中的單個span
25                             littleBoxLeft += littleBoxWidth;
26                             html += "<span style='display:block;position:absolute;left:" + littleBoxLeft + "px;top:" + littleBoxTop + "px;width:" + littleBoxWidth + "px; height:" + littleBoxHeight + "px; background-image:url("+url+");background-position:" + (littleBoxLeft) * (-1) + "px " + (littleBoxTop) * (-1) + "px;'></span>";
27                         }
28                         littleBoxLeft = littleBoxWidth * (-1);
29                     }
30                     obj.html(html);
31                 })(opt.count,opt.url,obj);
32 
33                 var animation = function (ani, delay, objs) {
34                     var res = function () { }
35                     if (ani == 1) {//馬賽克向中間聚攏
36                         res = function () {
37                             objs.animate({ top: objHeight / 2, left: objWidth / 2, opacity: 0 }, delay);
38                             setTimeout(function(){obj.html("");},delay);
39                         }
40                     }
41                     else if (ani == 2) {//碎片向左上角聚攏消失
42                         res = function () {
43                             objs.animate({ left: 0, top: 0, opacity: 0 }, delay); setTimeout(function () { obj.html(""); }, delay);
44                         }
45                     }
46                     else if (ani == 3) {//拉扯消失
47                         res = function () {
48                             objs.filter(":even").animate({top:-100,left:-100},delay);
49                             objs.filter(":odd").animate({ top: -100, left:900}, delay);   setTimeout(function(){obj.html("");},delay);                         
50                         }
51                     }
52                     else if (ani == 4) {//
53                         res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
54                     }
55                     else {
56                         res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
57                     }
58                     return res;
59                 } (opt.ani, opt.delay, obj.children());
60 
61                 obj.on("click", "span", animation);
62             }
63         })(jQuery);

css代碼:

1   .box { width: 1000px; height:600px;}

插件的調用:

1  $(function () {
2             $(".box").gysMaSaiKe({
3                 count: [10, 15], //馬賽克水平數量,豎直方向數量;數量不能過多,否則計算量太大,計算機執行不了,導致瀏覽器卡死
4                 ani: 4, //動畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
5                 delay: 5000, //動畫執行時間
6                 url: "1.jpg" //圖片路徑
7               });
8         });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM