HTML:将链接导向锚定在页面中间 [英] HTML: Making a link lead to the anchor centered in the middle of the page

查看:716
本文介绍了HTML:将链接导向锚定在页面中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到我的html页面上的一个锚点。当链接被点击时,它会导致页面滚动到锚点,以便锚点位于页面可见部分的顶部。我怎样才能让页面滚动,以便锚点位于页面中间?

I have a link to an anchor on my html page. When the link is clicked it causes the page to scroll to the anchor so that the anchor is at the very top of the visible part of the page. How can I make the page scroll so that the anchor will be in the middle of the page?

推荐答案

没有直接的方法在纯html \css中这样做。使用js是可能的,通过获取元素的顶部位置并将页面的顶部位置设置为该元素的位置减去页面高度的一半。

There is no straight way of doing this in pure html\css. It is possible though using js, by getting the element's top location and setting the page's top position to that element's position minus half of the page's height.

这篇关于HTML:将链接导向锚定在页面中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆