编写JQuery插件示例

2019-04-15 14:02发布

javascript: $.fn.extend({ PhotoShowSilder:function(_box){ _box = _box || document; var $ul = $("ul",_box); var $li = $("li",$ul); var img = $("#PhotoShow",_box); var pic = $("#pic",_box); var minheight = "60px"; var maxheight = (Math.ceil($li.length/3) * 60) + "px"; var isExp = false; return this.each(function(){ var $this = $(this); $this.click(function(event){ if(isExp){ pic.animate({ height: minheight }, 600 ); //alert($this.attr("src")); //alert(event.target.src); $this.attr("src","resource/btn.gif"); isExp = false; } else{ pic.animate({ height: maxheight }, 600 ); $this.attr("src","resource/btn-1.gif"); isExp = true; } }); }); } }); (function($){ $(".PhotoShowSilder").PhotoShowSilder($(".productArea")); })(jQuery);   html代码: -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> http://www.w3.org/1999/xhtml"> Content-Type" content="text/html; charset=UTF-8"> X-UA-Compatible" content="IE=EmulateIE7"> 我的订单 resource/css.css" rel="stylesheet" type="text/css"> class="cols_manage">
class="wrapper">
class="productArea"> class="track product-btn" id="PhotoShow" style="cursor: pointer;"> resource/btn.gif" class="PhotoShowSilder" alt="">


注意:引入的自定义插件js,要加上defer关键字