页面浮窗简单实现

本文介绍了如何使用HTML和JavaScript实现页面浮窗。通过设置样式和JavaScript事件监听,实现在页面滚动时保持浮窗的位置。根据屏幕高度设置了不同分辨率下浮窗的定位。

1.html代码:

<div id="jdSmall" style="position: absolute; right: 0px;top :screen.height-450;  z-index: 9999;">

    <a href="#">
        <img class="img_piaochuang1" src="">
    </a>
    <a href="javascript:void(0)" id="ewm-close" style="width:27px; height:21px; position:absolute; right:0; top:0; background:url()) no-repeat right 0;"></a>

</div>

2.js代码:

        $(function(){
           $("#ewm-close").click(function(){
               $(this).parent().hide();
           });

          //相对固定悬浮窗位置
           function scrollx(p) { 
var d = document, dd = d.documentElement, db = d.body, w = window, o = d.getElementById(p.id), ie6 = /msie 6/i.test(navigator.userAgent), style, timer; 
if (o) { 
 cssPub = ";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.t!=undefined?'top:'+p.t+'px;':'bottom:0;'); 
 if (p.r != undefined && p.l == undefined) { 
  o.style.cssText += cssPub + ('right:'+p.r+'px;'); 
 } else { 
  o.style.cssText += cssPub + ('margin-left:'+p.l+'px;'); 
 } 
 if(p.f&&ie6){ 
  cssTop = ';top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );'; 
  cssRight = ';right:expression(documentElement.scrollright + '+(p.r==undefined?dd.clientWidth-o.offsetWidth:p.r)+' + "px")'; 
  if (p.r != undefined && p.l == undefined) { 
   o.style.cssText += cssRight + cssTop; 
  } else { 
   o.style.cssText += cssTop; 
  } 
  dd.style.cssText +=';background-image: url(about:blank);background-attachment:fixed;'; 
 }else{ 
  if(!p.f){ 
   w.onresize = w.onscroll = function(){ 
    clearInterval(timer); 
    timer = setInterval(function(){ 
     //双选择为了修复chrome 下xhtml解析时dd.scrollTop为 0 
     var st = (dd.scrollTop||db.scrollTop),c; 
     c = st - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||dd.clientHeight)-o.offsetHeight); 
     if(c!=0){ 
      o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px'; 
     }else{ 
      clearInterval(timer); 
     } 
    },10) 
   } 
  } 
 } 


if(screen.height>800){//粗暴地处理分辨率对浮窗的影响
  scrollx({id:'jdSmall', r:0, t:screen.height-640, f:1});
}else if(screen.height>720){
scrollx({id:'jdSmall', r:0, t:screen.height-545, f:1});
}else{
scrollx({id:'jdSmall', r:0, t:screen.height-460, f:1});
}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值