Blur a separate div on scrolling?
I have a timeline and I would like the first element to be focused, but when scrolling, I want the next element to be in focus, and the next, and so on. So far I have managed to get all the focusing items. 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>
+3
source to share
1 answer
Add this to your CSS first, don't forget to add filter for 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, don't use them property .css
.
Fiddle for testing
Update 1 This doesn't work in both directions, elements are blurry when they go out of sight.
+1
source to share