Blur individual div when scrolling?

161 views Asked by At

I have a timeline and i would like the first item to be focused but when scrolling i then want the next item to be in focus and the next and so on. So far i have managed to only get all the items to focus. Any ideas how i can do this?

here is my fiddle here is my code

$(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

There are 1 answers

0
Bobby Tables On BEST ANSWER

First add this to your CSS, don't forget to add the filter for the other browsers like FF or IE.

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

Change javascript to this

$(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");
        }
    })
});

As a basic idea if you can use css classes use them don't use .css property.

Fiddle to test

Update 1 Not it works both way, the elements are blurred when they go out of view