我尝试使用IScroll库中的scrollToElement函数,但我无法使其工作。
new IScroll(document.querySelector(".wrapper")).scrollToElement(document.querySelector("h2"));

.wrapper {
background-color: red;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/iScroll/5.1.3/iscroll.js"></script>
<div class="wrapper">
<h1>This is H1</h1>
<h1>This is H2</h1>
<h1>This is H3</h1>
<h1>This is H4</h1>
<h1>This is H5</h1>
<h1>This is H6</h1>
<h1>This is H7</h1>
<h1>This is H8</h1>
<h1>This is H9</h1>
<h1>This is H10</h1>
<h1>This is H11</h1>
<h2>This is H12</h2>
<h1>This is H13</h1>
<h1>This is H14</h1>
<h1>This is H15</h1>
<h1>This is H16</h1>
<h1>This is H17</h1>
<h1>This is H18</h1>
<h1>This is H19</h1>
</div>
&#13;
正如您所看到的,我只想尝试滚动到h2元素,但没有任何反应。
我做错了什么?
提前致谢!
答案 0 :(得分:1)
您需要稍微更改HTML。检查此fiddle。 HTML和CSS中有一些要求IScroll可以使用。
var test = new IScroll(".wrapper").scrollToElement("h2");
&#13;
.wrapper {
position: relative;
}
#scroller {
position: absolute;
}
&#13;
<body>
<div class="wrapper">
<div id="scroller">
<h1>This is H1</h1>
<h1>This is H2</h1>
<h1>This is H3</h1>
<h1>This is H4</h1>
<h1>This is H5</h1>
<h1>This is H6</h1>
<h1>This is H7</h1>
<h1>This is H8</h1>
<h1>This is H9</h1>
<h1>This is H10</h1>
<h1>This is H11</h1>
<h2>This is H12</h2>
<h1>This is H13</h1>
<h1>This is H14</h1>
<h1>This is H15</h1>
<h1>This is H16</h1>
<h1>This is H17</h1>
<h1>This is H18</h1>
<h1>This is H19</h1>
</div>
</div>
</body>
&#13;