MSN弹出式窗口popup的JS实现

本文介绍了一种在博客中实现浮动弹窗效果的方法,通过JavaScript和CSS实现自定义弹窗的显示位置及动画效果,适用于博客园等平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文参考 【玉开】的blog。
效果图:

实现这样的效果步骤如下:
1.把下面的代码添加到博客园管理-选项-Configure-页首Html代码
None.gif < script language = " javascript " >
None.gif
var  divTop,divLeft,divWidth,divHeight,docHeight,docWidth,i  =   0 ;
None.gif
var  eMsg  =   new  Object();
None.gifeMsg.id 
=  'eMsg';
ExpandedBlockStart.gifContractedBlock.gifeMsg.obj 
=   function () dot.gif {return document.getElementById(eMsg.id);} ;
None.gif
ExpandedBlockStart.gifContractedBlock.gifeMsg.onLoad 
=   function () dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
trydot.gif{
InBlock.gif        divTop 
= parseInt(eMsg.obj().style.top,10); 
InBlock.gif        divLeft 
= parseInt(eMsg.obj().style.left,10);
InBlock.gif        divHeight 
= parseInt(eMsg.obj().offsetHeight,10);
InBlock.gif        divWidth 
= parseInt(eMsg.obj().offsetWidth,10);
InBlock.gif        docWidth 
= document.body.clientWidth;
InBlock.gif        docHeight 
= document.body.clientHeight;//内容高度
InBlock.gif
        //alert(document.body.scrollTop);
InBlock.gif
        //return;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif        alert("top.window.screen.availHeight "+docHeight);
InBlock.gif        alert("top.window.screenTop "+top.window.screenTop);        
InBlock.gif        alert("document.body.clientHeight "+document.body.clientHeight);        
InBlock.gif        alert("document.body.clientWidth "+docWidth);        
InBlock.gif        alert("document.documentElement.scrollTop "+document.documentElement.scrollTop);        
ExpandedSubBlockEnd.gif
*/

InBlock.gif        eMsg.obj().style.top 
= parseInt(document.body.scrollTop,10+ docHeight + 10;//parseInt(document.body.scrollTop,10)被document.documentElement.scrollTop替换
InBlock.gif
        eMsg.obj().style.left = parseInt(document.body.scrollLeft,10+ docWidth - divWidth;
InBlock.gif        eMsg.obj().style.visibility
="visible";
InBlock.gif        eMsg.timer 
= window.setInterval(eMsg.move,2);
InBlock.gif       
InBlock.gif        
//hp.skin.onBeforChange = function(){
InBlock.gif
        //    if(eMsg.obj())eMsg.obj().parentNode.removeChild(eMsg.obj());
InBlock.gif
        //};
ExpandedSubBlockEnd.gif
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch(e)dot.gif{}
ExpandedBlockEnd.gif}
;
ExpandedBlockStart.gifContractedBlock.gifeMsg.onResize 
=   function () dot.gif {
InBlock.gif    i
+=1;
InBlock.gif    
//alert(i);
InBlock.gif
    if(i>1000) eMsg.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
trydot.gif{
InBlock.gif        divHeight 
= parseInt(eMsg.obj().offsetHeight,10);
InBlock.gif        divWidth 
= parseInt(eMsg.obj().offsetWidth,10);
InBlock.gif        docWidth 
= document.body.clientWidth;
InBlock.gif        docHeight 
= document.body.clientHeight;
InBlock.gif        eMsg.obj().style.top 
= docHeight - divHeight + parseInt(document.body.scrollTop,10);//parseInt(document.body.scrollTop,10)被document.documentElement.scrollTop替换
InBlock.gif
        eMsg.obj().style.left = docWidth - divWidth + parseInt(document.body.scrollLeft,10);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch(e)dot.gif{}
ExpandedBlockEnd.gif}
;
ExpandedBlockStart.gifContractedBlock.gifeMsg.move 
=   function () dot.gif {
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(parseInt(eMsg.obj().style.top,10<= (docHeight - divHeight + parseInt(document.body.scrollTop,10)))//parseInt(document.body.scrollTop,10)被document.documentElement.scrollTop替换
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            window.clearInterval(eMsg.timer);
InBlock.gif            eMsg.timer 
= window.setInterval(eMsg.onResize,1);
ExpandedSubBlockEnd.gif        }

InBlock.gif        divTop 
= parseInt(eMsg.obj().style.top,10);
InBlock.gif        eMsg.obj().style.top 
= divTop - 1;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch(e)dot.gif{}
ExpandedBlockEnd.gif}
;
ExpandedBlockStart.gifContractedBlock.gifeMsg.close 
=   function () dot.gif {
InBlock.gif
//alert('will close');
InBlock.gif
    if(eMsg.obj()) eMsg.obj().parentNode.removeChild(eMsg.obj());
InBlock.gif    
if(eMsg.timer) window.clearInterval(eMsg.timer);
ExpandedBlockEnd.gif}
;
ExpandedBlockStart.gifContractedBlock.gifeMsg.createInstance 
=   function (bodyHtml) dot.gif {
InBlock.gif    
//if(!titleHtml || !bodyHtml)throw '必须为titleHtml指定值,必须为bodyHtml指定值。';
InBlock.gif
  try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
if(eMsg.obj())eMsg.obj().parentNode.removeChild(eMsg.obj());
InBlock.gif    
if(eMsg.timer) window.clearInterval(eMsg.timer);   
InBlock.gif    i
=0;
InBlock.gif    
var odiv = document.createElement('DIV');
InBlock.gif    odiv.id 
= eMsg.id;
InBlock.gif    odiv.innerHTML 
= '<div class="eMsgInner">'
InBlock.gif    
+ '<div class="body" oncontextmenu="eMsg.close();return false;" title="右键关闭">'
InBlock.gif        
+ '<div class="content">'
InBlock.gif        
+ bodyHtml    
InBlock.gif        
+ '</div>'
InBlock.gif    
+ '</div>'
InBlock.gif    
+ '</div>';
InBlock.gif    document.body.appendChild(odiv);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
catch(e)dot.gif{}
ExpandedBlockEnd.gif}
;
None.gif
None.gifwindow.onresize 
=  eMsg.onResize;
None.gif
None.gif
function  eMsgPopup(bodyHtml)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifeMsg.createInstance(bodyHtml);
InBlock.gifeMsg.onLoad();
ExpandedBlockEnd.gif}

None.gif
function  myPopup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifeMsgPopup('
<div align=left>博主今日推荐:</div>'
InBlock.gif    
+'<br><div align=left><a href="http://live-bj.5d6d.com" target="_blank">北京混混网</a></div>');
ExpandedBlockEnd.gif}

None.gif
None.gif//window.onload
= myPopup();
None.gif
None.gif
</ script >

2.把下面的CSS样式增加到博客园管理-选项-Configure-通过CSS定制页面
ExpandedBlockStart.gif ContractedBlock.gif DIV#eMsg {dot.gif} {
InBlock.gif    background-image
: url(https://images.cnblogs.com/cnblogs_com/vagerent/popupBG.gif);
InBlock.gif    visibility
: hidden; 
InBlock.gif    width
: 199px; 
InBlock.gif    height
: 70px; 
InBlock.gif    position
: absolute; 
InBlock.gif    z-index
: 99999; 
InBlock.gif    left
: 0px;
ExpandedBlockEnd.gif
}

None.gifDIV#eMsg DIV.eMsgInner
ExpandedBlockStart.gifContractedBlock.gif
{dot.gif} {
InBlock.gif    background-image
: url(https://images.cnblogs.com/cnblogs_com/vagerent/popupBG.gif);
InBlock.gif    height
:70px;
InBlock.gif    width
:199px;
ExpandedBlockEnd.gif
}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDIV.body
{dot.gif} {
InBlock.gif    height
:72px;
InBlock.gif    clear
:both;
InBlock.gif    border-right
: #728eb8 1px solid; 
InBlock.gif    padding-top
: 1px; 
InBlock.gif    border-top
: #728eb8 1px solid;
InBlock.gif    border-left
: #728eb8 1px solid; 
InBlock.gif    color
: #1f336b; 
InBlock.gif    word-break
: break-all;
InBlock.gif    border-bottom
: #b9c9ef 1px solid;
ExpandedBlockEnd.gif
}

ExpandedBlockStart.gifContractedBlock.gifDIV.content
{dot.gif} {text-align:center;font-size: 12px;}
None.gif

3.现在就可以出效果了,不过需要修改那段JS代码,把下面这段修改成自己的文字就可以了。(还有css里面的背景图,最好从我的blog下载然后上传到你自己的blog)
None.gif function  myPopup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifeMsgPopup('
<div align=left>博主今日推荐:</div>'
InBlock.gif    
+'<br><div align=left><a href="http://live-bj.5d6d.com" target="_blank">北京混混网</a></div>');
ExpandedBlockEnd.gif}
4.具体效果,测试:)  或者点我blog左上角的卡马克的图片试试。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值