jquery.smartAD.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*!
  2. * AD plugin smartAD V1.0
  3. * Depended jQuery. url http://jquery.com/
  4. * Author JsonZou
  5. * Copyright 2013
  6. *
  7. * Date: 2013-2-14 21:00
  8. */
  9. ;(function($){
  10. //浮窗样式定义
  11. var smartADClass={
  12. "smart-ad-fixed":{"position":"fixed","z-index":99},//浮动
  13. "smart-ad-nofixed":{"position":"absolute","z-index":99},//不浮动
  14. "smart-ad-win":function(style){//广告窗体
  15. return {"background":style.background?style.background:"#fff",
  16. "border":style.border?style.border:"1px solid #eee",
  17. "width":style.width,
  18. "left":style.left,
  19. "top":style.top,
  20. "padding":0,"cursor":"pointer"};
  21. },
  22. "smart-ad-win-header":function(style){//广告窗体头部
  23. return {"background":style.background?style.background:"#aaa",
  24. "border-bottom":style.border?style.border:"1px solid #ddd",
  25. "width":"100%","height":"12px","font-size":"10px","display":"none","padding":0};
  26. },
  27. "smart-ad-win-body":function(style){//广告body
  28. return {//"background":style.background?style.background:"#aaa",
  29. "height":"100%",
  30. "width":"100%","padding":0,"word-wrap":"break-word","word-break":"normal"};
  31. },
  32. "smart-ad-win-close":{"position":"absolute","right":"-3px","top":"-2px","padding":0,//广告窗体关闭按钮
  33. "width":"24px","height":"14px"},
  34. "smart-ad-position-tl":function(style){//广告窗体位置topleft(顶部左边)
  35. return {
  36. 'height':style.height?style.height:90,
  37. 'width':style.width?style.width:120,
  38. 'top':0,'left':0};
  39. },
  40. "smart-ad-position-tm":function(style){//广告窗体位置topmiddle(顶部中间)
  41. return {
  42. 'height':style.height?style.height:90,
  43. 'width':style.width?style.width:960,
  44. "margin-left":style.width?-style.width/2:-480,
  45. 'top':0,'left':'50%'};
  46. },
  47. "smart-ad-position-tr":function(style){//广告窗体位置topright(顶部右边)
  48. return {
  49. 'height':style.height?style.height:90,
  50. 'width':style.width?style.width:120,
  51. 'top':0,'right':0,left:'auto'};
  52. },
  53. "smart-ad-position-mm":function(style){//广告窗体位置middlemiddle(中部中间)
  54. return {
  55. 'height':style.height?style.height:400,
  56. 'width':style.width?style.width:600,
  57. "margin-top":style.height?-style.height/2:-200,
  58. "margin-left":style.width?-style.width/2:-300,
  59. 'top':'50%','left':"50%"};
  60. },
  61. "smart-ad-position-ml":function(style){//广告窗体位置middleleft(中部左边)
  62. return {
  63. 'height':style.height?style.height:300,
  64. 'width':style.width?style.width:120,
  65. "margin-top":style.height?-style.height/2:-150,
  66. 'top':'50%','left':"10px"};
  67. },
  68. "smart-ad-position-mlt":function(style){//广告窗体位置middlelefttop(中部左边上方)
  69. return {
  70. 'height':style.height?style.height:150,
  71. 'width':style.width?style.width:120,
  72. "margin-top":style.height?-(style.height+10):-160,
  73. 'top':'50%','left':"10px"};
  74. },
  75. "smart-ad-position-mlb":function(style){//广告窗体位置middleleftbottoom(中部左边下方)
  76. return {
  77. 'height':style.height?style.height:150,
  78. 'width':style.width?style.width:120,
  79. "margin-top":10,
  80. 'top':'50%','left':"10px"};
  81. },
  82. "smart-ad-position-mr":function(style){//广告窗体位置middleright(中部右边)
  83. return {
  84. 'height':style.height?style.height:300,
  85. 'width':style.width?style.width:120,
  86. "margin-top":style.height?-style.height/2:-150,
  87. 'top':'50%','right':"10px",left:'auto'};
  88. },
  89. "smart-ad-position-mrt":function(style){//广告窗体位置middlerighttop(中部右边上方)
  90. return {
  91. 'height':style.height?style.height:150,
  92. 'width':style.width?style.width:120,
  93. "margin-top":style.height?-(style.height+10):-160,
  94. 'top':'50%','right':"10px",left:'auto'};
  95. },
  96. "smart-ad-position-mrb":function(style){//广告窗体位置middlerightbottom(中部右边下方)
  97. return {
  98. 'height':style.height?style.height:150,
  99. 'width':style.width?style.width:120,
  100. "margin-top":10,
  101. 'top':'50%','right':"10px",left:'auto'};
  102. },
  103. "smart-ad-position-bl":function(style){//广告窗体位置bottomleft(底部左边)
  104. return {
  105. 'height':style.height?style.height:230,
  106. 'width':style.width?style.width:270,
  107. 'bottom':0,'left':0,'top':'atuo'};
  108. },
  109. "smart-ad-position-bm":function(style){//广告窗体位置bottommiddle(底部中间)
  110. return {
  111. 'height':style.height?style.height:90,
  112. 'width':style.width?style.width:960,
  113. "margin-left":style.height?-style.height/2:-480,
  114. 'bottom':0,'left':"50%","top":'auto'};
  115. },
  116. "smart-ad-position-br":function(style){//广告窗体位置bottomright(底部右边)
  117. return {
  118. 'height':style.height?style.height:230,
  119. 'width':style.width?style.width:270,
  120. 'bottom':0,'right':0,left:'auto',top:'auto'};
  121. }
  122. };
  123. //参数常量
  124. var contants={
  125. delayFuncs:["fadeOut","hide","slideUp"],//支持的效果
  126. delayFunc:'fadeOut',//默认效果
  127. positions:['tl','tm','tr','mm','ml','mr','mlt','mlb','mrt','mrb','bl','bm','br'],//支持的方位
  128. position:"br",//默认方位
  129. style:{height:0,width:0,background:"",border:"",top:0,left:0}//窗体样式
  130. };
  131. //调用参数
  132. var $option={
  133. position:"",//广告悬挂位置,t(top上),b(bottom下),l(left左),r(right右),m(middle中).可选值(tl|tm|tr ml|mr|mlt|mlb|mrt|mrb bl|bm|br)
  134. style:contants.style,//广告窗体样式
  135. close:true,//是否可关闭
  136. header:true,//是否显示广告窗体头部
  137. fixed:true,//是否固定位置浮动窗口显示
  138. content:"",//广告内容,一般是图片,或html代码
  139. link:"#",//广告链接
  140. delay:0,//广告窗体延迟消失时间
  141. delayFunc:contants.delayFunc,//广告窗体消失效果函数,为jQuery自带函数,可选值:[fadeOut,hide,slideUp]
  142. before:null,//广告加载前回调函数function(option,win,winHeader,winBody){}.option:配置参数,win:广告窗体jQuery对象,winHeader:广告窗体头部jQuery对象,winBody:广告窗体body的jQuery对象
  143. after:null//广告加载后回调函数function(option,win,winHeader,winBody){}.option:配置参数,win:广告窗体jQuery对象,winHeader:广告窗体头部jQuery对象,winBody:广告窗体body的jQuery对象
  144. };
  145. //浮窗控件
  146. var _floatWin=$("<div class='smart-ad-win'></div>");
  147. var _floatWinHeader=$("<div class='smart-ad-win-header'></div>");
  148. var _floatWinBody=$("<div class='smart-ad-win-body'></div>");
  149. var _floatWinCloseBtn=$("<div class='smart-ad-win-close'>关闭</div>");
  150. //smartAD定义
  151. var $_smartAD=function(_option){
  152. //整理参数
  153. var option=$.extend({},$option,_option);
  154. option.style=$.extend({},contants.style,option.style);
  155. option.style.height=parseInt(option.style.height);
  156. option.style.width=parseInt(option.style.width);
  157. option.style.top=parseInt(option.style.top);
  158. option.style.left=parseInt(option.style.left);
  159. //验证
  160. //if(!option.link||!option.content){return;}
  161. //浮窗控件生成
  162. var floatWin=_floatWin.clone();
  163. var floatWinHeader=_floatWinHeader.clone();
  164. var floatWinBody=_floatWinBody.clone();
  165. var floatWinCloseBtn=_floatWinCloseBtn.clone();
  166. floatWin.append(floatWinHeader).append(floatWinBody);
  167. floatWinHeader.append(floatWinCloseBtn);
  168. $("body").append(floatWin);
  169. //广告加载之前回调
  170. if(option.before){option.before(option,floatWin,floatWinHeader,floatWinBody);}
  171. //浮窗样式渲染
  172. floatWin.css(smartADClass["smart-ad-win"](option.style));
  173. floatWinHeader.css(smartADClass["smart-ad-win-header"](option.style));
  174. floatWinBody.css(smartADClass["smart-ad-win-body"](option.style));
  175. if(option.position){
  176. if($.inArray(option["position"],contants.positions)==-1){alert("Error!\nThe position is not exits.Supports:\n"+contants.positions);return;}
  177. floatWin.css(smartADClass["smart-ad-position-"+option["position"]](option.style))
  178. };
  179. floatWinCloseBtn.css(smartADClass["smart-ad-win-close"]);
  180. floatWinHeader.width(floatWinBody.width());
  181. floatWin.css(smartADClass["smart-ad-fixed"]);
  182. if(!option.fixed){floatWin.css(smartADClass["smart-ad-nofixed"]);}
  183. //广告内容渲染
  184. floatWinBody.html(option.content);
  185. //事件绑定
  186. //关闭按钮事件
  187. if(option.close){floatWinHeader.show();floatWinCloseBtn.click(function(){floatWin.hide();});}
  188. //广告点击事件
  189. floatWinBody.attr("link",option.link);
  190. if(option.link&&option.link!='#'){
  191. floatWinBody.click(function(){smartADClickEvent(option.link);});
  192. }
  193. //窗体消失效果事件
  194. if(option.delay>0){
  195. if($.inArray(option.delayFunc,contants.delayFuncs)==-1){option.delayFunc=contants.delayFunc;}
  196. setTimeout(function(){floatWin[option.delayFunc](800);},option.delay);
  197. }
  198. //广告加载之后回调
  199. if(option.after){option.after(option,floatWin,floatWinHeader,floatWinBody);}
  200. };
  201. //smartAD广告点击事件
  202. var smartADClickEvent=function(link){
  203. var adform=$("#smart-ad-form");
  204. if(adform.length==0){
  205. adform=$("<form id='smart-ad-form' name='smart-ad-form' target='_blank' method='get'></form>");
  206. $('body').append(adform)
  207. }
  208. adform.attr('action',link);
  209. adform.submit();
  210. };
  211. //window 扩展支持
  212. window.$smartAD=function(option_){
  213. if(!option_["position"]){option_.position=contants.position;}
  214. $_smartAD(option_);
  215. };;
  216. //jquery扩展函数
  217. $.smartAD=$smartAD;
  218. //jquery对象支持函数
  219. $.fn.extend({
  220. "smartAD":function(_option_){
  221. var wrap=$("<div></div>");
  222. var temp=$(this).clone();
  223. temp.attr("id","smart-ad-id-"+$(this).attr("id"));
  224. wrap.append(temp);
  225. var fixed=_option_['fixed'];
  226. _option_=$.extend({},$option,_option_);
  227. _option_.style=$.extend({},contants.style,_option_.style);
  228. if(!_option_.position){//没有方位默认是dom对象所在位置
  229. if(!fixed){_option_.fixed=false;}
  230. _option_["style"]={top:_option_["style"].top?_option_["style"].top:$(this).offset().top,
  231. left:_option_["style"].left?_option_["style"].left:$(this).offset().left,
  232. height:_option_["style"].height?_option_["style"].height:$(this).height(),
  233. width:_option_["style"].width?_option_["style"].width:$(this).width(),
  234. background:_option_["style"].background,
  235. border:_option_["style"].border
  236. };
  237. }else{
  238. _option_["style"]["top"]=0;
  239. _option_["style"]["left"]=0;
  240. //_option_["style"]["height"]=$(this).height();
  241. // _option_["style"]["width"]=$(this).width();
  242. }
  243. if(!_option_.content){//没有内容默认是dom的内容
  244. $(this).hide();
  245. _option_.content=wrap.html();
  246. }
  247. $_smartAD(_option_);
  248. }
  249. });
  250. })(jQuery);
  251. !function($){
  252. /**
  253. * Description: jquery飘窗插件,可自适应浏览器宽高的变化
  254. * Author: ddqre12345
  255. * CreateTime: 2017.5.3
  256. * param: args={startL:default, startT:default, angle:-Math.PI/4, speed: 125}
  257. * 参数说名: startL飘窗初始时距离左边的距离, startT飘窗初始化距离顶部的距离, angle飘窗初始运动方向, speed运动速度(px/s)
  258. */
  259. $.fn.autoMove = function(args){
  260. //先定义一些工具函数判断边距
  261. function isTop(pos, w_w, w_h, d_w, d_h){//飘窗到达上边距
  262. return (pos.top<=0) ? true : false;
  263. }
  264. function isBottom(pos, w_w, w_h, d_w, d_h){//飘窗到达下边距
  265. return (pos.top>=(w_h-d_h)) ? true : false;
  266. }
  267. function isLeft(pos, w_w, w_h, d_w, d_h){//飘窗到达左边距
  268. return (pos.left<=0) ? true : false;
  269. }
  270. function isRight(pos, w_w, w_h, d_w, d_h){//飘窗到达右边距
  271. return (pos.left>=(w_w-d_w)) ? true : false;
  272. }
  273. return this.each(function(){
  274. var w_w = parseInt($(window).width()),
  275. w_h = parseInt($(window).height()),
  276. d_w = parseInt($(this).width()),
  277. d_h = parseInt($(this).height()),
  278. d_l = (w_w-d_w)/2,
  279. d_t = (w_h-d_h)/2,
  280. max_l = w_w - d_w;
  281. max_t = w_h - d_h;
  282. //位置及参数的初始化
  283. var setobj = $.extend({startL:d_l, startT:d_t, angle:Math.PI/4, speed:100}, args);
  284. $(this).css({position: 'absolute', left: setobj['startL']+'px', top: setobj['startT']+'px'});
  285. var position = {left: setobj['startL'], top: setobj['startT']};//飘窗位置对象
  286. var that = $(this);
  287. var angle= setobj.angle;
  288. var time = 10;//控制飘窗运动效果,值越小越细腻
  289. var step = setobj.speed * (time/1000);//计算运动步长
  290. var decoration = {x:step*Math.cos(angle), y:step*Math.sin(angle)};//计算二维上的运动增量
  291. var mvtid;
  292. $(window).on('resize', function(){//窗口大小变动时重新设置运动相关参数
  293. w_w = parseInt($(window).width());
  294. w_h = parseInt($(window).height());
  295. max_l = w_w - d_w;
  296. max_t = w_h - d_h;
  297. });
  298. function move(){
  299. position.left += decoration.x;
  300. position.top += decoration.y;
  301. if(isLeft(position, w_w, w_h, d_w, d_h)||isRight(position, w_w, w_h, d_w, d_h)){
  302. decoration.x = -1*decoration.x;
  303. }
  304. if(isRight(position, w_w, w_h, d_w, d_h)){
  305. position.left = max_l;
  306. }
  307. if(isTop(position, w_w, w_h, d_w, d_h)||isBottom(position, w_w, w_h, d_w, d_h)){
  308. decoration.y = -1*decoration.y;
  309. }
  310. if(isBottom(position, w_w, w_h, d_w, d_h)){
  311. position.top = max_t;
  312. }
  313. //that.css({left:position.left, top:position.top});
  314. that.animate({left:position.left, top:position.top}, time);//改用jquery动画函数使其更加平滑
  315. mvtid = setTimeout(move, time);//递归调用,使飘窗连续运动
  316. }
  317. move();//触发动作
  318. that.on('mouseenter', function(){ clearTimeout(mvtid) });//添加鼠标事件
  319. that.on('mouseleave', function(){ move() });
  320. });
  321. }; //end plugin definition
  322. }(jQuery);