【问题标题】:jQuery - how to make a fixed div scroll horizontally after scrolling verticallyjQuery - 如何在垂直滚动后使固定的 div 水平滚动
【发布时间】:2011-03-04 07:59:12
【问题描述】:

这是以下内容的一种变体,但略有不同:How do I get a fixed position div to scroll horizontally with the content? Using jQuery

这是我的变体:http://jsfiddle.net/Bdwc4/

基本上,我希望能够看到 div 最右侧的“x”,为此,div 必须是绝对的。但同时,我需要在垂直滚动时固定 div。换句话说,您应该始终能够在该固定位置看到“x”,同时垂直或水平滚动。它有点像我想要它做的,但只有当你在窗口的顶部。无论您垂直滚动到哪里,我都希望能够水平滚动。

如果您选择不使用上面的 jsfiddle,这是我正在使用的代码:

<style>
.scroll_fixed {
    left:500px;
    position:absolute
}
.scroll_fixed.fixed {
    position:fixed
} 
.x { float:right }

.foo { background-color: red; width: 150px; height:150px; left:500px }
body { width: 500px }
.header { margin-top: 100px }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
$(window).scroll(function(event) {
    var x = 0 - $(this).scrollLeft();
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y) {
        // if so, ad the fixed class
        $('.scroll_fixed').addClass('fixed');
    } else {
        // otherwise remove it
        $('.scroll_fixed').removeClass('fixed');
    }  
});
</script>
<div class="header"></div>
<div class="scroll_fixed foo"><div class="x">x</div></div>
<div>
    Enter a very long string of text
</div>

输入代码后,将浏览器窗口水平和垂直缩小,直到红色框中的“x”看不见,这将迫使你水平滚动才能看到它,而当你垂直滚动时,红色盒子应该在一个固定的位置。

【问题讨论】:

    标签: jquery css


    【解决方案1】:

    看起来这就是你所追求的:http://css-tricks.com/scrollfollow-sidebar/

    【讨论】:

    • 是的!这正是我想要的!很好的发现,谢谢!
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 2012-12-25
      • 2023-03-19
      • 1970-01-01
      • 2013-08-22
      • 2019-03-27
      相关资源
      最近更新 更多