2011-04-03 12 views
5

मैं Google क्रोम 10 का उपयोग कर रहा हूं और स्क्रॉल एंड का पता लगाने के लिए जावास्क्रिप्ट लिख रहा हूं।मैं जावास्क्रिप्ट द्वारा निर्दिष्ट तत्व के स्क्रॉल एंड का पता कैसे लगा सकता हूं?

window की पुस्तक अंत का पता लगाने के लिए, नीचे दिए गए कोड ठीक काम किया:

window.addEventListener(
    'scroll', 
    function() 
    { 
     var scrollTop = document.documentElement.scrollTop || 
      document.body.scrollTop; 
     var offerHeight = document.body.offsetHeight; 
     var clientHeight = document.documentElement.clientHeight; 
     if (offsetHeight <= scrollTop + clientHeight) 
     { 
      // Scroll end detected 
     } 
    }, 
    false 
); 

अब मैं <section id="box" style="height: 500px; overflow: auto;">
इस तरह, निर्दिष्ट तत्व की पुस्तक अंत का पता लगाने के लिए चाहते हैं कोड है कि सही ढंग से पता नहीं लगा पाया है :

document.getElementById('box').addEventListener(
    'scroll', 
    function() 
    { 
     var scrollTop = document.getElementById('box').scrollTop; 
     var offerHeight = document.getElementById('box').offsetHeight; 
     var clientHeight = document.getElementById('box').clientHeight; 
     if (offsetHeight <= scrollTop + clientHeight) 
     { 
      // This is called before scroll end! 
     } 
    }, 
    false 
); 

क्या कोई मेरा कोड ठीक कर सकता है? धन्यवाद।

उत्तर

4

फिक्स्ड।

document.getElementById('box').addEventListener(
    'scroll', 
    function() 
    { 
     var scrollTop = document.getElementById('box').scrollTop; 
     var scrollHeight = document.getElementById('box').scrollHeight; // added 
     var offsetHeight = document.getElementById('box').offsetHeight; 
     // var clientHeight = document.getElementById('box').clientHeight; 
     var contentHeight = scrollHeight - offsetHeight; // added 
     if (contentHeight <= scrollTop) // modified 
     { 
      // Now this is called when scroll end! 
     } 
    }, 
    false 
) 
संबंधित मुद्दे