我有一个header.php
,它有我的<a name='#top' />
还有一个footer.php
我想把它写成
if ($_SERVER[' is scroll bar ']
{
echo <a href='#top'>Back to Top</a>
}
是否有可能用PHP或Javascript做这样的事情,在那里我可以知道我的页面是否会在渲染时间之前很长?
首先,我不确定这种代码的实际用途。我从来都不确定"back to top"或"jump to top"链接能做什么:
- 它会把我送到页面的顶部吗?
- 它会把我送到顶部以下的某个锚点吗?
- 会把我带到顶部页面吗?
- 会重新加载吗?
我确实知道按下Home
键会做什么,但是,我怀疑大多数人宁愿使用本地控件来导航页面。
尽管如此,一旦页面被加载,您可以通过在页面调整:
上设置事件处理程序来动态切换这种链接。var toTopLink = document.getElementById("toTopLink");
function checkLink(e) {
/*
* document.height = works in most browsers
* document.body.scrollHeight = needed in IE
*
* window.innerHeight = works in most browsers
* document.documentElement.offsetHeight = IE in compatibility mode
* document.body.offsetHeight = IE backwards compatibility
*
* In most browsers, document.height equals window.innerHeight if there is no
* scrollbar. For IE, we test whether the scrollable height is greater than the
* visible document height.
*/
if ((document.height || document.body.scrollHeight) >
(window.innerHeight || document.documentElement.offsetHeight ||
document.body.offsetHeight)) {
toTopLink.style.display = "block";
} else {
toTopLink.style.display = "none";
}
}
window.onresize = checkLink;
window.onload = checkLink;
不同的浏览器——我指的是IE和其他浏览器——都有自己的查找高度的方法,所以上面的解释可能有些多余。
这在PHP中是不可能的,甚至不要尝试。使用JavaScript实现,如:
if(document.body.scrollHeight > window.innerHeight){
// show scroll-to-top link
}
No。如果浏览器不首先呈现它,就无法在任何语言中进行识别。
是的,但我建议使用JavaScript,而不是PHP。此外,要获得页面的实际大小,必须首先在浏览器中呈现内容。
<a href='#top' id="back" style="display:none;">Back to Top</a>
把这个JavaScript片段放在页面底部,或者放在DOMready回调函数中:
<script type="text/javascript">
var backAnchor = document.getElementById('back');
if (document.body.scrollHeight > window.innerHeight) {
backAnchor.styles.display = 'none';
}
</script>