最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make image size change smoothly on scroll? - Stack Overflow

programmeradmin1浏览0评论

I have header with big logo i want to make it small after scroll more than 100px, this is working fine but not smoothly, how can i do it smooth?

My Code:-

$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100){
            $('header').addClass('fixed-header');
        }
        else{
            $('header').removeClass('fixed-header');
        }
    });
});
header{ padding:0px; position: sticky; top:0px; left:0px; background: #FFF; z-index: 1000;}
header.fixed-header{box-shadow: 20px 4px 10px rgba(39, 59, 69, 0.2);}

header .logo img{width:200px;}
header.fixed-header .logo img{width:100px;}

.box-height{ height:500px;}
<script src=".3.1/jquery.min.js"></script>

<header>
    <div class="col-md-4">
      <a href="#" class="logo"><img src=".png"></a>
  </div>
</header>

<div class="box-height"></div>

I have header with big logo i want to make it small after scroll more than 100px, this is working fine but not smoothly, how can i do it smooth?

My Code:-

$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100){
            $('header').addClass('fixed-header');
        }
        else{
            $('header').removeClass('fixed-header');
        }
    });
});
header{ padding:0px; position: sticky; top:0px; left:0px; background: #FFF; z-index: 1000;}
header.fixed-header{box-shadow: 20px 4px 10px rgba(39, 59, 69, 0.2);}

header .logo img{width:200px;}
header.fixed-header .logo img{width:100px;}

.box-height{ height:500px;}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<header>
    <div class="col-md-4">
      <a href="#" class="logo"><img src="https://www.freepnglogos./uploads/google-logo-png-hd-11.png"></a>
  </div>
</header>

<div class="box-height"></div>

Share Improve this question edited Feb 27, 2020 at 11:28 Romulo 5,1142 gold badges21 silver badges31 bronze badges asked Feb 27, 2020 at 11:09 Rohit VermaRohit Verma 3,7858 gold badges42 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Solution

transition: all linear .5s

Explanation

You can take a look at the transition property in CSS.

Taking into consideration the solution above, here's a break down of the syntax:

1) transition-property: defines which property you want to animate. It can be a single property, multiple properties or all;
2) transition-timing-function: transition function to be used, it will define how the animation will occurs;
3) transition-delay: defines how long the animation will take to finish;

References

Using CSS transitions

Example

$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100){
            $('header').addClass('fixed-header');
        }
        else{
            $('header').removeClass('fixed-header');
        }
    });
});
header{ padding:0px; position: sticky; top:0px; left:0px; background: #FFF; z-index: 1000;}
header.fixed-header{box-shadow: 20px 4px 10px rgba(39, 59, 69, 0.2);}

header .logo img{width:200px; transition: all linear .5s}
header.fixed-header .logo img{width:100px;}

.box-height{ height:500px;}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<header>
    <div class="col-md-4">
      <a href="#" class="logo"><img src="https://www.freepnglogos./uploads/google-logo-png-hd-11.png"></a>
  </div>
</header>

<div class="box-height"></div>

You can use Transition Property of css:
https://www.w3schools./cssref/css3_pr_transition-property.asp\

 header .logo img{width:200px;transition:width 0.3s ease-in-out 0s;}
发布评论

评论列表(0)

  1. 暂无评论