使用纯javascript以百分比形式获取视口中的元素可见性

时间:2017-07-19 09:33:09

标签: javascript html css

我试图以百分比(数字%)来获取视口中元素的可见性。

下面是我厌倦的代码,但缺少了一些东西。

function calculateVisibilityForDiv(div$) {
    var windowHeight    = window.innerWidth || 
    document.documentElement.clientWidth,
    docScroll       = window.scrollTop || document.body.scrollTop,
    divPosition     = div$.offsetTop,
    divHeight       = div$.offsetHeight || div$.clientHeight,
    hiddenBefore    = docScroll - divPosition,
    hiddenAfter     = (divPosition + divHeight) - (docScroll + 
     windowHeight);

if ((docScroll > divPosition + divHeight) || (divPosition > docScroll + 
   windowHeight)) {
    return 0;
} else {
    var result = 100;

    if (hiddenBefore > 0) {
        result -= (hiddenBefore * 100) / divHeight;
    }

    if (hiddenAfter > 0) {
        result -= (hiddenAfter * 100) / divHeight;
    }

    return result;
 }
}

var getOffset = function(elem) {
  var box = { top: 0, left: 0 };
  if(typeof elem.getBoundingClientRect !== "undefined") box = 
  elem.getBoundingClientRect();
  return {
    x: box.left + (window.pageXOffset || document.scrollLeft || 0) - 
     (document.clientLeft || 0),
    y: box.top + (window.pageYOffset || document.scrollTop || 0)  - 
     (document.clientTop || 0)
   };
},

我正在尝试从文档视口中获取DOM元素可见性百分比。

1 个答案:

答案 0 :(得分:0)

我修改了几行代码并且工作正常。 检查下面的小提琴

https://jsfiddle.net/darjiyogen/q16c1m7s/1/



'use strict';
var results = {};

function display() {
  var resultString = '';
  $.each(results, function(key) {
    resultString += '(' + key + ': ' + Math.round(results[key]) + '%)';
  });
  $('p').text(resultString);
}

function calculateVisibilityForDiv(div$) {
  debugger;
  div$ = div$[0];

  var windowHeight = window.innerHeight || document.documentElement.clientHeight,
    docScroll = window.scrollTop || document.body.scrollTop,
    divPosition = div$.offsetTop,
    divHeight = div$.offsetHeight || div$.clientHeight,
    hiddenBefore = docScroll - divPosition,
    hiddenAfter = (divPosition + divHeight) - (docScroll + windowHeight);

  if ((docScroll > divPosition + divHeight) || (divPosition > docScroll + windowHeight)) {
    return 0;
  } else {
    var result = 100;

    if (hiddenBefore > 0) {
      result -= (hiddenBefore * 100) / divHeight;
    }

    if (hiddenAfter > 0) {
      result -= (hiddenAfter * 100) / divHeight;
    }

    return result;
  }
}

var getOffset = function(elem) {
  var box = {
    top: 0,
    left: 0
  };
  if (typeof elem.getBoundingClientRect !== "undefined") box = elem.getBoundingClientRect();
  return {
    x: box.left + (window.pageXOffset || document.scrollLeft || 0) - (document.clientLeft || 0),
    y: box.top + (window.pageYOffset || document.scrollTop || 0) - (document.clientTop || 0)
  };
};

function calculateAndDisplayForAllDivs() {
  $('div').each(function() {
    var div$ = $(this);
    results[div$.attr('id')] = calculateVisibilityForDiv(div$);
  });

  display();
}

$(document).scroll(function() {
  calculateAndDisplayForAllDivs();
});

$(document).ready(function() {
  calculateAndDisplayForAllDivs();
});

div {
  height: 300px;
  width: 300px;
  border-width: 1px;
  border-style: solid;
}

p {
  position: fixed;
  left: 320px;
  top: 4px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
<div id="div4">div4</div>
<p id="result"></p>
&#13;
&#13;
&#13;