图片左右浮动底部浮动

本文介绍了一种通过JavaScript实现网页中浮动元素动态定位的方法。该方法能够使浮动元素随着页面滚动而自动调整位置,确保用户在浏览不同区域时仍能看到这些元素。文章详细解释了实现原理,并提供了具体代码示例。

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

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>无标题页</title>
</head>
<body style="height:1500px">


<!--右边-->
<div id="divRight">
<style type="text/css">

  #divMsgInfo{
 position:absolute;

 width:90px;
 height:300px;
 font-size:9pt;


 
}
  </style>
 <script type="text/javascript">
 var lastScrollY=0;
 function heartBeat(){
  diffY=document.documentElement.scrollTop;
  percent=0.1*(diffY-lastScrollY);
  if(percent>0)percent=Math.ceil(percent);
  else percent=Math.floor(percent);
  document.getElementById("divMsgInfo").style.top=parseInt(document.getElementById("divMsgInfo").style.top)+percent+"px";
  lastScrollY=lastScrollY+percent;
 }
 var timeright = window.setInterval("heartBeat()",1);
</script>

   <div id="divMsgInfo"  runat="server" style=" right:10px; top:120px;">
        <img src ="left.gif" />
           <div style="float:right; color:White"><a onclick="clearInterval(timeright);document.getElementById('divRight').innerHTML = ''" style="color:White; cursor:pointer">关闭</a></div>
   </div>
</div>

 

<!--左边-->
<div id="divLeftFloat">
<style type="text/css"> 
 #divMsgLeftInfo{ 
 position:absolute; 
 width:90px;
  height:318px;  
  font-size:9pt;  }  
  </style>    
  <script type="text/javascript">  
  var lastScrollYLeft=0; 
   function heartBeatLeft()
   {   
     diffYLeft=document.documentElement.scrollTop;   
     percent=0.1*(diffYLeft-lastScrollYLeft);   
     if(percent>0)
        percent=Math.ceil(percent);    
     else percent=Math.floor(percent);    
     document.getElementById("divMsgLeftInfo").style.top=parseInt(document.getElementById("divMsgLeftInfo").style.top)+percent+"px";    
     lastScrollYLeft=lastScrollYLeft+percent;    }  
     var timeleft = window.setInterval("heartBeatLeft()",1); 
     </script>    
    
     <div id="divMsgLeftInfo" style=" left:10px; top:120px;">
     <a href="http://www.ispeak.cn" target="_blank">
     <img border="0" src ="/Files/index/FloatImg/20101112115620528353991.png" /></a>              
     <div style="float:right; color:White">
     <a onclick="clearInterval(timeleft);document.getElementById('divLeftFloat').innerHTML = ''" style="color:White; cursor:pointer">关闭</a></div>   
     </div>  </div>

 

<!--底部-->
<div id="divBottomFloat">
<style type="text/css">

  #divMsgBottomInfo{
 position:absolute;
 /*background:#eee;*/
 width:330px;
 height:188px;
 font-size:9pt;
/* border-color:#442200;*/
/* border-color:#000000;*/
 
}
  </style>
 

   <script type="text/javascript">
 var lastScrollYBottom=0;
 function heartBeatBottom(){

var isIE = document.all && window.external;
 bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;

  if(isIE){
   docHeight = document.documentElement.clientHeight || document.body.clientHeight;
  }
  else{
   docHeight = (document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight;
  }
 
  diffYBottom=(docHeight - parseInt(document.getElementById("divMsgBottomInfo").offsetHeight,10)) + parseInt(bodyScrollTop, 10);
  document.getElementById("divMsgBottomInfo").style.top=diffYBottom+"px";

 }
 var timebottom=window.setInterval("heartBeatBottom()",1);
</script>

   <div id="divMsgBottomInfo"  runat="server" style=" right:10px; top:500px;">
       <div style="float:right; color:White"><a onclick="clearInterval(timebottom);document.getElementById('divBottomFloat').innerHTML = ''" style="color:White; cursor:pointer">关闭</a></div>
        <img src ="/images/tmp2.jpg" style="width:330px; height:170px" />  
   
   </div>
</div>


</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值