简体   繁体   English

在滚动时检查元素是否在视口外的哪个事件?

[英]Which event to check if element is outside viewport on scroll?

I am trying to check if an element is inside my viewport when scrolling. 我正在尝试在滚动时检查元素是否在我的视口内。 If it is outside my viewport, I add a class that fixes the element to the top. 如果它在我的视口之外,我会添加一个将元素修复到顶部的类。

The function I use to determine if the element is outside the viewport is: 我用来确定元素是否在视口之外的函数是:

isElementInViewport : function(el) {
    //special bonus for those using jQuery
    if (typeof jQuery === "function" && el instanceof jQuery) {
        el = el[0];
    }

    var rect = el.getBoundingClientRect();

    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
        rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
    );
}

I added a scroll event which fires this function : 我添加了一个触发此函数的scroll事件:

$(window).on('scroll', function(event){
   testObject.isElementInViewport(event.target);
}

The problem here is that while scrolling, my Lenovo Yoga's CPU goes crazy. 这里的问题是,在滚动时,我的联想瑜伽的CPU变得疯狂。 I have tried: 我努力了:

  • polling with an interval 间隔polling
  • using a timeout function and a variable outside the event function scope to toggle on a certain interval 使用timeout functionvariable outside the event function scope来切换特定间隔

Both methods work, but I need a way to minimize performance impacts, because the page I use this in already has LOADS of JS working. 这两种方法都有效,但我需要一种方法来最小化性能影响,因为我使用它的页面已经有JS的LOADS工作。 I also need to fix the bar to top as soon as it gets outside the viewport and not a few milliseconds later. 我还需要在它到达视口之外时将其固定到顶部,而不是几毫秒之后。

Are there any low-performance solutions for this? 有没有针对此的低性能解决方案? Can this be done in CSS only? 这可以只在CSS中完成吗?

Edit 编辑

I've noticed that I didn't asked my question right. 我注意到我没有问我的问题。 The current answers below are working, but give the same HUGE performance impact when I scroll up and down a bit: 下面的当前答案是有效的,但当我向上和向下滚动时,会产生相同的巨大性能影响:

第一个答案 第二个答案

I need to prevent the script from needing so much CPU power! 我需要防止脚本需要这么多的CPU能力!

copied from jQuery – test if element is in viewport (visible on screen) jQuery复制- 测试元素是否在视口中(在屏幕上可见)

HTML HTML

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box orange"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

JQuery JQuery的

$.fn.isOnScreen = function() {
  var win = $(window);

  var viewport = {
    top: win.scrollTop(),
    left: win.scrollLeft()
  };
  viewport.right = viewport.left + win.width();
  viewport.bottom = viewport.top + win.height();

  var bounds = this.offset();
  bounds.right = bounds.left + this.outerWidth();
  bounds.bottom = bounds.top + this.outerHeight();

  return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

};

$(window).scroll(function() {
  if ($('.orange').isOnScreen() === true) {
    console.log("Element is in viewport")
  }
});

Fidde Fidde

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM