【问题标题】:Determine if element has scrolled into view确定元素是否已滚动到视图中
【发布时间】:2021-04-15 21:07:36
【问题描述】:

我正在尝试确定当用户滚动时某个元素是否在视口中可见。此代码在 Modal 之外工作,但不在 Modal 内部。我想这与 z-index,但无论我尝试什么,它都不起作用。有什么想法吗?

app.component.ts

app.component.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <h1>Is div shown? {{isTestDivScrolledIntoView ? 'Yes!' : 'No :-('}}</h1>


          <div #testDiv class="test">Test</div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

</body>
</html>

app.component.css

    p {
    font-family: Lato;
  }
  
  h1 {
    position: fixed;
    top: 0;
    padding-bottom: 5%;
  }
  
  .test {
    text-align: center;
    height: 40px;
    margin-bottom: 40vh;
    margin-top: 40vh;
    border: 1px solid #000000;
  }

【问题讨论】:

    标签: javascript angular twitter-bootstrap web bootstrap-modal


    【解决方案1】:

    检查一个名为 Intersection 观察者的功能。它允许您在元素进入视图时监听它,然后触发回调。

    https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

    【讨论】:

    • 如果我的 Modal 出现 z-index 问题,会有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2013-05-13
    • 2018-04-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    相关资源
    最近更新 更多