最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于jquery的一個(gè)圖片hover的插件

 更新時(shí)間:2010年04月24日 13:34:57   作者:  
很簡(jiǎn)單有趣的效果,邏輯很清楚,代碼也簡(jiǎn)單,是練手的好東東。
先來(lái)看看使用方法。
演示地址 http://demo.jb51.net/js/jCutter_jquery/demo.htm
HTML文件中這樣寫(xiě):
復(fù)制代碼 代碼如下:

<div class="jcutter">
<img src="1.jpg" alt="">
<div class="jcutter-content">
這是點(diǎn)開(kāi)后的頁(yè)面的內(nèi)容
</div>
     </div>

調(diào)用的話需要這樣寫(xiě):
復(fù)制代碼 代碼如下:

$(document).ready(function(){
options={
'speedIn':600, //圖片進(jìn)入時(shí)候的動(dòng)畫(huà)速度
'speedOut':400, //圖片退出時(shí)候的動(dòng)畫(huà)速度
'easeIn':'easeOutBounce', //圖片進(jìn)入時(shí)候的動(dòng)畫(huà)效果,這個(gè)效果需要easing庫(kù)
'easeOut':'' //圖片退出時(shí)候的動(dòng)畫(huà)效果
}
$('.jcutter').jCutter(options);
})

當(dāng)然要引用這個(gè)插件才行。下面我們來(lái)講解這個(gè)插件的編寫(xiě)。
一、jQuery插件編寫(xiě)的方法
寫(xiě)一個(gè)jQuery插件,首先需要一些必要的結(jié)構(gòu),如下所示:
復(fù)制代碼 代碼如下:

(function($){
$.fn.jCutter = function(o){
o = $.extend({
speedIn: 300,
speedOut: 300,
easeIn: '',
easeOut: ''
}, o || {});
};
})(jQuery);

這個(gè)結(jié)構(gòu)和我們最終的結(jié)果有些出入,但是大體上jQuery插件的結(jié)構(gòu)就是如此。
首先要寫(xiě)成一個(gè)閉包的形式,不污染命名空間,然后根據(jù)jQuery提供的接口來(lái)編寫(xiě),這里的jCutter可以改成你自己插件的名字。$.extend是一個(gè)非常有趣的函數(shù),他會(huì)將第一個(gè)和第二個(gè)參數(shù)合并,對(duì)于兩個(gè)參數(shù)中都出現(xiàn)的值,用后者代替前者。
二、開(kāi)始編寫(xiě)
在這個(gè)例子中,因?yàn)橐玫竭x擇器,所以我們做一些修改,結(jié)構(gòu)改成如下的樣子。
復(fù)制代碼 代碼如下:

(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添加一個(gè)類,這樣我們操作起來(lái)方便一些。通過(guò)上面的結(jié)構(gòu)我們可以清楚的看到程序的邏輯,init()用來(lái)進(jìn)行一些初始化的任務(wù),然后用generate()來(lái)生成需要的結(jié)構(gòu),最后用cutter()來(lái)完成動(dòng)畫(huà)和事件效果。
三、初始化程序
需要初始化的東西主要是一些參數(shù),然后找到需要進(jìn)行修改的圖片,最后進(jìn)行渲染。都是一些比較簡(jiǎn)單的操作。
復(fù)制代碼 代碼如下:

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();
};

四、生成需要的結(jié)構(gòu)
這個(gè)效果的原理就是:我們把圖片復(fù)制到四個(gè)層里面,然后將這四個(gè)層相對(duì)定位,再把這個(gè)圖拼起來(lái),這樣動(dòng)畫(huà)效果就能達(dá)到了。
復(fù)制代碼 代碼如下:

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();
};

這里的代碼也比較簡(jiǎn)單,首先得到外面層的寬度和高度,然后計(jì)算,再生成四個(gè)層,給四個(gè)層寫(xiě)入相應(yīng)的位置代碼,需要注意的是,外面層的position屬性要設(shè)置為relative,要么里面的層就無(wú)法準(zhǔn)確定位了。這里其實(shí)可以直接寫(xiě)入相應(yīng)的html代碼,但是為了表現(xiàn)清晰,我們采用了比較明朗的寫(xiě)法,先生成一個(gè)div,然后賦給他一些css屬性。
五、添加動(dòng)畫(huà)效果,注冊(cè)事件處理程序
完成了結(jié)構(gòu)的任務(wù),下一步就是給他添加動(dòng)畫(huà)效果了,我們只需要將這四個(gè)圖層在鼠標(biāo)經(jīng)過(guò)的時(shí)候移出外面的層,然鼠標(biāo)離開(kāi)的時(shí)候再?gòu)?fù)位就可以了,寫(xiě)起來(lái)也是非常的簡(jiǎn)單,看代碼:
復(fù)制代碼 代碼如下:

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()函數(shù)的作用就是如果在事件再次出發(fā)的時(shí)候,上一次的動(dòng)畫(huà)還在進(jìn)行中的話,就終止動(dòng)畫(huà),這樣效果更加自然一些。that.easeIn和that.easeOut參數(shù)是用來(lái)設(shè)置動(dòng)畫(huà)的模式的,默認(rèn)的jQuery庫(kù)只有兩種簡(jiǎn)單的線性庫(kù),可以下載jQuery.easing庫(kù)來(lái)添加更多絢麗的效果。
這樣就完成了這個(gè)插件的編寫(xiě),完整的代碼如下:
復(fù)制代碼 代碼如下:

(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);

很簡(jiǎn)單有趣的效果,邏輯很清楚,代碼也簡(jiǎn)單,是練手的好東東。
打包下載地址 http://m.fzitv.net/jiaoben/26031.html

相關(guān)文章

  • jquery 3D球狀導(dǎo)航的文章分類

    jquery 3D球狀導(dǎo)航的文章分類

    分類標(biāo)題呈現(xiàn)3D球狀效果,點(diǎn)擊分類標(biāo)題的時(shí)候,會(huì)彈出這個(gè)分類對(duì)應(yīng)的推薦文章列表。
    2010-07-07
  • CSS3,HTML5和jQuery搜索框集錦

    CSS3,HTML5和jQuery搜索框集錦

    小編收集了13個(gè)有用的CSS3,HTML5和jQuery搜索表單腳本來(lái)幫助大家從頭開(kāi)始創(chuàng)建一個(gè)搜索框。希望能夠幫助到大家,讓我們一起來(lái)看看吧!
    2014-12-12
  • jquery實(shí)現(xiàn)折疊菜單效果【推薦】

    jquery實(shí)現(xiàn)折疊菜單效果【推薦】

    本文主要介紹了jquery實(shí)現(xiàn)折疊菜單效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • 讀jQuery之二(兩種擴(kuò)展)

    讀jQuery之二(兩種擴(kuò)展)

    上一篇分析了jQuery對(duì)象的組成,這篇分析下它的extend方法。
    2011-06-06
  • jQuery bind事件使用詳解

    jQuery bind事件使用詳解

    很久沒(méi)有寫(xiě)東西了,今天在工作中碰見(jiàn)問(wèn)題才發(fā)現(xiàn)。以后得多逼自己多抽時(shí)間來(lái)寫(xiě)寫(xiě)自己的東西,也順便和大家分享一下自己在工作中碰見(jiàn)的問(wèn)題。
    2011-05-05
  • 淺談jQuery中事情的動(dòng)態(tài)綁定

    淺談jQuery中事情的動(dòng)態(tài)綁定

    下面小編就為大家?guī)?lái)一篇淺談jQuery中事情的動(dòng)態(tài)綁定。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • jQuery+css3動(dòng)畫(huà)屬性制作獵豹瀏覽器寬屏banner焦點(diǎn)圖

    jQuery+css3動(dòng)畫(huà)屬性制作獵豹瀏覽器寬屏banner焦點(diǎn)圖

    本文給大家分享的是使用jQuery結(jié)合CSS3制作的仿獵豹瀏覽器寬屏banner焦點(diǎn)圖特效,代碼很簡(jiǎn)單,效果卻非常棒,而且兼容各大瀏覽器,這里推薦給大家,有需要的小伙伴參考下。
    2015-03-03
  • jQuery中設(shè)置form表單中action值的實(shí)現(xiàn)方法

    jQuery中設(shè)置form表單中action值的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇jQuery中設(shè)置form表單中action值的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • jQuery+css3實(shí)現(xiàn)Ajax點(diǎn)擊后動(dòng)態(tài)刪除功能的方法

    jQuery+css3實(shí)現(xiàn)Ajax點(diǎn)擊后動(dòng)態(tài)刪除功能的方法

    這篇文章主要介紹了jQuery+css3實(shí)現(xiàn)Ajax點(diǎn)擊后動(dòng)態(tài)刪除功能的方法,可實(shí)現(xiàn)點(diǎn)擊選區(qū)后出現(xiàn)選區(qū)收縮、滾動(dòng)消失的效果,涉及jquery結(jié)合Ajax與數(shù)學(xué)運(yùn)算實(shí)時(shí)操作頁(yè)面元素的相關(guān)技巧,需要的朋友可以參考下
    2015-08-08
  • 基于jquery實(shí)現(xiàn)的類似百度搜索的輸入框自動(dòng)完成功能

    基于jquery實(shí)現(xiàn)的類似百度搜索的輸入框自動(dòng)完成功能

    自動(dòng)完成功能是指:類似百度搜索之類的輸入一個(gè)詞的一部分后就自動(dòng)提示,然后用戶可以選擇,不需要再輸入剩余部分。
    2011-08-08

最新評(píng)論

洞头县| 青河县| 潜江市| 靖宇县| 丹阳市| 大宁县| 闽侯县| 资溪县| 咸宁市| 马鞍山市| 句容市| 长白| 兴义市| 田阳县| 嘉鱼县| 河津市| 彝良县| 即墨市| 武义县| 南宁市| 大城县| 抚宁县| 渑池县| 德惠市| 琼海市| 莲花县| 剑川县| 衡阳市| 丰台区| 岳阳县| 米脂县| 白玉县| 西乌珠穆沁旗| 明星| 修武县| 富宁县| 密山市| 婺源县| 开江县| 棋牌| 井冈山市|