如何在滚动时平滑更改图像大小?

Roh*_*rma 3 javascript css jquery smoothing smooth-scrolling

我有一个带有大徽标的标题,我想在滚动超过 100 像素后将其缩小,这工作正常但不流畅,我怎样才能让它平滑?

我的代码:-

$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100){
            $('header').addClass('fixed-header');
        }
        else{
            $('header').removeClass('fixed-header');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)
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;}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/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.com/uploads/google-logo-png-hd-11.png"></a>
  </div>
</header>

<div class="box-height"></div>
Run Code Online (Sandbox Code Playgroud)

Rom*_*ulo 6

解决方案

transition: all linear .5s
Run Code Online (Sandbox Code Playgroud)

解释

您可以查看transitionCSS中的属性。

考虑到上述解决方案,以下是语法的细分:

1) transition-property: 定义你想要动画的属性。它可以是单个属性、多个属性或all
2) transition-timing-function: 要使用的过渡函数,它将定义动画将如何发生;
3) transition-delay: 定义动画完成需要多长时间;

参考

使用 CSS 过渡

例子

transition: all linear .5s
Run Code Online (Sandbox Code Playgroud)
$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100){
            $('header').addClass('fixed-header');
        }
        else{
            $('header').removeClass('fixed-header');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)
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;}
Run Code Online (Sandbox Code Playgroud)