jquery懶加載、回到頂部

Q&A:

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

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>task29</title>
  </head>
  <body>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容1</p>
    <p style="height: 100px">內(nèi)容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. 當(dāng)窗口滾動時,判斷一個元素是不是出現(xiàn)在窗口可視范圍。每次出現(xiàn)都在控制臺打印 true。用代碼實(shí)現(xiàn)

<!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>

效果預(yù)覽

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

<!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>

效果預(yù)覽

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

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


coding:


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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容