Javascript: จะตรวจสอบได้อย่างไรว่าหน้าต่างเบราว์เซอร์เลื่อนไปที่ด้านล่าง?


187

ฉันต้องการตรวจสอบว่าผู้ใช้เลื่อนไปที่ด้านล่างของหน้า หากพวกเขาอยู่ที่ด้านล่างของหน้าเมื่อฉันเพิ่มเนื้อหาใหม่ที่ด้านล่างฉันจะเลื่อนพวกเขาไปที่ด้านล่างใหม่โดยอัตโนมัติ หากพวกเขาไม่ได้อยู่ที่ด้านล่างพวกเขากำลังอ่านเนื้อหาก่อนหน้าสูงขึ้นในหน้าดังนั้นฉันไม่ต้องการเลื่อนพวกเขาโดยอัตโนมัติเพราะพวกเขาต้องการที่จะอยู่ที่พวกเขาอยู่

ฉันจะตรวจสอบได้อย่างไรว่าผู้ใช้เลื่อนไปที่ด้านล่างของหน้าหรือหากพวกเขาเลื่อนขึ้นที่สูงขึ้นบนหน้า?


1
อันนี้ทำงานใน Angular 6 -> stackoverflow.com/a/42547136/621951
GünayGültekin

คำตอบ:


268
window.onscroll = function(ev) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // you're at the bottom of the page
    }
};

ดูตัวอย่าง


29
ไม่ทำงานเมื่อมีการตั้งค่าองค์ประกอบ html / body เป็น 100% (เพื่อให้ร่างกายเติมความสูงวิวพอร์ตทั้งหมด)
Grodriguez

5
ใช้document.documentElement.scrollTopแทนwindow.scrollYIE ไม่แน่ใจว่าถ้ามีรุ่นของ IE window.scrollYสนับสนุน
Batandwa

3
ไม่ทำงานใน Chromium เวอร์ชัน 47.0.2526.73 สร้างขึ้นบน Ubuntu 14.04 ทำงานบนระบบปฏิบัติการพื้นฐาน 0.3.2 (64 บิต)
K - ความเป็นพิษใน SO กำลังเพิ่มขึ้น

9
ฉันใช้ document.body.scrollHeight แทน offsetHeight (ในกรณีของฉัน offsetHeight นั้นเล็กกว่า window.innerHeight เสมอ)
Oliver

1
@KarlMorrison คุณสามารถลองคำตอบที่ได้รับรางวัลโปรดปราน (โดย @Dekel) กับเบราว์เซอร์ของคุณ?
Basj

115

รหัสที่อัปเดตสำหรับการสนับสนุนเบราว์เซอร์ที่สำคัญทั้งหมด (รวมถึง IE10 และ IE11)

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.pageYOffset แทน window.scrollY

และตัวอย่างข้อมูลที่ใช้งานได้:

หมายเหตุสำหรับ mac

จากความคิดเห็นของ @ Raphaëlมีปัญหาใน mac เนื่องจากออฟเซตขนาดเล็ก
เงื่อนไขที่อัพเดตต่อไปนี้ใช้งานได้:

(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2

ฉันไม่ได้มีโอกาสทดสอบเพิ่มเติมหากใครบางคนสามารถแสดงความคิดเห็นเกี่ยวกับปัญหาเฉพาะนี้ได้จะดีมาก


ยืนยันการทำงานกับ FF, Chrome, IE10, Chrome สำหรับ Android ขอบคุณ @Dekel!
Basj

2
แปลกเพราะมันอาจฟังดูเฉพาะในเบราว์เซอร์ของฉันฉันสั้น 1 px และด้วยเหตุนี้สภาพไม่ได้ถูกทริกเกอร์ ไม่แน่ใจว่าทำไมต้องเพิ่ม px พิเศษอีกสองสามตัวเพื่อให้ทำงานได้
Sharjeel Ahmed

3
บนคอมพิวเตอร์ Mac เงื่อนไขด้านล่างนี้ไม่ตรงตามเนื่องจากออฟเซ็ตขนาดเล็ก (~ 1px) ที่เราอัปเดตสภาพเช่นนั้น(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
Raphaël

4
ขอบคุณ @Dekel! ที่จริงแล้วเราพบว่าหน้าต่าง pageYOffset เป็นแบบลอยบน mac (window.innerHeight + Math.ceil(window.pageYOffset + 1)) >= document.body.offsetHeightทางออกสุดท้ายของเราคือ
Raphaël

2
ไม่สามารถใช้งานได้ในกรณีที่ร่างกายมีสไตล์ที่กำหนดความสูงเป็น 100%
raRaRa

56

คำตอบที่ยอมรับไม่ได้ผลสำหรับฉัน สิ่งนี้ทำ:

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ที่รองรับได้ดีกว่า


1
ไม่ทำงานใน IE10 / 11 ตรวจสอบคำตอบของ Dekel ( stackoverflow.com/questions/9439725/ … ) สำหรับการสนับสนุน IE ทำงานให้ฉัน
Herr_Schwabullek

คำตอบอื่น ๆ เรียก console.log () ทุกครั้งที่ฉันเลื่อนไม่เพียง แต่เมื่อฉันอยู่ที่ด้านล่างของหน้า คำตอบนี้ใช้ได้สำหรับฉันบน Chrome
Defcronyke

หากคุณกำจัด window.scrollY ซึ่งไม่ทำงานเช่นหรือ edge นี่เป็นคำตอบที่ดี แทนที่ด้วย: (window.innerHeight + window.pageYOffset)> = document.body.scrollHeight
colemerrick

21

ฉันค้นหาคำตอบ แต่ไม่พบคำตอบที่แน่นอน นี่คือโซลูชันจาวาสคริปต์ที่ทำงานร่วมกับ Firefox, IE และ Chrome ล่าสุดในเวลาที่ตอบคำถามนี้:

// 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);

4
สิ่งนี้เกือบจะได้ผลสำหรับฉัน แต่ฉันต้องใช้((document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight)แทนเพียงdocument.body.scrollHeightเพื่อพิจารณากรณีที่ html / body ตั้งค่าความสูง: 100%
Grodriguez

1
@Grodriguez ขอบคุณสำหรับข้อมูล! มันอาจเป็นประโยชน์สำหรับเราในอนาคต! :-)
pasztorpisti

15

วิธีนี้ใช้ได้ผล

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


1
งานนี้มีการตอบสนองในปี 2019 ทำงานกับร่างกายสูง 100% คุ้มกับ html สูง 100% ทำงานกับโครเมี่ยม, ซาฟารี, Firefox, ขอบ
กีวีโกรธ

เพียงทราบ: การตั้งชื่อควรมีการเปลี่ยนแปลง - ควรเปลี่ยนตัวแปร เนื่องจาก scrollHeight แสดงความสูงของหน้าทั้งหมดและ totalHeight แสดงจุดเลื่อนปัจจุบันดังนั้นมันจึงค่อนข้างสับสน
Vladimir Marton

4

ฉันเพิ่งเริ่มดูสิ่งนี้และคำตอบที่นี่ช่วยฉันได้ขอบคุณมาก ฉันได้ขยายเพิ่มเล็กน้อยเพื่อให้รหัสปลอดภัยตลอดทางจนถึง 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>

เรียงงาน แต่มันไม่แม่นยำมาก มันพิจารณาแล้วว่ามันถูกเลื่อนไปที่ด้านล่างตราบใดที่คุณอยู่ภายในประมาณ 16px ของด้านล่าง
Peter Hall

4

หากคุณกำลังตั้งค่า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!');
  }
}

3
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ค่า

หวังว่าจะช่วย



2

คุณสามารถดูการเลื่อนแบบไม่มีที่สิ้นสุดของ jquery:

http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/

ซึ่งดูเหมือนว่ามันจะเป็นสิ่งที่คุณขอสมมติว่าคุณยินดีที่จะใช้ห้องสมุด jquery และไม่หวังว่าจะใช้วิธีการ JS อย่างแท้จริง


2
แม้ว่าลิงก์นี้อาจตอบคำถามได้ดีกว่าหากรวมส่วนสำคัญของคำตอบไว้ที่นี่และให้ลิงก์สำหรับการอ้างอิง คำตอบสำหรับลิงก์เท่านั้นอาจไม่ถูกต้องหากหน้าเว็บที่เชื่อมโยงมีการเปลี่ยนแปลง
bobthedeveloper

@Hatsjoem เขากำลังลิงก์ไปยังปลั๊กอิน "ชิ้นส่วนสำคัญ" ที่ควรคัดลอกมาคืออะไร? ใครสนใจคำตอบว่า "จะไม่ถูกต้องหากหน้าเว็บที่เชื่อมโยงมีการเปลี่ยนแปลง" หากลิงก์ใช้งานไม่ได้หมายความว่าปลั๊กอินนั้นถูกยกเลิกและไม่มีอยู่อีกต่อไป - แม้ว่าคุณจะเพิ่มข้อมูลเพิ่มเติมลงในคำตอบก็จะยังคงไม่ถูกต้อง ใช่คำตอบที่ลิงก์เท่านั้นไม่จำเป็นต้องเลวร้าย
dcastro

@Hatsjoem จากmeta : "เมื่อห้องสมุดอ้างอิงอยู่ในตำแหน่งที่รู้จักและมีเสถียรภาพคำตอบแบบนี้เป็นคำตอบที่ไม่ดีแต่โดยทั่วไปแล้วคำตอบยัง : มันบอกว่า" ใช้สิ่งนี้ "
dcastro

นอกจากนี้ Google อาจยังคงมีอยู่หากการเลื่อนแบบไม่สิ้นสุดเปลี่ยน URL นี้ จุดคือการใช้ปลั๊กอินนั้นดังนั้นหากการเชื่อมโยงตายเพียง google เลื่อนการค้นหา jquery ไม่มีที่สิ้นสุด คุณอาจจะพบสิ่งที่คุณต้องการ
ไก่ชิง

2

น่าแปลกที่ไม่มีวิธีแก้ปัญหาใดที่เหมาะกับฉัน ฉันคิดว่าเป็นเพราะฉัน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!"); }
};

2
$(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");
    }

}

idk มุมมองที่แตกต่าง แต่คำตอบที่ได้รับการยอมรับนั้นดีกว่าแน่นอน
Zihan Zhang

2
const handleScroll = () => {
    if (Math.round(window.scrollY + window.innerHeight) >= Math.round(document.body.scrollHeight)) {
        onScroll();
    }
};

รหัสนี้ใช้ได้สำหรับฉันใน Firefox และ IE เช่นกัน


1

การใช้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>


1

คุณสามารถตรวจสอบว่าผลรวมของความสูงของหน้าต่างและด้านบนของสกรอลนั้นใหญ่กว่าของร่างกายหรือไม่

if (window.innerHeight + window.scrollY >= document.body.scrollHeight) {}


0

ฉันต้องหาวิธี (ใน Java) เพื่อเลื่อนดูระบบอย่างเป็นระบบเพื่อหาส่วนประกอบที่ฉันไม่รู้จัก XPath ที่ถูกต้อง (เรื่องยาวดังนั้นแค่เล่นตาม) ตามที่ฉันเพิ่งแจ้งฉันต้องเลื่อนลงในขณะที่ค้นหาส่วนประกอบและหยุดอย่างใดอย่างหนึ่งเมื่อพบส่วนประกอบหรือถึงด้านล่างของหน้า

ข้อมูลโค้ดต่อไปนี้ควบคุมการเลื่อนลงไปที่ด้านล่างของหน้า:

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));

หน้าต่างจะขยายใหญ่สุดก่อนที่จะมีการเรียกใช้โค้ดขนาดสั้นนี้


0

คำตอบที่ยอมรับไม่ได้ผลสำหรับฉัน สิ่งนี้ทำ:

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')
    }
})

0

โซลูชันสองข้อที่ฉันพบว่าใช้งานได้สำหรับฉัน:

  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')
    }
  })
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.