简体   繁体   English

不在视口中时移除元素

[英]Remove element when not in viewport

I'd like to trigger animations (rather than just transformations) on an element when it's in the viewport.当元素位于视口中时,我想在元素上触发动画(而不仅仅是转换)。 I read you can do this if you can create the element when it's in the viewport and delete it when it's not in the viewport.我读到如果您可以在视口中创建元素并在它不在视口中时将其删除,您可以这样做。

I've run into a challenge removing the element when it's not in the viewport.当元素不在视口中时,我遇到了删除元素的挑战。

This code works to add and remove classes to elements when they're in the viewport此代码用于在元素位于视口中时向元素添加和删除类

var scroll = window.requestAnimationFrame ||
             // IE Fallback
             function(callback){ window.setTimeout(callback, 1000/60)};

//create array of all elements with specific class
var elementsToShow = document.querySelectorAll('.show-on-scroll');

//create  loop function cycles above array calls function on it, adds clss if functions returns true, loop runs loop
function loop() {

    Array.prototype.forEach.call(elementsToShow, function(element){
      if (isElementInViewport(element)) {
        element.classList.add("is-visible");
} else {
element.classList.remove("is-visible");

}
});
scroll(loop);
}

// Call the loop for the first time
loop();

// Helper function from: http://stackoverflow.com/a/7557433/274826
//the function that will return true checks for list element in viewport
function isElementInViewport(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.bottom >= 0)
    ||
    (rect.bottom >= (window.innerHeight || document.documentElement.clientHeight) &&
      rect.top <= (window.innerHeight || document.documentElement.clientHeight))
    ||
    (rect.top >= 0 &&
      rect.bottom <= (window.innerHeight || document.documentElement.clientHeight))
  );
}

In the Array.prototype.forEach.call I've added this code, which I've been exploring (along with many other variations).在 Array.prototype.forEach.call 中,我添加了这段代码,我一直在探索(以及许多其他变体)。 It makes more sense now to check with someone with more js experience.现在与有更多 js 经验的人核实更有意义。

Is anyone familiar enough with this structure to spot a quick way through?有没有人对这种结构足够熟悉,可以快速通过?


Array.prototype.forEach.call(elementsToShow, function(element){
      if (isElementInViewport(element)) {
        element.classList.add("is-visible");

if (element.matches('this-element') && element.lastChild.matches("is-animated")){
const tag = document.createElement("p");
tag.id = "is-animated";
var textTag = document.createTextNode("Some text in the tag.");
tag.appendChild(textTag);
document.getElementById("this-element").appendChild(tag);
}

} else {
element.classList.remove("is-visible");

if(element.matches('is-animated')){
element.remove();
}

}
});
scroll(loop);
}

In the html there is div with id and class "this-element" with several child在 html 中有 div id 和 class "this-element" 有几个孩子

tags.标签。

This was a red-herring.这是一个红鲱鱼。 The solution was not to remove the elements when out of view, but to change the animation and transition duration, so they end quickly.解决方案不是在看不见的情况下删除元素,而是更改 animation 和过渡持续时间,以便它们快速结束。 See here:看这里:

function loop() {

Array.prototype.forEach.call(elementsToShow, function(element){

if (isElementInViewport(element)) {
element.classList.remove("end-transitions");
element.classList.add("is-visible");
}

else {
element.classList.remove("is-visible");
element.classList.add("end-transitions");
}
});

scroll(loop);
}


Where .end-transitions contains this:其中.end-transitions包含以下内容:

.end-transitions{
animation-duration:0s !important;
animation-delay:0s !important;
transition-duration: 0s !important;
transition-delay:0s !important;
}

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

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