[英]Jumping effect for scroll fixed scrollTop()
我指的是以下教程:
雖然頁面中的演示看起來不錯,但是當我在自己的網站上實現該演示時,我得到了“跳躍”的效果。 通常,第一項跳到第二項的一半。
我該如何解決?
以下是我的HTML:
<div id="map_container">
<div id="map" class="well"></div>
</div>
<ul>
<li>1. Many contents here...</li>
<li>2. Many contents here...</li>
<li>3. Many contents here...</li>
<li>4. Many contents here...</li>
<li>5. Many contents here...</li>
<li>6. Many contents here...</li>
<li>7. Many contents here...</li>
</ul>
以下是Javascript:
$(document).ready( function(){
$('#map_container').css("width", $('#map').width());
$("#map_container").scrollFixed({hideBeforeDiv:'footer'});
});
(function($){
$.fn.scrollFixed = function(params){
params = $.extend( {appearAfterDiv: 0, hideBeforeDiv: 0}, params);
var element = $(this);
if(params.appearAfterDiv)
var distanceTop = element.offset().top + $(params.appearAfterDiv).outerHeight(true) + element.outerHeight(true);
else
var distanceTop = element.offset().top;
if(params.hideBeforeDiv)
var bottom = $(params.hideBeforeDiv).offset().top - element.outerHeight(true) - 10;
else
var bottom = 200000;
$(window).scroll(function(){
if( $(window).scrollTop() > distanceTop && $(window).scrollTop() < bottom )
element.css({'position':'fixed', 'top':'0'});
else
element.css({'position':'static'});
});
};
})(jQuery);
當Javascript動態添加position: fixed
於#map_container
,只需使用CSS將相同的#map_container
高度應用於ul
的第一項即可。 以下解決方案是使用SCSS完成的:
#map_container {
&.fixed {
position: fixed;
top: 0;
& ~ .ul .li:first-child {
margin-top: 210px;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.