-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.smartAD.js
285 lines (250 loc) · 11.2 KB
/
jquery.smartAD.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*!
* AD plugin smartAD V1.0
* Depended jQuery. url http://jquery.com/
* Author JsonZou
* Copyright 2013
*
* Date: 2013-2-14 21:00
*/
;(function($){
//浮窗样式定义
var smartADClass={
"smart-ad-fixed":{"position":"fixed","z-index":99},//浮动
"smart-ad-nofixed":{"position":"absolute","z-index":99},//不浮动
"smart-ad-win":function(style){//广告窗体
return {"background":style.background?style.background:"#ddd",
"border":style.border?style.border:"1px solid #eee",
"width":style.width,
"left":style.left,
"top":style.top,
"padding":0,"cursor":"pointer"};
},
"smart-ad-win-header":function(style){//广告窗体头部
return {"background":style.background?style.background:"#aaa",
"border-bottom":style.border?style.border:"1px solid #ddd",
"width":"100%","height":"12px","font-size":"10px","display":"none","padding":0};
},
"smart-ad-win-body":function(style){//广告body
return {//"background":style.background?style.background:"#aaa",
"height":"100%",
"width":"100%","padding":0,"word-wrap":"break-word","word-break":"normal"};
},
"smart-ad-win-close":{"position":"absolute","right":"-3px","top":"-2px","padding":0,//广告窗体关闭按钮
"background":"url(img/close.png)","width":"14px","height":"14px"},
"smart-ad-position-tl":function(style){//广告窗体位置topleft(顶部左边)
return {
'height':style.height?style.height:90,
'width':style.width?style.width:120,
'top':0,'left':0};
},
"smart-ad-position-tm":function(style){//广告窗体位置topmiddle(顶部中间)
return {
'height':style.height?style.height:90,
'width':style.width?style.width:960,
"margin-left":style.width?-style.width/2:-480,
'top':0,'left':'50%'};
},
"smart-ad-position-tr":function(style){//广告窗体位置topright(顶部右边)
return {
'height':style.height?style.height:90,
'width':style.width?style.width:120,
'top':0,'right':0,left:'auto'};
},
"smart-ad-position-mm":function(style){//广告窗体位置middlemiddle(中部中间)
return {
'height':style.height?style.height:400,
'width':style.width?style.width:600,
"margin-top":style.height?-style.height/2:-200,
"margin-left":style.width?-style.width/2:-300,
'top':'50%','left':"50%"};
},
"smart-ad-position-ml":function(style){//广告窗体位置middleleft(中部左边)
return {
'height':style.height?style.height:300,
'width':style.width?style.width:120,
"margin-top":style.height?-style.height/2:-150,
'top':'50%','left':"10px"};
},
"smart-ad-position-mlt":function(style){//广告窗体位置middlelefttop(中部左边上方)
return {
'height':style.height?style.height:150,
'width':style.width?style.width:120,
"margin-top":style.height?-(style.height+10):-160,
'top':'50%','left':"10px"};
},
"smart-ad-position-mlb":function(style){//广告窗体位置middleleftbottoom(中部左边下方)
return {
'height':style.height?style.height:150,
'width':style.width?style.width:120,
"margin-top":10,
'top':'50%','left':"10px"};
},
"smart-ad-position-mr":function(style){//广告窗体位置middleright(中部右边)
return {
'height':style.height?style.height:300,
'width':style.width?style.width:120,
"margin-top":style.height?-style.height/2:-150,
'top':'50%','right':"10px",left:'auto'};
},
"smart-ad-position-mrt":function(style){//广告窗体位置middlerighttop(中部右边上方)
return {
'height':style.height?style.height:150,
'width':style.width?style.width:120,
"margin-top":style.height?-(style.height+10):-160,
'top':'50%','right':"10px",left:'auto'};
},
"smart-ad-position-mrb":function(style){//广告窗体位置middlerightbottom(中部右边下方)
return {
'height':style.height?style.height:150,
'width':style.width?style.width:120,
"margin-top":10,
'top':'50%','right':"10px",left:'auto'};
},
"smart-ad-position-bl":function(style){//广告窗体位置bottomleft(底部左边)
return {
'height':style.height?style.height:230,
'width':style.width?style.width:270,
'bottom':0,'left':0,'top':'atuo'};
},
"smart-ad-position-bm":function(style){//广告窗体位置bottommiddle(底部中间)
return {
'height':style.height?style.height:90,
'width':style.width?style.width:960,
"margin-left":style.height?-style.height/2:-480,
'bottom':0,'left':"50%","top":'auto'};
},
"smart-ad-position-br":function(style){//广告窗体位置bottomright(底部右边)
return {
'height':style.height?style.height:230,
'width':style.width?style.width:270,
'bottom':0,'right':0,left:'auto',top:'auto'};
}
};
//参数常量
var contants={
delayFuncs:["fadeOut","hide","slideUp"],//支持的效果
delayFunc:'fadeOut',//默认效果
positions:['tl','tm','tr','mm','ml','mr','mlt','mlb','mrt','mrb','bl','bm','br'],//支持的方位
position:"br",//默认方位
style:{height:0,width:0,background:"",border:"",top:0,left:0}//窗体样式
};
//调用参数
var $option={
position:"",//广告悬挂位置,t(top上),b(bottom下),l(left左),r(right右),m(middle中).可选值(tl|tm|tr ml|mr|mlt|mlb|mrt|mrb bl|bm|br)
style:contants.style,//广告窗体样式
close:true,//是否可关闭
header:true,//是否显示广告窗体头部
fixed:true,//是否固定位置浮动窗口显示
content:"",//广告内容,一般是图片,或html代码
link:"#",//广告链接
delay:0,//广告窗体延迟消失时间
delayFunc:contants.delayFunc,//广告窗体消失效果函数,为jQuery自带函数,可选值:[fadeOut,hide,slideUp]
before:null,//广告加载前回调函数function(option,win,winHeader,winBody){}.option:配置参数,win:广告窗体jQuery对象,winHeader:广告窗体头部jQuery对象,winBody:广告窗体body的jQuery对象
after:null//广告加载后回调函数function(option,win,winHeader,winBody){}.option:配置参数,win:广告窗体jQuery对象,winHeader:广告窗体头部jQuery对象,winBody:广告窗体body的jQuery对象
};
//浮窗控件
var _floatWin=$("<div class='smart-ad-win'></div>");
var _floatWinHeader=$("<div class='smart-ad-win-header'></div>");
var _floatWinBody=$("<div class='smart-ad-win-body'></div>");
var _floatWinCloseBtn=$("<div class='smart-ad-win-close'></div>");
//smartAD定义
var $_smartAD=function(_option){
//整理参数
var option=$.extend({},$option,_option);
option.style=$.extend({},contants.style,option.style);
option.style.height=parseInt(option.style.height);
option.style.width=parseInt(option.style.width);
option.style.top=parseInt(option.style.top);
option.style.left=parseInt(option.style.left);
//验证
//if(!option.link||!option.content){return;}
//浮窗控件生成
var floatWin=_floatWin.clone();
var floatWinHeader=_floatWinHeader.clone();
var floatWinBody=_floatWinBody.clone();
var floatWinCloseBtn=_floatWinCloseBtn.clone();
floatWin.append(floatWinHeader).append(floatWinBody);
floatWinHeader.append(floatWinCloseBtn);
$("body").append(floatWin);
//广告加载之前回调
if(option.before){option.before(option,floatWin,floatWinHeader,floatWinBody);}
//浮窗样式渲染
floatWin.css(smartADClass["smart-ad-win"](option.style));
floatWinHeader.css(smartADClass["smart-ad-win-header"](option.style));
floatWinBody.css(smartADClass["smart-ad-win-body"](option.style));
if(option.position){
if($.inArray(option["position"],contants.positions)==-1){alert("Error!\nThe position is not exits.Supports:\n"+contants.positions);return;}
floatWin.css(smartADClass["smart-ad-position-"+option["position"]](option.style))
};
floatWinCloseBtn.css(smartADClass["smart-ad-win-close"]);
floatWinHeader.width(floatWinBody.width());
floatWin.css(smartADClass["smart-ad-fixed"]);
if(!option.fixed){floatWin.css(smartADClass["smart-ad-nofixed"]);}
//广告内容渲染
floatWinBody.html(option.content);
//事件绑定
//关闭按钮事件
if(option.close){floatWinHeader.show();floatWinCloseBtn.click(function(){floatWin.hide();});}
//广告点击事件
floatWinBody.attr("link",option.link);
if(option.link&&option.link!='#'){
floatWinBody.click(function(){smartADClickEvent(option.link);});
}
//窗体消失效果事件
if(option.delay>0){
if($.inArray(option.delayFunc,contants.delayFuncs)==-1){option.delayFunc=contants.delayFunc;}
setTimeout(function(){floatWin[option.delayFunc](800);},option.delay);
}
//广告加载之后回调
if(option.after){option.after(option,floatWin,floatWinHeader,floatWinBody);}
};
//smartAD广告点击事件
var smartADClickEvent=function(link){
var adform=$("#smart-ad-form");
if(adform.length==0){
adform=$("<form id='smart-ad-form' name='smart-ad-form' target='_blank' method='get'></form>");
$('body').append(adform)
}
adform.attr('action',link);
adform.submit();
};
//window 扩展支持
window.$smartAD=function(option_){
if(!option_["position"]){option_.position=contants.position;}
$_smartAD(option_);
};;
//jquery扩展函数
$.smartAD=$smartAD;
//jquery对象支持函数
$.fn.extend({
"smartAD":function(_option_){
var wrap=$("<div></div>");
var temp=$(this).clone();
temp.attr("id","smart-ad-id-"+$(this).attr("id"));
wrap.append(temp);
var fixed=_option_['fixed'];
_option_=$.extend({},$option,_option_);
_option_.style=$.extend({},contants.style,_option_.style);
if(!_option_.position){//没有方位默认是dom对象所在位置
if(!fixed){_option_.fixed=false;}
_option_["style"]={top:_option_["style"].top?_option_["style"].top:$(this).offset().top,
left:_option_["style"].left?_option_["style"].left:$(this).offset().left,
height:_option_["style"].height?_option_["style"].height:$(this).height(),
width:_option_["style"].width?_option_["style"].width:$(this).width(),
background:_option_["style"].background,
border:_option_["style"].border
};
}else{
_option_["style"]["top"]=0;
_option_["style"]["left"]=0;
//_option_["style"]["height"]=$(this).height();
// _option_["style"]["width"]=$(this).width();
}
if(!_option_.content){//没有内容默认是dom的内容
$(this).hide();
_option_.content=wrap.html();
}
$_smartAD(_option_);
}
});
})(jQuery);