10

示例: http: //www.chartjs.org/

向下滚动时,每篇文章都会在它进入浏览器的视口时显示。CSS是

#examples article {
    -webkit-transition: opacity 200ms ease-in-out;
    -ms-transition: opacity 200ms ease-in-out;
    -moz-transition: opacity 200ms ease-in-out;
    -o-transition: opacity 200ms ease-in-out;
    transition: opacity 200ms ease-in-out;
    position: relative;
    margin-top: 20px;
    clear: both;
}

我试过这个css,但它不起作用。是否有一些与css一起工作的javascript?我怎样才能实现这种滚动->淡入效果?

4

4 回答 4

25

演示小提琴

你想要这样的东西吗?

   $(window).scroll(function () {

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

            var bottom_of_object = $(this).position().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'
                }, 500);

            }

        });

    });
于 2013-03-23T18:29:13.977 回答
6

穆罕默德的回答没有考虑任何绝对定位的图像......我们应该使用偏移量而不是位置

$(window).scroll(function () {

    /* Check the location of each desired element */
    $('.article').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'
            }, 500);

         }
     });
});
于 2014-04-16T16:40:09.587 回答
1

因为我在评论中读到:

如果您希望对象在对象的1/3可见时淡入,请使用以下代码:

jQuery:

$(document).ready(function() {

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

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

            var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;
            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'},500);

            }

        });

    });

});

我改变的唯一行是:

var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;

通过更改/3为例如/4,您可以让它淡入,当对象的 1/4 可见时。

于 2020-02-06T12:31:53.010 回答
0

最佳工艺:

HTML:

<div id="container">

    <div id="monster">Hello</div>
    <div>Hello</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>

</div>

jQuery:

$(function(){  // $(document).ready shorthand
  $('.monster').fadeIn('slow');
});

$(document).ready(function() {

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

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

            var bottom_of_object = $(this).position().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'},1500);

            }

        }); 

    });

});

CSS:

#container
{
    height:2000px;    
}

#container DIV
{ 
    margin:50px; 
    padding:50px; 
    background-color:pink; 
}

.hideme
{
    opacity:0;
}
于 2018-04-28T09:30:35.727 回答