javascript - Switching CSS Background Image With jQuery On Scroll -
i'm trying header made gets smaller on scroll. currently, code looks jquery involved in header size:
$(function() { $('#header').data('size','big'); }); $(window).scroll(function() { if ($(document).scrolltop() > 0) { if ($('#header').data('size') == 'big') { $('#header').data('size', 'small'); $('#header').stop().animate({ height: '60px' }, 600); } } else { if ($('#header').data('size') == 'small') { $('#header').data('size', 'big'); $('#header').stop().animate({ height: '100px' }, 600); } } });
it working great, , have header size portion down. how can replicate in order change size of logo 80 x 80 40 x 40 (and include transition)?
here's css logo:
#logo { background: url("images/logo.png") no-repeat; position: absolute; top: 8px; width: 81px; height: 85px; z-index: 73; -webkit-transition: background-image .5s; margin-left: 100px; }
*edited include full code
your js:
$(function() { $('#header').data('size','big'); }); $(window).scroll(function() { if ($(document).scrolltop() > 0) { if ($('#header').data('size') == 'big') { $('#header').data('size', 'small'); $('#header').stop().animate({ height: '60px' }, 600); $('#logo').stop().animate({ height: '40px', width: '40px' },600); } } else { if ($('#header').data('size') == 'small') { $('#header').data('size', 'big'); $('#header').stop().animate({ height: '100px' }, 600); $('#logo').stop().animate({ height: '85px', width: '81px' },600); } } });
and css:
#logo { background: url("images/logo.png") no-repeat; background-size: contain; position: absolute; top: 8px; width: 81px; height: 85px; z-index: 73; -webkit-transition: background-image .5s; margin-left: 100px; }
i'd point out while jquery nice , easily, (myself included) might consider preferable toggle css class (something 'smaller') on both #header , #logo , use css resizing , animation. css transition be:
#header, #logo { transition: height 0.6s, width 0.6s; }
Comments
Post a Comment