scrollIntoView 让元素滚动到视口 顶部 底部 中间

 

element的接口

partial interface Element {
  DOMRectList getClientRects();
  [NewObject] DOMRect getBoundingClientRect();
  void scrollIntoView(optional (boolean or ScrollIntoViewOptions) arg);
  void scroll(optional ScrollToOptions options);
  void scroll(unrestricted double x, unrestricted double y);
  void scrollTo(optional ScrollToOptions options);
  void scrollTo(unrestricted double x, unrestricted double y);
  void scrollBy(optional ScrollToOptions options);
  void scrollBy(unrestricted double x, unrestricted double y);
  attribute unrestricted double scrollTop;
  attribute unrestricted double scrollLeft;
  readonly attribute long scrollWidth;
  readonly attribute long scrollHeight;
  readonly attribute long clientTop;
  readonly attribute long clientLeft;
  readonly attribute long clientWidth;
  readonly attribute long clientHeight;
};

 原生的dom对象scrollIntoView方法就是让这个滑进视口参数 boolean 或者 ScrollIntoViewOptions

enum ScrollLogicalPosition { "start", "center", "end", "nearest"};dictionary ScrollIntoViewOptions
 : ScrollOptions { ScrollLogicalPosition block = "start"; ScrollLogicalPosition inline = "nearest";};

写法

demo.scrollIntoView({block:"start"})//开始位置
    demo.scrollIntoView({block:"center"})//中间位置
    demo.scrollIntoView({block:"end"})//末尾位置

demo 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <!-- Apple devices fullscreen -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <!-- Apple devices fullscreen -->
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Title</title>
    <style>
        ul > li {
            height: 100px;
            border-bottom: 1px solid #000000;
            line-height: 100px;
            display: inline-block;
            width: 100%;
        }

        #demo {
            background-color: #f00;

        }
    </style>
</head>
<body>
<div class="box" style="width: 300px;height: 300px; border: 1px solid #000;overflow: auto">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
        <li>11</li>
        <li>12</li>
        <li id="demo">13</li>
        <li><a href="#" id="demo1" style="color: #00ff00;">demo</a></li>
        <li>15</li>
        <li>16</li>
        <li>17</li>
        <li>18</li>
        <li>19</li>
        <li>20</li>
    </ul>
</div>
</body>
<script>
    var demo = document.getElementById("demo");
    demo.scrollIntoView({block:"center"})
</script>
</html>

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值