/** * 浮动飘窗广告js * author:zhangzj; * v 1.0.1 * 2015-12-29 * */ (function(win, $) { var defaultsettings = { // 浮动元素dom dom: null, // 元素宽度 width: 100, //元素高度 height: 100, // 步长 step: 1, ypos:100, xpos:100, // 延时 delay: 30 }; win.floatad = function(opts) { this.cfg = $.extend({}, defaultsettings, opts); // 定义元素初始偏移量 this.ypos = opts.ypos; this.xpos = opts.xpos; // 定义元素初始方向 this.xin = true; this.yin = true; // 初始化函数 this._initevent(); }; $.extend(floatad.prototype, { _initevent: function() { var c = this.cfg; var t = this; var itl = setinterval(function() { t._changepos(c); }, c.delay); $(c.dom).mouseover(function() { clearinterval(itl); }); $(c.dom).mouseout(function() { itl = setinterval(function() { t._changepos(c); }, c.delay); }); $(c.dom).find('.close').on('click',function(){ $(this).parent().addclass('hidden'); }); }, _changepos: function(c) { // 获得元素大小 var mw = c.width, mh = c.height; // 获得当前对象 var self = this; // 可视区域窗口大小 var w = $(window).width(), h = $(window).height(); // 获得纵向偏移量 var sh = $(window).scrolltop(); // 进行浮动,判断是否在边界位置 self.xpos = self.xpos + c.step * (self.xin ? 1 : -1); if (self.xpos >= (w - mw)) { self.xin = false; self.xpos = w - mw; } else if (self.xpos <= 0) { self.xin = true; self.xpos = 0; } self.ypos = self.ypos + c.step * (self.yin ? 1 : -1); if (self.ypos >= (h - mh)) { self.yin = false; self.ypos = h - mh; } else if (self.ypos <= 0) { self.yin = true; self.ypos = 0; } // 设置偏移量 $(c.dom).css({ 'top': self.ypos + sh, 'left': self.xpos }); } }); }(this, jquery));