javascript - Resizing logo when scrolling -
i have used following code change size of logo when scrolling down page.
$(document).on('scroll', function() { if ($(document).scrolltop() >= 10) { $('.logo img').css('width', '50px'); } else { $('.logo img').css('width', ''); } });
.nav { position: fixed top: 0; z-index: 1; width: 100%; } .nav .logo { position: fixed; text-align: left; z-index: 2; top: 0; overflow: hidden; opacity: .5; } .container { padding-top: 120px; height: 500px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="nav"> <div class="logo"> <a href="index.html"><img src="http://placehold.it/100x100" alt="" /></a> </div> </div> <div class="container"> lorem ipsum </div>
i transition smooth large small.
https://jsfiddle.net/sl89qxcx/
is there can add achieve smooth transition?
the best way achieve use css transitions, hardware accelerated, , better separation of concerns. can toggle animation adding/removing class in js.
the important part add both width
, transition
rule default state of .logo img
element. can amend width
in added class. try this:
$(document).on('scroll', function() { $('.logo img').toggleclass('small', $(document).scrolltop() >= 10); });
.nav { position: fixed top: 0; z-index: 1; width: 100%; } .nav .logo { position: fixed; text-align: left; z-index: 2; top: 0; overflow: hidden; opacity: .5; } .nav .logo img { width: 100px; transition: width 0.3s; } .nav .logo img.small { width: 50px; } .container { padding-top: 120px; height: 500px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="nav"> <div class="logo"> <a href="index.html"><img src="http://placehold.it/100x100" alt="" /></a> </div> </div> <div class="container"> lorem ipsum </div>
Comments
Post a Comment