类似与锚点的用法,可以让元素跳到需要的地方。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-1.9.1.min.js"></script> <style> .in-top{ width: 120px; height: 30px; line-height: 30px; } .middle{ width: 100%; height: 900px; background-color: #95b1ff; } #top{ width: 100%; height: 300px; background-color: antiquewhite; } </style> </head> <body> <button class="in-top">click me</button> <div class="middle">撑地方</div> <div id="top"></div> <script> $(".in-top").click(function () { document.getElementById("top").scrollIntoView(); }) </script> </body> </html>
这是写的一个demo,一开始用的jquery方法取top元素,总是报错,百度后才知道,只能用getelementbyid的方法。