Javascript 固定/粘性div内容在粘贴到顶部后向左移动
我想在下面滚动时将某个div粘贴到顶部。经过一些研究,我找到了这个解决方案。一切正常,除了滚动div时div的内容有点向左移动 javascriptJavascript 固定/粘性div内容在粘贴到顶部后向左移动,javascript,html,css,sticky,Javascript,Html,Css,Sticky,我想在下面滚动时将某个div粘贴到顶部。经过一些研究,我找到了这个解决方案。一切正常,除了滚动div时div的内容有点向左移动 javascript function sticky_relocate() { var window_top = $(window).scrollTop(); var div_top = $('#sticky-anchor').offset().top; if (window_top > div_top) { $('
function sticky_relocate() {
var window_top = $(window).scrollTop();
var div_top = $('#sticky-anchor').offset().top;
if (window_top > div_top) {
$('#sticky').addClass('stick');
} else {
$('#sticky').removeClass('stick');
}
}
$(function () {
$(window).scroll(sticky_relocate);
sticky_relocate();
});
CSS
#sticky {
padding: 0.5ex;
font-size: 2em;
border-radius: 0.5ex;
}
#sticky.stick {
position: fixed;
top: 0;
z-index: 10000;
border-radius: 0 0 0.5em 0.5em;
}
#sticky.stick {
position: fixed;
top: 0;
left:0;
right:0;
z-index: 10000;
}
HTML
<hr />
<div id="sticky-anchor"></div>
<div id="sticky">
<center>Unternehmen Leistungen Kunden Stärken</center>
<hr /></div>
#粘性{
填充:0.5ex;
字号:2em;
边界半径:0.5ex;
}
#粘滞的{
位置:固定;
排名:0;
z指数:10000;
边界半径:0.5em 0.5em;
}
圣尔肯雷斯通根酒店
您必须将div居中以防止该移位。将div
与位置居中:固定
,您需要将左
和右
设置为0
CSS
#sticky {
padding: 0.5ex;
font-size: 2em;
border-radius: 0.5ex;
}
#sticky.stick {
position: fixed;
top: 0;
z-index: 10000;
border-radius: 0 0 0.5em 0.5em;
}
#sticky.stick {
position: fixed;
top: 0;
left:0;
right:0;
z-index: 10000;
}
函数粘性_重定位(){
var window_top=$(window.scrollTop();
var div_top=$('#粘性锚点').offset().top;
如果(窗口顶部>分区顶部){
$('#sticky').addClass('stick');
}否则{
$('#sticky')。移除类('stick');
}
}
$(函数(){
$(窗口)。滚动(粘性重新定位);
粘性_重新定位();
});代码>
#粘性{
位置:相对位置;
填充:0.5ex;
字号:2em;
文本对齐:居中;
}
#粘滞的{
位置:固定;
排名:0;
左:0;
右:0;
z指数:10000;
}
.内容{
高度:1000px;
}
圣尔肯雷斯通根酒店
您的html是什么样子的?