DOM的滚动

DOM规范中并没有规定各浏览器需要实现怎样的滚动页面区域,各浏览器实现了相应的方法,可以使用不同的方式控制页面区域的滚动。这些方法作为HTMLElement类型的扩展存在,所以它能在所有元素上使用。

1、scrollIntoView(alignWithTop)  滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素。如果alignWithTop为true,或者省略它,窗口会尽可能滚动到自身顶部与元素顶部平齐。-------目前各浏览器均支持

2、scrollIntoViewIfNeeded(alignCenter) 只在当前元素在视窗的可见范围内不可见的情况下,才滚动浏览器窗口或容器元素,最终让当前元素可见。如果当前元素在视窗中可见,这个方法不做任何处理。如果将可选参数alignCenter设置为true,则表示尽量将元素显示在视窗中部(垂直方向)------Safari、Chrome实现了这个方法

3、scrollByLines(lineCount) 将元素的内容滚动指定的行数的高度,lineCount的值可以为正值或是负值。---Safari、Chrome实现了这个方法

4、scrollByPages(pageCount) 将元素的内容滚动指定的页面的高度,具体高度由元素的高度决定。---Safari、Chrome实现了这个方法

 

scrollIntoView()和scrollIntoVIewIfNeeded()作用的是元素的窗口,而scrollByLines()、scrollByPages()影响元素自身,下面是几个示例:

//将页面主体滚动5行

document.body.scrollByLines(5);

 

//确保当前元素可见

document.getElementById(“test”).scrollIntoView();

 

//确保只在当前元素不可见的情况下才使其可见

document.getElementById(“test”).scrollIntoViewIfNeeded();

 

//将页面主体往回滚1页

doument.body.scrollByPages(-1);

由于只有scrollIntoView被各浏览器均支持,所以这个方法最为常用。

/***************************/

以上内容摘自《JavaScript高级程序设计(第2版)》

/*************************/

以前看QQ邮箱有一个效果,研究一番后找到使用scrollIntoView做到的,示例再放一个。

<!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>ScrollIntoView</title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
* {margin:0; padding:0;}
body {background-color:#FFFFFF;}
td,input,button,select,body {font-family:"lucida Grande",Verdana;font-size:12px;}
.ico_big{float:left;margin:2px 8px 0 0;}
img{border:none;}
.toolbg a, .attbg a {color:#244281;}
.txt_left{text-align:left;}
.attbg {background-color:#e8edf4;}
.toolbg {background-color:#D2DBEA;}
</style>
</head>
<body>
<div style="margin:10px auto; width:500px;padding:50px; border:1px solid #ccc;">
<span onClick="ScrollToAttach();" style="cursor:pointer">
<a id="linkAttachment"><img src="http://m369.mail.qq.com/zh_CN/htmledition/images/newicon/fj_min/fu_rar.gif" align="absmiddle" />可以点击我试试!</a>
</span>
<div style="margin:500px;"></div>
<div id="attachment" style="padding:2px;margin-bottom:15px;*margin-bottom:5px;display: color:#666;" class="attbg">
<div style="padding:6px 0 10px 6px;" class="txt_left">
   <b style="font-size:14px;">
    <img src="http://res.qqmail.com/zh_CN/htmledition/images/icon_att3476l.gif" align="absmiddle" class="showattch" border="0"/> 附件
   </b>(<span id="attachmentCount">1</span> 个)
</div>
<div style="padding:0 8px 6px 12px;background:#fff;line-height:140%;">
   <div class="graytext clr" style="padding-top:12px;">
    <b style="color:#000;font-weight:bold;font-size:12px;" >普通附件</b>
    <div style="padding:1px 0 0 0; color:#666;">(已通过卡巴斯基杀毒引擎扫描)</div>
   </div>
   <div style="margin-top:10px;">
    <div class="ico_big">
     <a href="javascript:;">
      <img style="width:auto;" src="http://m369.mail.qq.com/zh_CN/htmledition/images/newicon/fj_max/fu_rar.gif"/>
     </a>
    </div>
    <div class="name_big">
     <span>ecomstore.tar</span><span class="graytext"> (1.46M)</span>
     <div class="down_big" >
      <a title="请直接点击或鼠标右键转下载工具打开,请不要拖拽到下载工具悬浮框中" href="javascript:;" >下载</a>  
      <a href="javascript:;">打开</a>  
      <a href="javascript:;">在线预览</a>  
      <a href="javascript:;">保存到我的随身盘</a>
     </div>
    </div>
   </div>
   <div class="clr" style="height:8px;overflow:hidden"></div>
</div>
</div>
<div style="margin:900px;"></div>
</div>
<button style="position:absolute; top:600px; left:100px; width:200px;" οnclick="scrollIntoViewHandler(this)">使用scrollIntoView:true</button>
<script type="text/javascript">
    
    function scrollIntoViewHandler(_this) {
       if(_this.flag) {
        _this.innerHTML = _this.innerHTML.replace(/false/, 'true');
        _this.flag = false;
       } else {
        _this.innerHTML = _this.innerHTML.replace(/true/, 'false');
        _this.flag = true;
       }
      document.getElementById("attachment").scrollIntoView(_this.flag);
   }
   function ScrollToAttach(vq) {
   vq = vq || "attachment";
   var bw = document.getElementById(vq);
   if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
      //bw.scrollIntoView(document.documentElement.clientHeight < bw.offsetHeight);
      bw.scrollIntoView(false);
   }
   Splash(vq, 4);
   }
   /**
   * 滚动至...
   * @param {Object} vq
   * @param {Object} times
   * @param {Object} isNotInit
   */
   function Splash(vq, times, isNotInit) {
       var ah = arguments.callee,
      afx = document.getElementById(vq);
       if (!isNotInit || !ah.time) {
           ah.orgclass = afx.className;
           ah.time = 0;
  }
       afx.className = (ah.time % 2 == 0) ? "toolbg": ah.orgclass;
       if (++ah.time < times) {
           setTimeout(function() {
               Splash(vq, times, true);
          }, 70);
      }
}
</script>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值