/* * description: 漂浮广告 * author: jjc * date: 2012.07.04 */ ;(function ( $, window, document, undefined ) { var pluginname = 'floatingad'; var defaults = { step: 1, delay: 50, islinkclosed: false, onclose: function(elem){} }; var ads = { linkurl: '#', 'z-index': '100', 'closed-icon': '', imgheight: '', imgwidth: '', title: '', img: '#', linkwindow: '_blank', headfilter: 0.2 }; function plugin(element, options) { this.element = element; this.options = $.extend( {}, defaults, options, { width: $(window).width(), height: $(window).height(), xpos: this.getrandomnum(0, $(window).width() - $(element).innerwidth()), ypos: this.getrandomnum(0, 300), yon: this.getrandomnum(0, 1), xon: this.getrandomnum(0, 1), ypath: this.getrandomnum(0, 1), xpath: this.getrandomnum(0, 1), hoffset: $(element).innerheight(), woffset: $(element).innerwidth(), fn: function(){}, interval: 0 } ); this._defaults = defaults; this._name = pluginname; this.init(); } plugin.prototype = { init: function () { var elem = $(this.element); var defaults = this.options; var p = this; var xflag = 0; var yflag = 0; elem.css({"left": defaults.xpos + p.scrollx(), "top": defaults.ypos + p.scrolly()}); defaults.fn = function(){ defaults.width = $(window).width(); defaults.height = $(window).height(); if(xflag == p.scrollx() && yflag == p.scrolly()){ elem.css({"left": defaults.xpos + p.scrollx(), "top": defaults.ypos + p.scrolly()}); if (defaults.yon) defaults.ypos = defaults.ypos + defaults.step; else defaults.ypos = defaults.ypos - defaults.step; if (defaults.ypos <= 0) { defaults.yon = 1; defaults.ypos = 0; } if (defaults.ypos >= (defaults.height - defaults.hoffset)) { defaults.yon = 0; defaults.ypos = (defaults.height - defaults.hoffset); } if (defaults.xon) defaults.xpos = defaults.xpos + defaults.step; else defaults.xpos = defaults.xpos - defaults.step; if (defaults.xpos <= 0) { defaults.xon = 1; defaults.xpos = 0; } if (defaults.xpos >= (defaults.width - defaults.woffset)) { defaults.xon = 0; defaults.xpos = (defaults.width - defaults.woffset); } } yflag = $(window).scrolltop(); xflag = $(window).scrollleft(); }; this.run(elem, defaults); }, run: function(elem, defaults){ this.start(elem, defaults); this.adevent(elem,defaults); }, start: function(elem, defaults){ elem.find('div.close').show(); defaults.interval = window.setinterval(defaults.fn, defaults.delay); window.settimeout(function(){elem.show();}, defaults.delay); }, getrandomnum: function (min, max){ var range = max - min; var rand = math.random(); return(min + math.round(rand * range)); }, getpath: function(on){ return on ? 0 : 1; }, clear: function(elem, defaults){ elem.find('div.close').show(); window.clearinterval(defaults.interval); }, close: function(elem, defaults, isclose){ //elem.unbind('hover'); elem.unbind("mouseenter mouseleave"); elem.hide(); if(isclose) defaults.onclose.call(elem); }, adevent: function(elem, defaults){ var obj = { elem: this, fn_close: function() { this.elem.close(elem, defaults, true); }, fn_clear: function() { if(this.elem.options.islinkclosed) this.elem.close(elem, defaults, false); } }; elem.find('div.button').bind('click', jquery.proxy(obj, "fn_close")); elem.find('a').bind('click', jquery.proxy(obj, "fn_clear")); var stop = { elem: this, over: function(){ this.elem.clear(elem, defaults); }, out: function(){ this.elem.start(elem, defaults); } }; elem.bind("mouseenter", jquery.proxy(stop, "over")); elem.bind("mouseleave", jquery.proxy(stop, "out")); /* elem.hover( jquery.proxy(stop, "over"), jquery.proxy(stop, "out") ); */ }, scrollx: function(){ var de = document.documentelement; return self.pagexoffset || (de && de.scrollleft) || document.body.scrollleft; }, scrolly: function(){ var de = document.documentelement; return self.pageyoffset || (de && de.scrolltop) || document.body.scrolltop; } }; $.fn.floatingad = function(options) { return this.children("div").each(function (i, elem) { if (!$.data(this, 'plugin_' + pluginname)) { $.data(this, 'plugin_' + pluginname, new plugin(this, options)); } }); }; $.floatingad = function(options){ if(options){ if(options.ad){ var addiv = $('#' + pluginname); if(addiv.length <= 0) addiv = $('