简体   繁体   English

IScroll scrollToElement不起作用

[英]IScroll scrollToElement not working

I'm trying to use the scrollToElement function from the IScroll library, but I can't get it to work. 我正在尝试使用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> 

As you can see, I'm just trying to scroll to the h2 element, but nothing happens. 如您所见,我只是尝试滚动到h2元素,但没有任何反应。

What am I doing wrong? 我究竟做错了什么?

Thanks in advance! 提前致谢!

jsfiddle jsfiddle

You need to slightly change your HTML. 您需要稍微更改HTML。 Check this fiddle . 检查这个小提琴 There are some requirements in HTML and CSS for IScroll to work. HTML和CSS中有一些要求才能使IScroll正常工作。

 var test = new IScroll(".wrapper").scrollToElement("h2"); 
 .wrapper { position: relative; } #scroller { position: absolute; } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM