页面中某块区域可以用scrollTo吗

项目中要自己做翻页,查到了scrollTo这方法,感觉很不错,但菜鸟教程中是在window上用,想知道如下代码中的container上是否一样使用呢???如各位大神知道的话,请赐代码,跪谢
比如点了尾页,会从首页滚动到尾页,这样的效果

https://img.mukewang.com/5c480a9e0001ca3302700880.jpg

慕的地6264312
浏览 510回答 1
1回答

LEATH

第一种:页面中引入jq,加入脚本就可以了。<script>&nbsp; &nbsp; $("#pagi li").on('click',function () {&nbsp; &nbsp; &nbsp; &nbsp; var index=$(this).index();&nbsp; &nbsp; &nbsp; &nbsp; $("#container").scrollTop(index*405)&nbsp; &nbsp; });</script>第二种:原生实现&nbsp;for(var i=0;i<4;i++){&nbsp; &nbsp; &nbsp; &nbsp; (function (index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.querySelectorAll('#pagi li')[index].onclick=function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.querySelectorAll('#container')[0].scrollTop=index*405;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })(i)&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript