飘窗管理

var browser={
  ie6:function(){return((window.XMLHttpRequest==undefined)&&(ActiveXObject!=undefined))},
  getWindow:function(){
    var myHeight=0;
    var myWidth=0;
    if(typeof(window.innerWidth)=='number'){
      myHeight=window.innerHeight;
      myWidth=window.innerWidth
    }else if(document.documentElement){
      myHeight=document.documentElement.clientHeight;
      myWidth=document.documentElement.clientWidth
    }else if(document.body){
      myHeight=document.body.clientHeight;
      myWidth=document.body.clientWidth
    }
    return{'height':myHeight,'width':myWidth}
  },
  getScroll:function(){
    var myHeight=0;
    var myWidth=0;
    if(typeof(window.pageYOffset)=='number'){
      myHeight=window.pageYOffset;
      myWidth=window.pageXOffset
    }else if(document.documentElement){
      myHeight=document.documentElement.scrollTop;
      myWidth=document.documentElement.scrollLeft
    }else if(document.body){
      myHeight=document.body.scrollTop;
      myWidth=document.body.scrollLeft
    }
    return{'height':myHeight,'width':myWidth}
  },
  getDocWidth:function(D){
    if(!D)var D=document;
    return Math.max(Math.max(D.body.scrollWidth,D.documentElement.scrollWidth),Math.max(D.body.offsetWidth,D.documentElement.offsetWidth),Math.max(D.body.clientWidth,D.documentElement.clientWidth))
  },
  getDocHeight:function(D){
    if(!D)var D=document;
    return Math.max(Math.max(D.body.scrollHeight,D.documentElement.scrollHeight),Math.max(D.body.offsetHeight,D.documentElement.offsetHeight),Math.max(D.body.clientHeight,D.documentElement.clientHeight))
  }
};

var dom={
  ID:function(id){
    var type=typeof(id);
    if(type=='object')return id;
    if(type=='string')return document.getElementById(id);
    return null
  },
  insertHtml:function(html){
    var frag=document.createDocumentFragment();
    var div=document.createElement("div");
    div.innerHTML=html;
    for(var i=0,ii=div.childNodes.length;i<ii;i++){
      frag.appendChild(div.childNodes[i])
    }
    document.body.insertBefore(frag,document.body.firstChild)
  }
};

var myEvent={
  add:function(element,type,handler){
    var ele=dom.ID(element);
    if(!ele)return;
    if(ele.addEventListener)ele.addEventListener(type,handler,false);
    else if(ele.attachEvent)ele.attachEvent("on"+type,handler);
    else ele["on"+type]=handler
  },
  remove:function(element,type,handler){
    var ele=dom.ID(element);
    if(!ele)return;
    if(ele.removeEventListener)ele.removeEventListener(type,handler,false);
    else if(ele.detachEvent)ele.detachEvent("on"+type,handler);
    else ele["on"+type]=null
  }
};

var position={
  rightCenter:function(id,hi,wi){
    var id=dom.ID(id);
    var ie6=browser.ie6();
    var win=browser.getWindow();
    var ele={'height':id.clientHeight,'width':id.clientWidth};
    if(ie6){
      var scrollBar=browser.getScroll()
    }else{
      var scrollBar={'height':0,'width':0};
      id.style.position='fixed'
    }
    ele.top=parseInt((win.height-ele.height)/2+scrollBar.height);
    // id.style.top=ele.top+'px';
    // id.style.right='3px'
    id.style.top=hi+'px';
    id.style.right=wi+'px'
  },
  floatRightCenter:function(id,hi,wi){
    position.rightCenter(id,hi,wi);
    var fun=function(){
      position.rightCenter(id,hi,wi)
    };
    if(browser.ie6()){
      myEvent.add(window,'scroll',fun);
      myEvent.add(window,'resize',fun)
    }else{
      myEvent.add(window,'resize',fun)
    }
  },
  leftCenter:function(id,hi,wi){
    var id=dom.ID(id);
    var ie6=browser.ie6();
    var win=browser.getWindow();
    var ele={'height':id.clientHeight,'width':id.clientWidth};
    if(ie6){
      var scrollBar=browser.getScroll()
    }else{
      var scrollBar={'height':0,'width':0};
      id.style.position='fixed'
    }
    ele.top=parseInt((win.height-ele.height)/2+scrollBar.height);
    // id.style.top=ele.top+'px';
    // id.style.left='3px'
    id.style.top=hi+'px';
    id.style.left=wi+'px'
  },
  floatLeftCenter:function(id,hi,wi){
    position.leftCenter(id,hi,wi);
    var fun=function(){
      position.leftCenter(id,hi,wi)
    };
    if(browser.ie6()){
      myEvent.add(window,'scroll',fun);
      myEvent.add(window,'resize',fun)
    }else{
      myEvent.add(window,'resize',fun)
    }
  },
  moveSpend:function(id,hi,wi,tm,stepx,stepy){
    var x = parseInt(wi),y = parseInt(hi)
    var sx = parseInt(stepx),sy = parseInt(stepy)
    var position = {left: parseInt(wi), top: parseInt(hi)};//飘窗位置对象
    var angle= Math.PI/4;
    // var time = 10;//控制飘窗运动效果,值越小越细腻
    // var step = 100 * (time/1000);//计算运动步长
    var decoration = {x:sx*Math.cos(angle), y:sy*Math.sin(angle)};//计算二维上的运动增量
    var obj=$("#"+id);
    var d_w= obj.width();//当前广告的宽
    var d_h = obj.height();//高
    var w_w = parseInt(document.body.clientWidth);
    var w_h = parseInt(document.body.clientHeight);
    var max_l = w_w - d_w;
    var max_t = w_h - d_h;
    function isTop(pos, w_w, w_h, d_w, d_h){//飘窗到达上边距
      return (pos.top<=0) ? true : false;
    }
    function isBottom(pos, w_w, w_h, d_w, d_h){//飘窗到达下边距
      return (pos.top>=(w_h-d_h)) ? true : false;
    }
    function isLeft(pos, w_w, w_h, d_w, d_h){//飘窗到达左边距
      return (pos.left<=0) ? true : false;
    }
    function isRight(pos, w_w, w_h, d_w, d_h){//飘窗到达右边距
      return (pos.left>=(w_w-d_w)) ? true : false;
    }
    // obj.find('img.float_ad_img').load(function(){
      tm = function(){
        position.left += decoration.x;
        position.top += decoration.y;
        if(isLeft(position, w_w, w_h, d_w, d_h)||isRight(position, w_w, w_h, d_w, d_h)){
          decoration.x = -1*decoration.x;
        }
        if(isRight(position, w_w, w_h, d_w, d_h)){
          position.left = max_l;
        }
        if(isTop(position, w_w, w_h, d_w, d_h)||isBottom(position, w_w, w_h, d_w, d_h)){
          decoration.y = -1*decoration.y;
        }
        if(isBottom(position, w_w, w_h, d_w, d_h)){
          position.top = max_t;
        }
        // obj.animate({left:position.left, top:position.top}, time);//改用jquery动画函数使其更加平滑
        obj.css({'top':position.top,'left':position.left});
      }
      var itl = setInterval(tm,10);
      $('#'+id).mouseover(function(){clearInterval(itl)});
      $('#'+id).mouseout(function(){itl=setInterval(tm, 10)} )
   // });
  }
};
function gethtml(i,dt,id){
  var html;
  html = '<div id="'+id+'" style="position:absolute;width:'+dt.floatwinwidth+'px;height:'+dt.floatwinheight+'px;z-index:10001">';
  if(dt.hasclose==1){
    html+='<a style="" href="javascript:void(0);" onclick="document.getElementById(\''+id+'\').style.display=\'none\'">关闭</a>';
  }
  if(dt.floatwinurl==undefined||dt.floatwinurl==""||dt.floatwinurl==null){
    html+='<a>';
  }else if(dt.floatwinurl.substr(0,5)=="http:"||dt.floatwinurl.substr(0,5)=="https"){
    html+='<a href="'+dt.floatwinurl+'">';
  }else{
    html+='<a href="index.do?method=forwardNewPage&page='+dt.floatwinurl+'">';
  }
  return html+='<img src="pic.do?method=getImgById&picid='+dt.picid+'"width="'+dt.floatwinwidth+'" height="'+dt.floatwinheight+'" /></a></div>';
}

function floatwinlist(){
  $.ajax({
    type: "post",
    url: 'floatwin.do?method=getFloatwinList&floatwinto=1',
    dataType: 'json',
    success: function(data){
      if(data.code=="1"){
        var dt=data.data;
        for(var i=0;i<dt.length;i++){
          if(dt[i].floatwinstyle==0){
            var html=gethtml(i,dt[i],"ad_left"+i);
            dom.insertHtml(html);position.floatLeftCenter('ad_left'+i,dt[i].floatwiny,dt[i].floatwinx);
          }else if(dt[i].floatwinstyle==1){
            var html=gethtml(i,dt[i],"ad_right"+i);
            dom.insertHtml(html);position.floatRightCenter('ad_right'+i,dt[i].floatwiny,dt[i].floatwinx);
          }else if(dt[i].floatwinstyle==2){
            var html=gethtml(i,dt[i],"ad_right"+i);
            dom.insertHtml(html);
            position.moveSpend('ad_right'+i,dt[i].floatwiny,dt[i].floatwinx,"tm"+i,1,1);
          }else if(dt[i].floatwinstyle==3){
            var html=gethtml(i,dt[i],"ad_left"+i);
            dom.insertHtml(html);
            position.floatLeftCenter('ad_left'+i,dt[i].floatwiny,dt[i].floatwinx);
            var html1=gethtml(i,dt[i],"ad_lefta"+i);
            dom.insertHtml(html1);
            position.floatRightCenter('ad_lefta'+i,dt[i].floatwiny,dt[i].floatwinx);
          }else if(dt[i].floatwinstyle==4){
            var html=gethtml(i,dt[i],"ad_right"+i);
            dom.insertHtml(html);
            var stepx=(parseInt(Math.random()*3)+1);
            var stepy=(parseInt(Math.random()*3)+1);
            position.moveSpend('ad_right'+i,dt[i].floatwiny,dt[i].floatwinx,"tm"+i,stepx,stepy);
          }
        }
      }
    }
  })
}
//工具函数判断边距

posted on 2018-09-12 13:55  cwone  阅读(196)  评论(0编辑  收藏  举报