【发布时间】:2017-09-09 16:02:35
【问题描述】:
我正在使用这个 javaScript 函数来修复右侧栏:
<script type="text/javascript">
$(document).ready(function () {
var top = $('#rightsidebar-wrapper').offset().top - parseFloat($('#rightsidebar-wrapper').css('marginTop').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('#rightsidebar-wrapper').addClass('fixed');
} else {
// otherwise remove it
$('#rightsidebar-wrapper').removeClass('fixed');
}
});
});
</script>
还有这个 CSS 来设置右侧边栏 div 的样式:
#rightsidebar-wrapper {
background: #ffffff;
width: 225px;
float: right;
margin-top: 8px 0px 0 0;
padding:0px;
word-wrap: break-word;
overflow: hidden;
}
#rightsidebar-wrapper.fixed {
position: fixed;
top: 5px;
}
这是放置在右侧的侧边栏。 问题是当侧边栏顶部在滚动时遇到屏幕顶端时,它会向左浮动。将其添加到 CSS 中
right: 10%;
它解决了这个问题,但是当页面被放大或缩小时,它再次失去了它的位置。 知道如何解决这个问题吗?
【问题讨论】:
-
你能解决这个问题吗?我将元素设置为绝对并使用 JS 计算使其工作。我遇到的问题是 IE 在您滚动时会抖动。 Chrome 和 Firefox 都很流畅。我还必须让我的响应,所以设置一个权利或边距权利将无济于事。我的解决方案在这里:jsfiddle.net/yq3rcp0j/7
标签: javascript css html position