如何在滚动上调整大小较小的徽标图像的动画?

时间:2017-07-24 09:57:19

标签: image jquery-animate onscroll

我能够弄清楚如何在滚动时动画标题大小调整。但是,我似乎无法弄清楚如何设置标题徽标图像的动画以使其在滚动时变小。任何帮助修改下面的代码将不胜感激。感谢。



	$(document).on("scroll", function(){
		if
      ($(document).scrollTop() > 100){
		  $("header").addClass("shrink");
		}
		else
		{
			$("header").removeClass("shrink");
		}
	});

#logo {
  width:100%;
  margin:0 auto;
  position: fixed;
 
  }
  
 #content {
  width:100%;
  margin:0 auto;
  } 
  
  header {
  	width: 100%;
 	  padding: 30px 0;
	  background: #000;
  	border-bottom: 1px solid #fff;
  	/* animation magic */
  	-webkit-transition: all 0.4s ease-in-out;
  	-moz-transition: all 0.4s ease-in-out ;
  	z-index: 9999;
 	  top: 0;
  	position: fixed;
}

.shrink {
  	padding: 0;
}
  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<div id="logo">
<header><img src="https://www.computerhope.com/cdn/computer-hope.jpg"></header>
</div>

<div id="content">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>

</div>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我首先会改变你的HTML,如下所示:

 <header>
     <div id="logo">
         <img src="https://www.computerhope.com/cdn/computer-hope.jpg">
     </div>
 </header>

并改变你的css:

#logo 
{
   width:200px;/*give it a fixed size*/
   margin:0 auto;
   /* animation magic */
   -webkit-transition: all 1s ease-in-out;
   -moz-transition: all 1s ease-in-out ;
   -ms-transition: all 1s ease-in-out ;
   -o-transition: all 1s ease-in-out ;
   transition: all 1s ease-in-out ;
}

#logo img 
{
   width:100%;
   height:auto;
}

header 
{
   width: 100%;
   padding: 30px 0;
   background: #000;
   border-bottom: 1px solid #fff;
   z-index: 9999;
}

.shrink 
{
   width:100px;/*change the size here*/
}

将您的jQuery更改为:

$(document).on("scroll", function(){
    if($(document).scrollTop() > 100)
    {
       $("#logo").addClass("shrink");
    }
    else
    {
        $("#logo").removeClass("shrink");
    }
});
相关问题