回答:
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
};
document.documentElement.scrollTop
代わりに使用しwindow.scrollY
ます。IEのどのバージョンがサポートされてwindow.scrollY
いるかは不明です。
window.onscroll = function(ev) {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
現在受け入れられている答えの問題は、 window.scrollY
IEでは利用できないことです。
ここからの引用であるMDN scrollYについては:
ブラウザ間の互換性のために、window.scrollYの代わりにwindow.pageYOffsetを使用してください。
そして実用的なスニペット:
@Raphaëlのコメントに基づくと、オフセットが小さいためMacに問題がありました。
次の更新された条件が機能します。
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
私はそれをさらにテストする機会がありませんでした、誰かがこの特定の問題についてコメントできればそれは素晴らしいでしょう。
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
(window.innerHeight + Math.ceil(window.pageYOffset + 1)) >= document.body.offsetHeight
です。
受け入れられた答えは私にとってはうまくいきませんでした。これはしました:
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
// you're at the bottom of the page
console.log("Bottom of page");
}
};
古いブラウザ(IE9)のサポートを検討している場合は、サポートwindow.pageYOffset
が少し優れているエイリアスを使用してください。
答えを探していましたが、正確な答えが見つかりませんでした。この回答の時点で最新のFirefox、IE、Chromeで動作する純粋なJavaScriptソリューションを次に示します。
// document.body.scrollTop alone should do the job but that actually works only in case of Chrome.
// With IE and Firefox it also works sometimes (seemingly with very simple pages where you have
// only a <pre> or something like that) but I don't know when. This hack seems to work always.
var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
// Grodriguez's fix for scrollHeight:
// accounting for cases where html/body are set to height:100%
var scrollHeight = (document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight;
// >= is needed because if the horizontal scrollbar is visible then window.innerHeight includes
// it and in that case the left side of the equation is somewhat greater.
var scrolledToBottom = (scrollTop + window.innerHeight) >= scrollHeight;
// As a bonus: how to scroll to the bottom programmatically by keeping the horizontal scrollpos:
// Since window.innerHeight includes the height of the horizontal scrollbar when it is visible
// the correct vertical scrollTop would be
// scrollHeight-window.innerHeight+sizeof(horizontal_scrollbar)
// Since we don't know the visibility/size of the horizontal scrollbar
// we scroll to scrollHeight that exceeds the value of the
// desired scrollTop but it seems to scroll to the bottom with all browsers
// without problems even when the horizontal scrollbar is visible.
var scrollLeft = (document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft;
window.scrollTo(scrollLeft, scrollHeight);
((document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight)
だけでなく、使用document.body.scrollHeight
する必要がありました
これは機能します
window.onscroll = function() {
// @var int totalPageHeight
var totalPageHeight = document.body.scrollHeight;
// @var int scrollPoint
var scrollPoint = window.scrollY + window.innerHeight;
// check if we hit the bottom of the page
if(scrollPoint >= totalPageHeight)
{
console.log("at the bottom");
}
}
古いブラウザー(IE9)をサポートする場合は、window.scrollYをwindow.pageYOffsetに置き換えます。
私はこれを見始めたばかりであり、ここでの答えは私を助けてくれたので、ありがとう。コードがIE7まで完全に安全になるように、少し拡張しました。
これが誰かにとって役立つことを願っています。
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 100px;
border-bottom: 1px solid #ddd;
}
div:nth-child(even) {
background: #CCC
}
div:nth-child(odd) {
background: #FFF
}
</style>
</head>
<body>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</body>
<script type="text/javascript">
console.log("Doc Height = " + document.body.offsetHeight);
console.log("win Height = " + document.documentElement.clientHeight);
window.onscroll = function (ev) {
var docHeight = document.body.offsetHeight;
docHeight = docHeight == undefined ? window.document.documentElement.scrollHeight : docHeight;
var winheight = window.innerHeight;
winheight = winheight == undefined ? document.documentElement.clientHeight : winheight;
var scrollpoint = window.scrollY;
scrollpoint = scrollpoint == undefined ? window.document.documentElement.scrollTop : scrollpoint;
if ((scrollpoint + winheight) >= docHeight) {
alert("you're at the bottom");
}
};
</script>
</html>
height: 100%
コンテナを設定している<div id="wrapper">
場合は、次のコードが機能します(Chromeでテスト済み)。
var wrapper = document.getElementById('wrapper');
wrapper.onscroll = function (evt) {
if (wrapper.scrollTop + window.innerHeight >= wrapper.scrollHeight) {
console.log('reached bottom!');
}
}
window.onscroll = function(ev) {
if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
この回答は、エッジケースを修正します、これがされているためpageYOffset
であるdouble
間、innerHeight
そして offsetHeight
ありますlong
、ブラウザはあなたに情報を与えるときに、あなたはピクセル短くてもよいです。例:ページの下部にある
本当 window.innerHeight = 10.2
本当 window.pageYOffset = 5.4
本当 document.body.offsetHeight = 15.6
その場合、計算は次のようになります:10 + 5.4> = 16これは 偽です
これを修正するMath.ceil
には、pageYOffset
値でます。
お役に立てば幸いです。
あなたがjqueryを愛しているなら
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
// doSomethingHere();
}
});
jqueryの無限スクロールを調べることができます。
http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/
jqueryライブラリーを使用するつもりで、厳密な純粋なJSメソッドを期待していない場合、それはあなたが望んでいることを行うように聞こえます。
驚いたことに、私にとって有効な解決策はありませんでした。それは私css
がめちゃくちゃになっていて、body
使用時にすべてのコンテンツをラップしなかったためだと思いますheight: 100%
(まだ理由がわからない)。しかし、解決策を探している間、私はうまく何かを思いついた...基本的に同じですが、多分それを見る価値があります-プログラミングに慣れていないので、同じことをもっと遅い、サポートされていない、または何かのような場合は申し訳ありませんそれ...
window.onscroll = function(evt) {
var check = (Element.getBoundingClientRect().bottom - window.innerHeight <= 0) ? true : false;
if (check) { console.log("You're at the bottom!"); }
};
$(document).ready(function(){
$('.NameOfYourDiv').on('scroll',chk_scroll);
});
function chk_scroll(e)
{
var elem = $(e.currentTarget);
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight())
{
alert("scrolled to the bottom");
}
}
機能コードスニペットの埋め込みを使用defaultView
しdocumentElement
て:
const { defaultView } = document;
const { documentElement } = document;
const handler = evt => requestAnimationFrame(() => {
const hitBottom = (() => (defaultView.innerHeight + defaultView.pageYOffset) >= documentElement.offsetHeight)();
hitBottom
? console.log('yep')
: console.log('nope')
});
document.addEventListener('scroll', handler);
<pre style="height:110vh;background-color:fuchsia">scroll down</pre>
正しいXPathがわからないコンポーネントを探すために、体系的に下にスクロールする方法(Javaで)を考え出す必要がありました(長い話なので、遊んでください)。先ほど述べたように、コンポーネントを探しながら下にスクロールし、コンポーネントが見つかったか、ページの下部に達したときに停止する必要がありました。
次のコードスニペットは、ページの下へのスクロールを制御します。
JavascriptExecutor js = (JavascriptExecutor) driver;
boolean found = false;
long currOffset = 0;
long oldOffset = 0;
do
{
oldOffset = currOffset;
// LOOP to seek the component using several xpath regexes removed
js.executeScript("window.scrollBy(0, 100)");
currOffset = (Long)js.executeScript("var offset = window.window.pageYOffset; return offset;");
} while (!found && (currOffset != oldOffset));
ちなみに、このコードスニペットが実行される前にウィンドウが最大化されます。
受け入れられた答えは私にとってはうまくいきませんでした。これはしました:
const element = document.createElement('div');
document.body.appendChild(element);
document.addEventListener('scroll', () => {
const viewportHeight = window.innerHeight;
const distance = element.getBoundingClientRect().top;
if (Math.floor(distance) <= viewportHeight) {
console.log('yep')
} else {
console.log('nope')
}
})
私が見つけた2つの解決策は私のために働きました:
window.addEventListener('scroll', function(e) {
if (
window.innerHeight + document.documentElement.scrollTop ===
document.documentElement.offsetHeight
) {
console.log('You are at the bottom')
}
})
そして他の:
window.addEventListener('scroll', function(e) {
if (
window.innerHeight + window.pageYOffset ===
document.documentElement.offsetHeight
) {
console.log('You are at the bottom')
}
})