我正在使用下面的代码,但我希望它能够动画,以便电话号码顺畅地向上滚动,类似于标题中的电话号码为此网站所做的方式:http://www.lyonsroofing.com
谢谢。
$(window).scroll(function(){
$("#theFixed").css("top",Math.max(28,45-$(this).scrollTop()));
});

#theFixed {
position:fixed;
top:45px;
}
.container {
width:100%;
height:2000px;
}
#logoright {
width:240px;
float:right;
text-align:right;
}
.logorightpad {
padding: 0;
}
.social {
float:right;
padding:0;
margin:3px 0 0 0;
}
.social-left {
float:left;
}

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="logoright">
<div class="logorightpad">
<div id="theFixed">
<div class="telephone">
<h2>555-555-1212</h2>
</div>
<div class="social">
<div class="social-right"><a target="_blank" href="https://www.facebook.com><i class="fa fa-facebook-square" aria-hidden="true"></i></a>
</div>
</div>
</div>
<div class="container">
</div>
&#13;
答案 0 :(得分:0)
使用jquery animate:http://api.jquery.com/animate/ 像shoule工作的东西: $(&#34;#theFixed&#34)动画({&#34;顶部&#34;,Math.max(28,45 - $(本).scrollTop())});
答案 1 :(得分:0)
使用css transition或jquery animate可以获得平滑的过渡效果。
$(window).scroll(function(){
$("#theFixed").css("top",Math.max(28,45-$(this).scrollTop()));
});
#theFixed {
position:fixed;
top:45px;
/* added transition*/
-webkit-transition: top 0.5s;
transition: top 0.5s;
}
.container {
width:100%;
height:2000px;
}
#logoright {
width:240px;
float:right;
text-align:right;
}
.logorightpad {
padding: 0;
}
.social {
float:right;
padding:0;
margin:3px 0 0 0;
}
.social-left {
float:left;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="logoright">
<div class="logorightpad">
<div id="theFixed">
<div class="telephone">
<h2>555-555-1212</h2>
</div>
<div class="social">
<div class="social-right"><a target="_blank" href="https://www.facebook.com><i class="fa fa-facebook-square" aria-hidden="true"></i></a>
</div>
</div>
</div>
<div class="container">
</div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</body>
</html>
我刚刚将transition: top 0.5s;
添加到您的CSS中。