自己用jQuery寫了一個(gè)圖片的馬賽克消失效果
更新時(shí)間:2014年05月04日 15:06:16 作者:
這篇文章主要介紹的是自己用jQuery寫了一個(gè)圖片的馬賽克消失效果實(shí)現(xiàn)過程,需要的朋友可以參考下
其中的一個(gè)效果:
html代碼:
<h1>單擊圖片,產(chǎn)生效果</h1>
<div class="box"></div>
插件代碼:
; (function ($) {
var defaults = {
ani: 4, //動(dòng)畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
delay: 3000, //動(dòng)畫執(zhí)行時(shí)間
url:"0",//圖片路徑
count: [20, 20]//馬賽克水平數(shù)量,豎直方向數(shù)量;數(shù)量不能過多,否則計(jì)算量太大,計(jì)算機(jī)執(zhí)行不了,導(dǎo)致瀏覽器卡死
}
$.fn.gysMaSaiKe = function (opt) {
opt = $.extend({}, defaults, opt);
if(opt.url=="0"){alert("沒有填寫圖片路徑參數(shù)");return;}
var obj = $(this);
if (obj.css("position") == "static") obj.css({ "position": "relative" });
obj.css("overflow","hidden");
var objWidth = obj.width();
var objHeight = obj.height();
(function (count,url, obj) {
var littleBoxWidth = Math.floor(objWidth / count[0]);
var littleBoxHeight = Math.floor(objHeight / count[1]);
var html = "";
var littleBoxLeft = littleBoxWidth * (-1), littleBoxTop = littleBoxHeight * (-1);
for (var i = 0; i < count[1]; i++) {//行
littleBoxTop += littleBoxHeight;
for (var j = 0; j < count[0]; j++) {//每一行中的單個(gè)span
littleBoxLeft += littleBoxWidth;
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>";
}
littleBoxLeft = littleBoxWidth * (-1);
}
obj.html(html);
})(opt.count,opt.url,obj);
var animation = function (ani, delay, objs) {
var res = function () { }
if (ani == 1) {//馬賽克向中間聚攏
res = function () {
objs.animate({ top: objHeight / 2, left: objWidth / 2, opacity: 0 }, delay);
setTimeout(function(){obj.html("");},delay);
}
}
else if (ani == 2) {//碎片向左上角聚攏消失
res = function () {
objs.animate({ left: 0, top: 0, opacity: 0 }, delay); setTimeout(function () { obj.html(""); }, delay);
}
}
else if (ani == 3) {//拉扯消失
res = function () {
objs.filter(":even").animate({top:-100,left:-100},delay);
objs.filter(":odd").animate({ top: -100, left:900}, delay); setTimeout(function(){obj.html("");},delay);
}
}
else if (ani == 4) {//
res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
}
else {
res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
}
return res;
} (opt.ani, opt.delay, obj.children());
obj.on("click", "span", animation);
}
})(jQuery);
css代碼:
.box { width: 1000px; height:600px;}
插件的調(diào)用:
$(function () {
$(".box").gysMaSaiKe({
count: [10, 15], //馬賽克水平數(shù)量,豎直方向數(shù)量;數(shù)量不能過多,否則計(jì)算量太大,計(jì)算機(jī)執(zhí)行不了,導(dǎo)致瀏覽器卡死
ani: 4, //動(dòng)畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
delay: 5000, //動(dòng)畫執(zhí)行時(shí)間
url: "1.jpg" //圖片路徑
});
});
html代碼:
復(fù)制代碼 代碼如下:
<h1>單擊圖片,產(chǎn)生效果</h1>
<div class="box"></div>
插件代碼:
復(fù)制代碼 代碼如下:
; (function ($) {
var defaults = {
ani: 4, //動(dòng)畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
delay: 3000, //動(dòng)畫執(zhí)行時(shí)間
url:"0",//圖片路徑
count: [20, 20]//馬賽克水平數(shù)量,豎直方向數(shù)量;數(shù)量不能過多,否則計(jì)算量太大,計(jì)算機(jī)執(zhí)行不了,導(dǎo)致瀏覽器卡死
}
$.fn.gysMaSaiKe = function (opt) {
opt = $.extend({}, defaults, opt);
if(opt.url=="0"){alert("沒有填寫圖片路徑參數(shù)");return;}
var obj = $(this);
if (obj.css("position") == "static") obj.css({ "position": "relative" });
obj.css("overflow","hidden");
var objWidth = obj.width();
var objHeight = obj.height();
(function (count,url, obj) {
var littleBoxWidth = Math.floor(objWidth / count[0]);
var littleBoxHeight = Math.floor(objHeight / count[1]);
var html = "";
var littleBoxLeft = littleBoxWidth * (-1), littleBoxTop = littleBoxHeight * (-1);
for (var i = 0; i < count[1]; i++) {//行
littleBoxTop += littleBoxHeight;
for (var j = 0; j < count[0]; j++) {//每一行中的單個(gè)span
littleBoxLeft += littleBoxWidth;
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>";
}
littleBoxLeft = littleBoxWidth * (-1);
}
obj.html(html);
})(opt.count,opt.url,obj);
var animation = function (ani, delay, objs) {
var res = function () { }
if (ani == 1) {//馬賽克向中間聚攏
res = function () {
objs.animate({ top: objHeight / 2, left: objWidth / 2, opacity: 0 }, delay);
setTimeout(function(){obj.html("");},delay);
}
}
else if (ani == 2) {//碎片向左上角聚攏消失
res = function () {
objs.animate({ left: 0, top: 0, opacity: 0 }, delay); setTimeout(function () { obj.html(""); }, delay);
}
}
else if (ani == 3) {//拉扯消失
res = function () {
objs.filter(":even").animate({top:-100,left:-100},delay);
objs.filter(":odd").animate({ top: -100, left:900}, delay); setTimeout(function(){obj.html("");},delay);
}
}
else if (ani == 4) {//
res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
}
else {
res = function () { objs.animate({ height: 0, width: 0 }, delay);setTimeout(function(){obj.html("");},delay); }
}
return res;
} (opt.ani, opt.delay, obj.children());
obj.on("click", "span", animation);
}
})(jQuery);
css代碼:
復(fù)制代碼 代碼如下:
.box { width: 1000px; height:600px;}
插件的調(diào)用:
復(fù)制代碼 代碼如下:
$(function () {
$(".box").gysMaSaiKe({
count: [10, 15], //馬賽克水平數(shù)量,豎直方向數(shù)量;數(shù)量不能過多,否則計(jì)算量太大,計(jì)算機(jī)執(zhí)行不了,導(dǎo)致瀏覽器卡死
ani: 4, //動(dòng)畫效果.1.馬賽克向中間聚攏,2.馬賽克左上角聚攏,3.馬賽克拉扯消失,4.原地縮小
delay: 5000, //動(dòng)畫執(zhí)行時(shí)間
url: "1.jpg" //圖片路徑
});
});
您可能感興趣的文章:
- JS實(shí)現(xiàn)馬賽克圖片效果完整示例
- C#對(duì)圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)代碼
- java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化
- Java實(shí)現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法示例
- php實(shí)現(xiàn)圖片局部打馬賽克的方法
- php實(shí)現(xiàn)圖片添加描邊字和馬賽克的方法
- 用javascript實(shí)現(xiàn)的圖片馬賽克后顯示并切換加文字功能
- 用javascript實(shí)現(xiàn)圖片馬賽克后顯示并切換
- three.js 將圖片馬賽克化的示例代碼
相關(guān)文章
jQuery ajaxSubmit 實(shí)現(xiàn)ajax提交表單局部刷新
這篇文章主要介紹了jQuery ajaxSubmit 實(shí)現(xiàn)ajax提交表單局部刷新 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
基于jquery打造的百分比動(dòng)態(tài)色彩條插件
主要是為了往后的項(xiàng)目中方便實(shí)現(xiàn)這類型的功能,在之前做問卷調(diào)查那個(gè)應(yīng)用中,就用到這個(gè)來顯示結(jié)果,但當(dāng)時(shí)開發(fā)時(shí)并不用是插件的,一大堆代碼,看也煩,用起來很麻煩2012-09-09
JQuery中html()方法使用不當(dāng)帶來的陷阱
html方法當(dāng)不傳參數(shù)時(shí)用來獲取元素的html內(nèi)容2011-04-04
網(wǎng)頁下載文件期間如何防止用戶對(duì)網(wǎng)頁進(jìn)行其他操作
網(wǎng)頁下載文件時(shí)需要一段時(shí)間,在這期間如何防止用戶對(duì)網(wǎng)頁進(jìn)行其他操作,將div覆蓋在網(wǎng)頁上,將網(wǎng)頁鎖住,具體實(shí)現(xiàn)如下2014-06-06
淺談如何實(shí)現(xiàn)easyui的datebox格式化
這篇文章主要和大家一起聊一聊如何實(shí)現(xiàn)easyui的datebox格式化的方法,感興趣的小伙伴們可以參考一下2016-06-06
jQuery加PHP實(shí)現(xiàn)圖片上傳并提交的示例代碼
這篇文章主要介紹了jQuery加PHP實(shí)現(xiàn)圖片上傳并提交的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
如何書寫高質(zhì)量jQuery代碼(使用jquery性能問題)
眾所周知,jQuery現(xiàn)在已經(jīng)非常流行,百度新首頁中也已經(jīng)開始使用jQuery,今天總結(jié)下怎么書寫更好的jQuery代碼使jQuery代碼更好、更快的執(zhí)行,希望本篇jQuery教程一改大家以前不合理的做法2014-06-06

