描点定位方式
1.同一个页面内描点定位,css实现,href=’#to’表示跳转到name值为to的a标签区域。
<a href="#to">跳转</a>
<a name="to">
要跳转到的区域
</a>
2.同样是css实现,跳转到id为to的区域
a href="#to">跳转</a>
<div id="to">
要跳转到的区域
</div>
3.js实现,使用scrollIntoView函数,定位到id为to的元素实例。
document.getElementById('to').scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest"
})
其中behavior表示为过渡方式,值有auto和smooth两种,auto表示立即定位到目标元素,smooth表示平滑移动到目标元素,默认为auto
block表示目标元素出现在屏幕的位置,值有end,center,start三种。
end表示滚动条向下滚动,目标元素出现就停止滚动,
center表示滚动条向下滚动,目标元素出现在屏幕中间停止滚动,
start表示滚动条向下滚动,目标元素出现在屏幕顶部,停止滚动。
以上滚动均需要屏幕高度足够,否则不能滚动。
scrollIntoView()也可以直接传一个布尔值,false表示block:“end”,不传或者传true表示block:“start”。
i