如何在滚动时平滑改变图像尺寸?

问题描述 投票:1回答:2

我有带有大徽标的标题,我想在滚动超过100像素后将其缩小,这可以正常工作,但不顺畅,我该如何使其顺畅?

我的代码:-

$(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.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>
javascript jquery css smooth-scrolling smoothing
2个回答
2
投票

解决方案

transition: all linear .5s

说明

您可以查看CSS中的transition属性。

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

1] transition-property:定义要设置动画的属性。它可以是单个属性,多个属性或all。2)transition-timing-function:要使用的过渡函数,它将定义动画的发生方式;3)transition-delay:定义动画需要多长时间才能完成;

参考

Using CSS transitions

示例

$(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.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>

1
投票

您可以使用CSS的过渡属性https://www.w3schools.com/cssref/css3_pr_transition-property.asp\

 header .logo img{width:200px;transition:width 0.3s ease-in-out 0s;}
© www.soinside.com 2019 - 2024. All rights reserved.