具体代码如下所示:
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <meta name="keywords" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>平滑滚动</title> <!--[if (gte IE 9)|!(IE)]><!--> <script src="assets/js/jquery.min.js"></script> <!--<![endif]--> <!--[if lte IE 8 ]> <script src="assets/ie8/jquery.min.js"></script> <script src="assets/ie8/modernizr.js"></script> <script src="assets/js/amazeui.ie8polyfill.min.js"></script> <![endif]--> <script src="assets/js/amazeui.min.js"></script> <link rel="stylesheet" href="assets/css/amazeui.min.css"> </head> <body style="margin:300px;"> <!-- $('html, body').animate({scrollTop: 0}, '500'); //兼容旧版IE的写法 --> <div style="height: 2000px;"></div> <!-- 滚动到顶部 --> <button data-am-smooth-scroll class="am-btn am-btn-success">滚动到顶部</button> <!-- 滚动到底部 --> <button id="doc-scroll-to-btm" class="am-btn am-btn-primary">滚动到底部</button> <script> $('#doc-scroll-to-btm').on('click', function() { var $w = $(window); $w.smoothScroll({position: $(document).height() - $w.height()}); }); </script> <!-- 定义选项 --> <button data-am-smooth-scroll="{position: 57, speed: 5000}" class="am-btn am-btn-danger">慢悠悠地滚到距离顶部 57px 的位置</button> <!--通过 Data API--> <!-- 在元素上添加 data-am-smooth-scroll 属性。 --> <button data-am-smooth-scroll class="am-btn am-btn-success">滚动到顶部</button> <!-- 如果要指定滚动的位置,可以给这个属性设一个值。 --> <button data-am-smooth-scroll="{position: 189}" class="am-btn am-btn-secondary">指定滚动的位置</button> <!-- 通过 Javascript $(window).smoothScroll([options]); // 滚动到底部 $('#my-button').on('click', function() { var $w = $(window); $w.smoothScroll({position: $(document).height() - $w.height()}); }); --> <div style="height: 2000px;"></div> </body> </html>
效果图: