var SupActivity = function(parmas){ // this.code = parmas.code; this.url = parmas.url; this.href = parmas.href; this.htmlString = parmas.temp; this.host = this.getHost(); this.index = parmas.index; this.init(); }; SupActivity.prototype = { getHost:function(){ var currentHost=window.location.host.split('.')[0];//区分项目:www(主站 1),jk(2),ios(3),publish(4),shouce(5),user(6),android(7),loc(测试环境) return currentHost }, createDom : function () { // 创建蒙层 var divWrap = document.createElement('div'); divWrap.id = 'activity'; // 将html片段插入蒙层中 divWrap.innerHTML = this.htmlString; var body = document.getElementsByTagName('body')[0]; // 将蒙层整体插入body中 body.appendChild(divWrap); // 往body中插入元素 document.getElementById('activity').style.cssText = 'position:fixed;top:0;left:0;z-index:100001;width:100vw;height:100vh;background: rgba(0,0,0,.3);'; // 写入蒙层样式,兼容IE if (this.isImg) { document.getElementById('activity').style.display = "none"; } this.aftercreateDom(); }, aftercreateDom : function () { var _self = this; this.closeActivity(); this.activityHref(); // console.log('--------------------------aftercreateDom--------------------------'); }, tocenter : function toCenter(center,content) { console.log(content) var width = document.getElementById(content).clientWidth; var height = document.getElementById(content).clientHeight; var str = 'margin-left:-'+width/2+'px;margin-top:-'+height/2+'px;'; document.getElementById(center).style.cssText = str + 'position:absolute;top:50%;left:50%;'; }, activityHref : function(){ var that = this; document.getElementById('activity-a').onclick = function () { base.countIndex('广告弹框'+that.index,'整体点击','页面'); } }, closeActivity : function () { var that = this; document.getElementById('close-activity').onclick = function () { base.countIndex('广告弹框'+that.index,'关闭按钮','按钮'); document.getElementById('activity').style.display = "none"; } }, beforecreateDom : function () { var _self = this; this.createDom(); // console.log(this.url) var _img = document.getElementById('img-content'); _img.setAttribute('src',this.url); var _href = document.getElementById('activity-a'); _href.setAttribute('href',this.href) }, init:function(){ this.beforecreateDom(); } }; var Dates = function () { return { /** * 日期加N天,用法:DateAdd('2016-04-13', 3); */ DateAdd: function (dateTime, date) { var timestamp = Date.parse(dateTime); var nowTime = timestamp + 86400000 * date; return this.DateFormat(nowTime, 'YYYY-MM-DD'); }, /** * 相差多少天,用法:DateDiff('2016-04-17','2016-04-13'); */ DateDiff: function (dateTime, dateTime2) { var timestamp = Date.parse(dateTime); var timestamp1 = Date.parse(dateTime2); var nowTime = timestamp - timestamp1; return nowTime / 86400000; }, /** * 日期格式化,用法:DateFormat(86400000, 'YYYY-MM-DD') */ DateFormat: function (timestamp, format) { var Dates = {}; Dates.digit = function (num) { return num < 10 ? '0' + (num | 0) : num; }; Dates.parse = function (ymd, hms, format) { ymd = ymd.concat(hms); format = format || (Dates.options ? Dates.options.format : config.format); return format.replace(/YYYY|MM|DD|hh|mm|ss/g, function (str, index) { ymd.index = ++ymd.index | 0; return Dates.digit(ymd[ymd.index]); }); }; var De = new Date((timestamp | 0) ? function (tamp) { return tamp < 86400000 ? ( +new Date + tamp * 86400000) : tamp; }(parseInt(timestamp)) : +new Date); return Dates.parse( [De.getFullYear(), De.getMonth() + 1, De.getDate()], [De.getHours(), De.getMinutes(), De.getSeconds()], format ); } } }; var base = { countIndex:function countIndex(page,name,type,remark){ var param={ 'page':page || '', 'name':name || '', 'type':type || '' // 'remark':remark || '-' }; $.ajax({ url: 'http://www.kuchuan.com/log', type: 'get', data: param, success:function(data){ if(200 == data.status){ //console.log(param); }else{ console.log("countIndex status error"); } }, error:function(){ console.log('countIndex error'); } }); }, }; var initRule = function(conf){ var rule = function(){ var _this = this; if(conf.reflow){ window.localStorage.removeItem('activity1'); } var config = JSON.parse(window.localStorage.getItem('activity1')) || conf; if(config.url.length !== conf.url.length || config.day !== conf.day){ config = conf //重新渲染 } config.temp = '
'; var nowStr = new Date().getTime(); var nowTime = Dates().DateFormat(nowStr,'YYYY-MM-DD'); var _day = parseInt(config['day'] * config.url.length); var currentItem = config.currentItem; //当前展示的图片 config.ftime = config.ftime || nowTime; config.ntime = config.ntime || nowStr; var countTime = Dates().DateAdd(config.ftime,_day-1); if(nowStr - config.ntime < 86400000 && config.ntime !== nowStr){ config.isOneDay = true; }else{ config.isOneDay = false; // Cookies.set('kuchuanActity','false',{domain:'.kuchuan.com'}) } // var _flag = Cookies.get('kuchuanActity') == 'true'; //第一次进入的时间 2017-5-2 // var curTime = GetDateStr(parseInt(config['day'] * currentItem)) // 当前时间戳 // console.log(curTime) // console.log(countTime,'展示结束日期') // console.log(config.ftime,'第一次进入的时间'); // console.log(nowTime,'今天的日期') // console.log(config.day,'每张展示几天') // console.log(config.url.length,'总共展示多少张') var diffDate = Dates().DateDiff(nowTime,config.ftime); // 28天 总共展示30天 每张10天 var _index = Math.floor(diffDate/config.day); var _inx = parseInt(_index)+1; // console.log(config.flag,nowTime <=config.ftime,!config.isOneDay) if(config.flag && nowTime <= config.ftime && !config.isOneDay){ var a1 = new SupActivity({ temp:config.temp, url : config.url[0], href: config.href[0], index: _inx }); console.log(_index,_inx) base.countIndex('广告弹框'+_inx,'整体流量','页面'); config.isShow = true; } else if(config.flag && nowTime > config.ftime && nowTime <= countTime && !config.isOneDay){ var a1 = new SupActivity({ temp:config.temp, url:config.url[_index], href:config.href[_index], index:_inx }); config.ntime = nowStr; config.isShow = true; base.countIndex('广告弹框'+_inx,'整体流量','页面'); // $.cookie('kuchuanActity','true',{domain:'.kuchuan.com'}); // config.isOneDay = true; }else if(config.flag && nowTime > countTime){ config.flag = !config.flag; // console.log('我是最后的结局') } window.localStorage.setItem('activity1', JSON.stringify(config)) }; rule(); } //'http://wpa.qq.com/msgrd?v=3&uin=3277439952&site=qq&menu=yes' var _config = { url : ['http://cdn.coolguang.com/public/main-site/images/float/activity_all_20181119.png'], //href:['http://www.kuchuan.com/client'], href:['http://cdn.coolguang.com/public/douyutuiguang/ps_v4/'], currentItem:1, day : 3, flag:true, isShow:false } // var isShow = false; var activityNum = window.localStorage.getItem('activityNum1')||parseInt(Math.random() * 100); if(activityNum <= 100){ initRule(_config); } window.localStorage.setItem('activityNum1',activityNum);