javascript crossbrowser确定用户是否滚动到页面底部 [英] javascript crossbrowser determine if user scrolld to the bottom of page
问题描述
当用户到达页面底部时,您怎么能进入 Javascript (没有Jquery)警报?
i尝试基于另一个例子,这样的事情但没有成功。
Hello how could i get in Javascript (no Jquery) an alert when the user got to the bottom of the page? i tried based on another example here something like this but no success.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="bla" style="width:145px;">
long text here
</div>
<script type="text/javascript">
var obj = document.getElementById("bla");
debugger;
if( obj.scrollTop == (obj.scrollHeight - obj.offsetHeight))
{
alert("down");
};
</script>
</body>
</html>
推荐答案
如果您使用< ;!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns = http://www.w3.org/1999/xhtml\">
doctype然后它可以工作。
If you are using <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
doctype then it sould work.
window.onscroll = function () {
if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1 || navigator.userAgent.toLowerCase().indexOf("safari") > -1) {
if (document.documentElement.scrollHeight == (document.body.scrollTop + document.documentElement.clientHeight)) {
alert("ok")
}
} else {
if (document.documentElement.scrollHeight == (document.documentElement.scrollTop + document.documentElement.clientHeight)) {
alert("ok");
}
}
}
如果你正常使用< html>
标记位于文档顶部,然后它应该可以正常工作。
If you are using normal <html>
tag at the top of your document then it should work.
window.onscroll = function () {
if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
if (document.documentElement.scrollHeight == (document.documentElement.scrollTop + document.documentElement.clientHeight)) {
alert("ok");
}
} else {
if (document.body.scrollHeight == (document.body.scrollTop + document.body.clientHeight)) {
alert("ok");
}
}
}
这篇关于javascript crossbrowser确定用户是否滚动到页面底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!