滚动时模糊个别div?

时间:2015-06-12 14:04:36

标签: javascript jquery html css timeline

我有一个时间轴,我希望第一个项目可以聚焦但是当我滚动时,我希望下一个项目是焦点而下一个项目,依此类推。到目前为止,我已设法只关注所有项目。我有什么想法可以做到这一点吗?

这里是my fiddle 这是我的代码

$(document).ready(function(){


    $('.timeline li').not('.timeline li:first')
  .css({
                            'filter': 'blur(2px)',
                            '-webkit-filter': 'blur(2px)',
                            '-moz-filter': 'blur(2px)',
                            '-o-filter': 'blur(2px)',
                            '-ms-filter': 'blur(2px)'
                        });


    });

$(window).scroll(function(){
    var prevTop = $(window).data("top");
    var  newTop = $(this).scrollTop();
if(prevTop)
{
    if(prevTop>newTop )
    {
       $('.timeline li').not('.timeline li:first')
  .css({
                            'filter': 'blur(2px)',
                            '-webkit-filter': 'blur(2px)',
                            '-moz-filter': 'blur(2px)',
                            '-o-filter': 'blur(2px)',
                            '-ms-filter': 'blur(2px)'
                        });
    }
    else{
     $('.timeline li')
 .css({
                            'filter': 'blur(0px)',
                            '-webkit-filter': 'blur(0px)',
                            '-moz-filter': 'blur(0px)',
                            '-o-filter': 'blur(0px)',
                            '-ms-filter': 'blur(0px)'
                        });
    }
}
    $(window).data("top",newTop );

});



<div class="wraphold">
        <div class="timeline-title"><span>Timeline</span></div>
    <ul class="timeline">
        <li>

            <div class="dot"><div class="date"><span>21st JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>
        <li>

            <div class="dot"><div class="date"><span>22nd JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>
        <li>

            <div class="dot"><div class="date"><span>23rd JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>
        <li>

            <div class="dot"><div class="date"><span>24th JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>

        <li>

            <div class="dot"><div class="date"><span>25th JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>
        <li>

            <div class="dot"><div class="date"><span>26th JUNE</span></div></div>
            <div class="box">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            </div>
        </li>

    </ul>
</div>

1 个答案:

答案 0 :(得分:1)

首先将此添加到您的CSS中,不要忘记为FF或IE等其他浏览器添加过滤器。

.blur {
    filter: blur(2px);
    -webkit-filter:blur(2px);
}

将javascript更改为此

$(document).ready(function () {
    $('.timeline li').not('.timeline li:first').addClass("blur")
});
var $window = $(window);

function isScrolledIntoView($elem, $window) {
    var docViewTop = $window.scrollTop();
    var docViewBottom = docViewTop + $window.height();

    var elemTop = $elem.offset().top;
    var elemBottom = elemTop + $elem.height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
var elements = $('.timeline li');
$(window).scroll(function () {
    elements.each(function () {
        $this = $(this);
        if (isScrolledIntoView($this, $window)) {
            $this.removeClass("blur");
        }
        else{
            $this.addClass("blur");
        }
    })
});

作为一个基本想法,如果你可以使用css类,那就不要使用.css属性。

Fiddle来测试

更新1 它不是双向工作,当它们离开视图时元素模糊