jquery懶加載、回到頂部

Q&A:

1. 如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisible實現

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>task29</title>
  </head>
  <body>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <p style="height: 100px">內容1</p>
    <div class="test">hello</div>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
      $.fn.isVisible = function() {
        var $cur = $(this),
            offsetTop = $cur.offset().top,
            $winH = $(window).height(),
            scrollTop = $(window).scrollTop(),
            scrollBottom = $(window).scrollTop() + $winH;
        if(offsetTop > scrollTop && scrollBottom > offsetTop) {
          console.log(true);
          return true;
        } else {
          console.log(false);
          return false;
        }
      }
      $('.test').isVisible();
    </script>
  </body>
</html>
isVisible

2. 當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。每次出現都在控制臺打印 true。用代碼實現

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>task29</title>
    <style type="text/css">
      html, body, div {
        margin: 0;
        padding: 0;
      }
      .test {
        margin: 30px auto;
        width: 200px;
        height: 200px;
        line-height: 200px;
        text-align: center;
        border: 3px solid;
        background-color: rgba(180, 255, 0, 0.5);
      }
    </style>
  </head>
  <body>
    <div class="test test1">test1</div>
    <div class="test test2">test2</div>
    <div class="test test3">test3</div>
    <div class="test test4">test4</div>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
      $.fn.isVisible = function() {
        var $self = $(this);
        $(window).on('scroll', function() {
          var $me = $(this);
              offsetTop = $self.offset().top,
              $winH = $me.height(),
              scrollTop = $me.scrollTop(),
              scrollBottom = $me.scrollTop() + $winH;
          if(offsetTop > scrollTop && scrollBottom > offsetTop) {
            console.log(true);
          } else {
            console.log(false);
          }
        });
      }
      $('.test4').each(function() {
        $(this).isVisible();
      });
    </script>
  </body>
</html>

效果預覽

3. 當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。在元素第一次出現時在控制臺打印 true,以后再次出現不做任何處理。用代碼實現

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>task29-demo3</title>
    <style type="text/css">
      html, body, div {
        margin: 0;
        padding: 0;
      }
      .test {
        margin: 30px auto;
        width: 200px;
        height: 200px;
        line-height: 200px;
        text-align: center;
        border: 3px solid;
        background-color: rgba(180, 255, 0, 0.5);
      }
    </style>
  </head>
  <body>
    <div class="test test1">test1</div>
    <div class="test test2">test2</div>
    <div class="test test3">test3</div>
    <div class="test test4">test4</div>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
      $.fn.isVisible = function() {
        var $self = $(this);
        $(window).on('scroll', function() {
          var $me = $(this);
              offsetTop = $self.offset().top,
              $winH = $me.height(),
              scrollTop = $me.scrollTop(),
              scrollBottom = $me.scrollTop() + $winH;
          if(!$self.data('data-visible')) {
            if(offsetTop > scrollTop && scrollBottom > offsetTop) {
                console.log(true);
                $self.data('data-visible', true);
            } else {
              return;
            }
          }
        });
      }
      $('.test4').each(function() {
        $(this).isVisible();
      });
    </script>
  </body>
</html>

效果預覽

4. 圖片懶加載的原理是什么?

圖片懶加載也稱為曝光加載,將頁面上的圖片分批加載,只有當圖片出現在window窗口中(用戶可見)的時候,才加載圖片;而正常情況下,img元素是自動加載的,所以可以自定義一個圖片地址屬性,當需要加載圖片的時候,將該自定義屬性值賦給src屬性,以實現優化頁面的渲染速度,圖片延遲加載。


coding:


本文歸饑人谷和本人所有,如需轉載請注明來源,謝謝

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 問答 1.如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisi...
    鴻鵠飛天閱讀 236評論 0 1
  • 如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisible實現...
    coolheadedY閱讀 415評論 0 0
  • 一、如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisible...
    咩咩咩1024閱讀 286評論 0 0
  • 如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisible實現...
    Nicklzy閱讀 578評論 0 50
  • 問答 1. 如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVis...
    小木子2016閱讀 210評論 0 0