先来看看使用方法。
演示地址 http://demo./js/jCutter_jquery/demo.htm
HTML文件中这样写:
复制代码 代码如下:
<div class="jcutter">
<img src="1.jpg" alt="">
<div class="jcutter-content">
这是点开后的页面的内容
</div>
</div>
调用的话需要这样写:
复制代码 代码如下:
$(document).ready(function(){
options={
'speedIn':600, //图片进入时候的动画速度
'speedOut':400, //图片退出时候的动画速度
'easeIn':'easeOutBounce', //图片进入时候的动画效果,这个效果需要easing库
'easeOut':'' //图片退出时候的动画效果
}
$('.jcutter').jCutter(options);
})
当然要引用这个插件才行。下面我们来讲解这个插件的编写。
一、jQuery插件编写的方法
写一个jQuery插件,首先需要一些必要的结构,如下所示:
复制代码 代码如下:
(function($){
$.fn.jCutter = function(o){
o = $.extend({
speedIn: 300,
speedOut: 300,
easeIn: '',
easeOut: ''
}, o || {});
};
})(jQuery);
这个结构和我们最终的结果有些出入,但是大体上jQuery插件的结构就是如此。
首先要写成一个闭包的形式,不污染命名空间,然后根据jQuery提供的接口来编写,这里的jCutter可以改成你自己插件的名字。$.extend是一个非常有趣的函数,他会将第一个和第二个参数合并,对于两个参数中都出现的值,用后者代替前者。
二、开始编写
在这个例子中,因为要用到选择器,所以我们做一些修改,结构改成如下的样子。
复制代码 代码如下:
(function($){
$.jCutter = function(node, o){
o = $.extend({
speedIn: 300,
speedOut: 300,
easeIn: '',
easeOut: ''
}, o || {});
var that = this;
that.init = function(){
};
that.generate = function(){
};
that.cutter = function(){
};
that.init();
};
$.fn.jCutter = function(o){
return this.each(function(i){
$.jCutter(this,o);
});
};
})(jQuery);
$.jCutter的含义是给jQuery添加一个类,这样我们操作起来方便一些。通过上面的结构我们可以清楚的看到程序的逻辑,init()用来进行一些初始化的任务,然后用generate()来生成需要的结构,最后用cutter()来完成动画和事件效果。
三、初始化程序
需要初始化的东西主要是一些参数,然后找到需要进行修改的图片,最后进行渲染。都是一些比较简单的操作。
复制代码 代码如下:
that.init = function(){
that.node = $(node);
that.img = that.node.find('img');
that.speedIn = o.speedIn;
that.speedOut = o.speedOut;
that.easeIn = o.easeIn;
that.easeOut = o.easeOut;
that.generate();
that.cutter();
};
四、生成需要的结构
这个效果的原理就是:我们把图片复制到四个层里面,然后将这四个层相对定位,再把这个图拼起来,这样动画效果就能达到了。
复制代码 代码如下:
that.generate = function(){
var w = that.node.width() / 2;
var h = that.node.height() / 2;
that.imga = [];
for (var i = 0; i < 4; i++) {
that.imga[i] = document.createElement('div');
that.imga[i] = $(that.imga[i]);
that.imga[i].css({
'position': 'absolute',
'z-index': '2',
'width': w,
'height': h,
'background': 'url("' + that.img.attr("src") + '") no-repeat'
});
$(that.node).append(that.imga[i]);
}
that.imga[0].css({
'left': '0px',
'top': '0px'
});
that.imga[1].css({
'right': '0px',
'top': '0px',
'background-position': '-' + w + 'px' + ' 0px'
});
that.imga[2].css({
'left': '0px',
'bottom': '0px',
'background-position': '0px' + ' -' + h + 'px'
});
that.imga[3].css({
'right': '0px',
'bottom': '0px',
'background-position': '-' + w + 'px ' + '-' + h + 'px'
});
that.img.remove();
};
这里的代码也比较简单,首先得到外面层的宽度和高度,然后计算,再生成四个层,给四个层写入相应的位置代码,需要注意的是,外面层的position属性要设置为relative,要么里面的层就无法准确定位了。这里其实可以直接写入相应的html代码,但是为了表现清晰,我们采用了比较明朗的写法,先生成一个div,然后赋给他一些css属性。
五、添加动画效果,注册事件处理程序
完成了结构的任务,下一步就是给他添加动画效果了,我们只需要将这四个图层在鼠标经过的时候移出外面的层,然鼠标离开的时候再复位就可以了,写起来也是非常的简单,看代码:
复制代码 代码如下:
that.cutter = function(){
var w = that.node.width() / 2;
var h = that.node.height() / 2;
that.node.hover(function(){
that.imga[0].stop().animate({
'left': '-' + w,
'top': '-' + h
}, that.speedOut, that.easeOut);
that.imga[1].stop().animate({
'right': '-' + w,
'top': '-' + h
}, that.speedOut, that.easeOut);
that.imga[2].stop().animate({
'left': '-' + w,
'bottom': '-' + h
}, that.speedOut, that.easeOut);
that.imga[3].stop().animate({
'right': '-' + w,
'bottom': '-' + h
}, that.speedOut, that.easeOut);
}, function(){
that.imga[0].stop().animate({
'left': 0,
'top': 0
}, that.speedIn, that.easeIn);
that.imga[1].stop().animate({
'right': 0,
'top': 0
}, that.speedIn, that.easeIn);
that.imga[2].stop().animate({
'left': 0,
'bottom': 0
}, that.speedIn, that.easeIn);
that.imga[3].stop().animate({
'right': 0,
'bottom': 0
}, that.speedIn, that.easeIn);
})
};
.stop()函数的作用就是如果在事件再次出发的时候,上一次的动画还在进行中的话,就终止动画,这样效果更加自然一些。that.easeIn和that.easeOut参数是用来设置动画的模式的,默认的jQuery库只有两种简单的线性库,可以下载jQuery.easing库来添加更多绚丽的效果。
这样就完成了这个插件的编写,完整的代码如下:
复制代码 代码如下:
(function($){
$.jCutter = function(node, o){
o = $.extend({
speedIn: 300,
speedOut: 300,
easeIn: '',
easeOut: ''
}, o || {});
var that = this;
that.init = function(){
that.node = $(node);
that.img = that.node.find('img');
that.speedIn = o.speedIn;
that.speedOut = o.speedOut;
that.easeIn = o.easeIn;
that.easeOut = o.easeOut;
that.generate();
that.cutter();
};
that.generate = function(){
var w = that.node.width() / 2;
var h = that.node.height() / 2;
that.imga = [];
for (var i = 0; i < 4; i++) {
that.imga[i] = document.createElement('div');
that.imga[i] = $(that.imga[i]);
that.imga[i].css({
'position': 'absolute',
'z-index': '2',
'width': w,
'height': h,
'background': 'url("' + that.img.attr("src") + '") no-repeat'
});
$(that.node).append(that.imga[i]);
}
that.imga[0].css({
'left': '0px',
'top': '0px'
});
that.imga[1].css({
'right': '0px',
'top': '0px',
'background-position': '-' + w + 'px' + ' 0px'
});
that.imga[2].css({
'left': '0px',
'bottom': '0px',
'background-position': '0px' + ' -' + h + 'px'
});
that.imga[3].css({
'right': '0px',
'bottom': '0px',
'background-position': '-' + w + 'px ' + '-' + h + 'px'
});
that.img.remove();
};
that.cutter = function(){
var w = that.node.width() / 2;
var h = that.node.height() / 2;
that.node.hover(function(){
that.imga[0].stop().animate({
'left': '-' + w,
'top': '-' + h
}, that.speedOut, that.easeOut);
that.imga[1].stop().animate({
'right': '-' + w,
'top': '-' + h
}, that.speedOut, that.easeOut);
that.imga[2].stop().animate({
'left': '-' + w,
'bottom': '-' + h
}, that.speedOut, that.easeOut);
that.imga[3].stop().animate({
'right': '-' + w,
'bottom': '-' + h
}, that.speedOut, that.easeOut);
}, function(){
that.imga[0].stop().animate({
'left': 0,
'top': 0
}, that.speedIn, that.easeIn);
that.imga[1].stop().animate({
'right': 0,
'top': 0
}, that.speedIn, that.easeIn);
that.imga[2].stop().animate({
'left': 0,
'bottom': 0
}, that.speedIn, that.easeIn);
that.imga[3].stop().animate({
'right': 0,
'bottom': 0
}, that.speedIn, that.easeIn);
})
};
that.init();
};
$.fn.jCutter = function(o){
return this.each(function(i){
$.jCutter(this,o);
});
};
})(jQuery);
很简单有趣的效果,逻辑很清楚,代码也简单,是练手的好东东。
打包下载地址 https://www./jiaoben/26031.html
相关推荐:
AI网页生成:轻松构建智能网站,提升品牌竞争力,杭州专业ai智能教育
ChatGPT目前,我无法查看或打开附件,但我依然能为你提供全面的帮助,苹果ai谷歌ai
Chato1免费么?揭开这款AI聊天机器人的神秘面纱,交个朋友ai写作
seo网赚什么意思,网站seo赚钱 ,ai打不开ai
AI热门软件,未来科技的钥匙
为什么“未备案域名”会成为互联网行业中的重要问题?,江干区seo优化价格
ChatGPT宕机两小时,OpenAI紧急修复,用户期待AI恢复正常服务,oppo小布ai
ChatGPT崩了?用户称打开是一片空白,背后隐藏了什么?,ai 新技巧
AI搜索写文章是什么意思?人工智能赋能内容创作的未来,高德地图 ai
SEO分类:从基础到进阶,全面解析SEO优化的关键要素,新建设网站排名
ChatGPT为什么访问不了?全面解析及解决方案,ai首页设计
SEO有点:揭秘优化之道,提升网站排名的秘诀,广州抖音seo厂家地址
ChatGPT的诞生,预示着人工智能大规模应用的时代已经来临,ai此生不渝
在线AI文章生成:内容创作新革命
OpenAI推出的GPT-4Turbo大幅降低了AI应用成本,推动了AI技术的普及化,ai*版
SEO观看:如何通过优化提升您的网站流量和品牌影响力,龙里网络营销推广
SEO合作:让你的网站在搜索引擎中脱颖而出,惠州抖音seo策划公司
求一个AI软件,彻底改变你的工作与生活!
什么是SEO优化方案,seo的优化方案 ,ai emorobot
什么是seo发外链,seo外链类型有哪些 ,小小苏ai
AI撰写大数据解决方案:开启智能数据时代的新篇章,ai头号公敌
优排软件:高效管理新天地,轻松提升工作效率,长葛外贸网站建设
WPS改写-轻松提升文档创作效率的秘密武器,推广网站的优势
SEO表格:优化网站排名的秘密武器,大数据推广营销费用多少
seo管理系统是什么,seo网站管理 ,过度圆ai
什么是“神马关键词”?如何用它提升你的网络营销效果?,新闻发布网站怎么做推广
文章AI排版,让创作更高效的秘密武器
SEO排序:如何让你的网页在搜索引擎中脱颖而出,平谷抖音seo推广招聘
什么是seo网络推广,seo网络推广技术 ,ai写真官方
SEO联系:如何通过SEO优化提升您的网络营销效果,企业营销推广获客
SEO好吗?助力网站成功的关键之道,网站优化方案范文怎么写
SEO架构:提升网站流量的核心策略,东营南江全平台营销推广
AI+写文章:开启智能创作新时代
seo能解决什么问题,seo会遇到哪些问题 ,挚爱花嫁ai
GPT人工智能-让未来触手可及的智慧之光,ai头花写真
seo适合看什么书,自学seo书籍推荐 ,ai少女 刻晴
打造高效创作体验,写文章AI软件重塑内容生产力
seo矩阵运营中心是什么,seo矩阵运营中心是什么意思啊 ,北京ai特效
“洗稿技巧如何让你的文章脱颖而出,轻松提升内容创作水平!”,台州椒江seo企业排名
ChatGPT5.0为什么一直没出来?背后的技术与战略,元宵节ai趣赏月
ChatGPT破解版:让人工智能助力你的工作与生活,ai cos帽
AI会生成同一篇文章吗?揭开智能创作的神秘面纱
优化服务网-提升客户体验,打造全方位智慧服务平台,东莞网站建设员招聘信息
ChatGPT全球宕机:人工智能的崩塌与未来的挑战,中国ai和美国ai教父
“赞片CMS”-让你的*站点管理更高效、便捷,日照seo平台
SEO代做:让你的企业轻松登顶搜索引擎,快速提升曝光率,seo 提高注册量
SEO怎么排名?这5大技巧,轻松提升网站排名,人人都可ai
SEO首选:如何通过优化网站提升排名和流量,简单的网站优化软件
ChatGPT:引领智能对话新潮流,助力未来科技,ai写作免费议论文
ChatGPT翻译打不开?解决方法!,ai爱股票