在滚动视口中显示元素

时间:2018-04-19 08:28:10

标签: javascript jquery html scroll viewport

我一直试图在视口中显示滚动元素,当没有时,再次隐藏它。但无论我尝试什么,我都无法使它发挥作用。

这是我到目前为止所做的,但该功能只在页面加载时运行一次,而不是在滚动时运行,因此它不会更新值。

$(window).scroll(function() {
    var top_of_element = $("#cont_quote blockquote").offset().top;
    var bottom_of_element = $("#cont_quote blockquote").offset().top + $("#cont_quote blockquote").outerHeight();
    var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
    var top_of_screen = $(window).scrollTop();

    if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
       $('#cont_quote blockquote').animate({'opacity':'1'},1000);
    }
    else {
       $('#cont_quote blockquote').animate({'opacity':'0'},1000);
    }
});



<section id="cont_quote">
    <article class="cont_q">
        <blockquote>Lorem ipsum</blockquote>
    </article>
</section>

3 个答案:

答案 0 :(得分:2)

在纯javascript中,你可以做类似这样的事情,它比jQuery方法使用更少的资源:

&#13;
&#13;
function inViewport( element ){
  
  // Get the elements position relative to the viewport

  var bb = element.getBoundingClientRect();
  
  // Check if the element is outside the viewport
  // Then invert the returned value because you want to know the opposite

  return !(bb.top > innerHeight || bb.bottom < 0);
  
}

var myElement = document.querySelector( 'div' );

// Listen for the scroll event

document.addEventListener( 'scroll', event => {
 
  // Check the viewport status

  if( inViewport( myElement ) ){
    
    myElement.style.background = 'red';
    
  } else {
    
    myElement.style.background = '';
    
  }
  
})
&#13;
body {
  
  height: 400vh;
  
}

div {
  
  width: 50vw;
  height: 50vh;
  position: absolute;
  top: 125vh;
  left: 25vw;
  transition: background 4s;
  border: 1px solid red;
  
}
&#13;
<p>Scroll Down</p>
<div></div>
&#13;
&#13;
&#13;

以下是不透明度更改的代码段:

&#13;
&#13;
function inViewport( element ){
  
  // Get the elements position relative to the viewport

  var bb = element.getBoundingClientRect();
  
  // Check if the element is outside the viewport
  // Then invert the returned value because you want to know the opposite

  return !(bb.top > innerHeight || bb.bottom < 0);
  
}

var myElement = document.querySelector( 'div' );

// Listen for the scroll event

document.addEventListener( 'scroll', event => {
 
  // Check the viewport status

  if( inViewport( myElement ) ){
    
    myElement.style.opacity = 1;
    
  } else {
    
    myElement.style.opacity = '';
    
  }
  
})
&#13;
body {
  
  height: 400vh;
  
}

div {
  
  width: 50vw;
  height: 50vh;
  position: absolute;
  top: 125vh;
  left: 25vw;
  transition: opacity 1s;
  opacity: .2;
  background: blue;
  
}
&#13;
<p>Scroll Down</p>
<div></div>
&#13;
&#13;
&#13;

以下是一个片段,向您展示如何定义触发视口的位置,我只是将innerHeight0值更改为object,您可以在其中定义像素数量应从顶部开始,从底部开始的像素数量。不要忘记为resize添加事件监听器,因为如果您的视口更改,这些基于像素的值将会更改,因此您的myViewport对象需要相应更新:

&#13;
&#13;
function inViewport( element, viewport = { top: 0, bottom: innerHeight } ){
  
  // Get the elements position relative to the viewport

  var bb = element.getBoundingClientRect();
  
  // Check if the element is outside the viewport
  // Then invert the returned value because you want to know the opposite

  return !(bb.top > viewport.bottom || bb.bottom < viewport.top);
  
}

var myViewport = { top: innerHeight * .4, bottom: innerHeight * .6 };
var myElement = document.querySelector( 'div' );

// Listen for the scroll event

document.addEventListener( 'scroll', event => {
 
  // Check the viewport status

  if( inViewport( myElement, myViewport ) ){
    
    myElement.style.opacity = 1;
    
  } else {
    
    myElement.style.opacity = '';
    
  }
  
})

window.addEventListener( 'resize', event => {
 
  // Update your viewport values

  myViewport.top = innerHeight * .4;
  myViewport.bottom = innerHeight * .6;
  
})
&#13;
body {
  
  height: 400vh;
  
}

div {
  
  width: 50vw;
  height: 50vh;
  position: absolute;
  top: 125vh;
  left: 25vw;
  transition: opacity 1s;
  opacity: .2;
  background: blue;
  
}
&#13;
<p>Scroll Down</p>
<div></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试使用:

$(window).on('scroll mousewheel', function() {

用你的功能包围你的功能:

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

答案 2 :(得分:0)

我试图仅通过您的代码来解决您的问题。它现在对我来说很好。请试试这个,让我知道。还打开浏览器控制台,查看是否有任何js错误。

$(window).scroll(function() {
  var top_of_element = $("#cont_quote blockquote").offset().top;
  var bottom_of_element = $("#cont_quote blockquote").offset().top + $("#cont_quote blockquote").outerHeight();
  var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
  var top_of_screen = $(window).scrollTop();

  if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
    $('#cont_quote blockquote').fadeIn(1000);
    console.log('if cond');
  } else {      
    $('#cont_quote blockquote').fadeOut(1000);
    console.log('else cond');
  }
});