简体   繁体   English

jQuery - 如何在垂直滚动后水平固定div滚动

[英]jQuery - how to make a fixed div scroll horizontally after scrolling vertically

This is kind of a variation of the following, but a little different: How do I get a fixed position div to scroll horizontally with the content? 这是以下的一种变体,但有一点不同: 如何获得固定位置div以与内容水平滚动? Using jQuery 使用jQuery

Here is my variation: http://jsfiddle.net/Bdwc4/ 这是我的变化: http//jsfiddle.net/Bdwc4/

Basically, I would like to be able to see that "x" on the far right of the div, and in order to do so, the div must be absolute. 基本上,我希望能够在div的最右边看到“x”,为了做到这一点,div必须是绝对的。 But at the same time, I need the div to be fixed when scrolling vertically. 但与此同时,我需要在垂直滚动时修复div。 In other words, you should always be able to see the "x" in that fixed position, while scrolling vertically or horizontally. 换句话说,在垂直或水平滚动时,您应始终能够在该固定位置看到“x”。 It kind of does what I want it to do, but only when you're at the top of the window. 它有点像我想要它做的,但只有当你在窗口的顶部。 I'd like to be able to scroll horizontally, regardless of where you scroll vertically. 无论你在哪里垂直滚动,我都希望能够水平滚动。

If you choose not to use jsfiddle above, here is the code I'm using: 如果你选择不使用上面的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>

After you enter in the code, shrink the browser window horizontally and vertically until the "x" in the red box is out of view, which will force you to scroll horizontally to see it, and while you scroll vertically, the red box should be in a fixed position. 输入代码后,水平和垂直缩小浏览器窗口,直到红色框中的“x”不在视图中,这将迫使您水平滚动查看它,当您垂直滚动时,红色框应该是在固定的位置。

看起来这就是你想要的: http//css-tricks.com/scrollfollow-sidebar/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM