Can I detect the user viewable area on the browser?

前端 未结 4 1502
情话喂你
情话喂你 2020-12-07 18:53

As you can see the image below, there is \"A\", \"B\", \"C\", \"D\" and \"E\" on the website, and the user may only can see the A, B, and a little parts of D in their browse

4条回答
  •  北海茫月
    2020-12-07 19:19

    Basically, you'd first have to measure the viewport dimentions, by using the window object, then you'd need to loop through each of the elements that you want to check, and calculate wether they fit.

    See this jsfiddle for an example.

    Here's the code (for posterity's sake):

    HTML:

    A big list!

      CSS:

      #info{
          position: fixed;
          right: 0px;
          text-align: center;
          background: white;
          border: 2px solid black;
          padding: 10px;
      }
      

      JS:

          $(function(){
      
          $(window).bind('scroll.measure resize.measure',function(){
      
              // Gather together the window width, height, and scroll position.
              var winWidth = $(window).width(),
                  winHeight = $(window).height(),
                  winLeft = $(window).scrollLeft(),
                  winTop = $(window).scrollTop(),
                  winBottom = winTop + winHeight,
                  winRight = winLeft + winWidth,
                  inView = [];
      
              // Loop over each of the elements you want to check
              $('.inview').each(function(){
      
                  // Get the elements position and dimentions.
                  var pos = $(this).position(),
                      width = $(this).outerWidth(),
                      height = $(this).outerHeight();
      
                  // Set bottom and right dimentions.
                  pos.bottom = pos.top + height;
                  pos.right = pos.left + width;
      
                  // Check whether this element is partially within
                  // the window's visible area.
                  if((
                      pos.left >= winLeft &&
                      pos.top >= winTop &&
                      pos.right <= winRight &&
                      pos.bottom <= winBottom
                  ) || (
                      pos.left >= winLeft && pos.top >= winTop && 
                      pos.left <= winRight && pos.top <= winBottom
                  ) || (
                      pos.right <= winRight && pos.bottom <= winBottom &&
                      pos.right >= winLeft && pos.bottom >= winTop
                  )){
                      // Change this to push the actual element if you need it.
                      inView.push( $(this).text() );
                  }
              });
      
              // For the purposes of this example, we only need the
              // first and last element, but in your application you may need all.
              var first = inView.shift(),
                  last = inView.pop();
      
              // Show the details in the info box.
              $('#info .wxh').text( winWidth+' x '+winHeight );
              $('#info .txl').text( winTop+' x '+winLeft );
              $('#info .report').text( 'Showing from '+first+' to '+last );
          });
      
          // The rest is just setup stuff, to make the area scrollable.
          for( var i=0; i<100; i++ ){
              $('ul').append('
    • List item '+i+'
    • '); } $(window).trigger('resize.measure'); }) ​

    提交回复
    热议问题