javascript - 到达顶部时隐藏元素

标签 javascript html css scroll

我正在创建一个动画,其中在滚动时显示/隐藏引号。我设法在滚动时显示它,但我想在它到达屏幕顶部时隐藏它,因此每次滚动页面时它都会重复动画。

目前,它到达顶部时并没有隐藏它,但我找不到原因。 这是 codepen

HTML:

<section id="cont_quote">
    <img class="img_quote" src="image">
    <article class="cont_q">
        <p>Lorem ipsum dolor sit amet...</p>
        <blockquote>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.</blockquote>
   </article>
</section>

JS:

/* Every time the window is scrolled ... */
$(window).scroll( function(){

 /* Check the location of each desired element */
 $('#cont_quote blockquote').each( function(i){

    var bottom_of_object = $(this).offset().top + $(this).outerHeight();
    var bottom_of_window = $(window).scrollTop() + $(window).height();

    /* If the object is completely visible in the window, fade it it */
    if( bottom_of_window > bottom_of_object ){               
        $(this).animate({'opacity':'1'},1000);

    } else if($(this).offset().top) {
        $(this).animate({'opacity':'0'},1000);
    }

 }); 
});

另外,你知道如何修复滚动条上的图像吗?但是一旦到达 cont_quote 的末尾,就取消修复它?

最佳答案

您好,您可以试试下面的方法

/* Every time the window is scrolled ... */
$(window).scroll( function(){

 /* Check the location of each desired element */
 $('#cont_quote blockquote').each( function(i){

    var bottom_of_object = $(this).offset().top + $(this).outerHeight();
    var bottom_of_window = $(window).scrollTop() + $(window).height();

    /* If the object is completely visible in the window, fade it it */
    if( bottom_of_window > bottom_of_object ){               
        $(this).animate({'opacity':'1'},1000);

    }
    if ($(window).scrollTop() <  $(this).offset().top) {
        $(this).animate({'opacity':'0'},1000);
    }

 }); 
});

关于javascript - 到达顶部时隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49896208/

相关文章:

javascript - 不正确的高度iphone

javascript - 创建长度为 n 的空数组

javascript - Meteor - 在哪里正确放置计算方法

javascript - React/Bootstrap 如何让图片适配页面?

html - 我无法在 UIWebview 中使背景图像在 Xcode 中工作

javascript - 如何使用弧法创建自定义圆?

javascript - 在 android 中从 webview 中拉出单个变量?

javascript - HTML5 : Placing a canvas on a video with controls

html - 如何获取导航栏后面的图像

css - 如何将外部资源/字体添加到 Firefox 扩展中?