html 页面内锚点定位及跳转方法总结( 二 )


会尽可能全部出现在视口中(可能的话 , 调用元素的底部会与视口的顶部齐平 。)不过顶部
不一定齐平 , 例如:
//让元素可见
document.forms[0].scrollIntoView();
当页面发生变化时 , 一般会用这个方法来吸引用户注意力 。实际上 , 为某个元素设置焦点也
会导致浏览器滚动显示获得焦点的元素 。
支持该方法的浏览器有 IE、Firefox、Safari和Opera 。
*/
document.querySelector("#roll1").onclick = function(){
document.querySelector("#roll_top").scrollIntoView(false);
}
document.querySelector("#roll2").onclick = function(){
document.querySelector("#roll_top").scrollIntoView(true);
【html 页面内锚点定位及跳转方法总结】}
}
</script>
<style type="text/css">
#myDiv{
height:900px;
background-color:gray;
}
#roll_top{
height:900px;
background-color:green;
color:#FFF;
font-size:50px;
position:relative;
}
#bottom{
position:absolute;
display:block;
left;0;bottom:0;
}
</style>
</head>
<body>
<button id="roll1">scrollIntoView(false)</button>
<button id="roll2">scrollIntoView(true)</button>
<div id="myDiv"></div>
<div id="roll_top">
scrollIntoView(ture)元素上边框与视窗顶部齐平
<span id="bottom">scrollIntoView(false)元素下边框与视窗底部齐平</span>
</div>
</body>
</html>
个人建议使用第四种方法 。




推荐阅读