【问题标题】:How can I detect if the user scrolls to end of an <embed> element?如何检测用户是否滚动到 <embed> 元素的末尾?
【发布时间】:2019-01-30 15:50:17
【问题描述】:

如何检测用户是否滚动到&lt;embed&gt; 元素的末尾?

<embed src="contract.pdf" type="application/pdf" width="800" height="800" id="contractPDF">  

我使用了这段代码,但它不起作用:

$('#contractPDF').bind('scroll', function() {
  if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {   
    alert("end reched");
  }
});

【问题讨论】:

    标签: javascript jquery html pdf scroll


    【解决方案1】:

    首先,您需要知道元素从哪里开始和结束,这样当您向下滚动时,您就知道您是否已经过了那个点。

    $(window).scroll(function() {
    
        // Fetch the embed container.
        var container = $('#contractPDF');
    
        // Height of our window.
        var windowHeight = $(window).height();
      
      // Container information.
      var containerHeight = $(container).height();
      var containerBottom = container.offset().top + containerHeight;
    
        // Location of our window in our page.
        var windowLocation = $(this).scrollTop();
    
       // Check if we are past it.
       if (windowLocation + windowHeight > containerBottom) { 
        alert('reached');
       }
    
    });
    #contractPDF {
      height: 900px;
      width: 200px;
      background-color: blue;
      position:relative;
      display:block;
    }
    
    #contractPDF2 {
      height: 900px;
      width: 200px;
      background-color: red;
      position:relative;
      display:block;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="contractPDF"></div>
    <div id="contractPDF2"></div>

    【讨论】:

    • 感谢您的帮助,代码甚至没有检测到窗口的滚动
    • 很奇怪我一直都是这样。你把它放在jQuery(document).ready(function() {}) 子句中对吗?
    • 我更新了我的代码并将其添加到一个 sn-p 中,以便您可以尝试。它现在正在运行,因此它也应该适用于您的项目。
    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多