简体   繁体   English

菜单栏仅垂直固定

[英]Menu bar stays fixed only vertically

I want to create a menu bar that stays vertically in place when you scroll up and down the screen, but if the user scrolls left and right, i want the menu bar to move left and right with the rest of the content.我想创建一个菜单栏,当您上下滚动屏幕时,该菜单栏垂直保持原位,但是如果用户左右滚动,我希望菜单栏与其余内容一起左右移动。

For example, if the user is on the home page and decides to scroll down to read the announcements.例如,如果用户在主页上并决定向下滚动以阅读公告。 I would want the left menu bar to stay in place.我希望左侧菜单栏保持原位。 but lets say the user zooms up on our webpage so the entire page doesnt fit on the screen.但是假设用户放大了我们的网页,因此整个页面不适合屏幕。 If the user decides to scroll all the way to the right side of the page, i would want the menu bar to disappear along with the other content on the very left of the page.如果用户决定一直滚动到页面右侧,我希望菜单栏与页面最左侧的其他内容一起消失。

Is there a way to do this with only css?有没有办法只用css来做到这一点?在此处输入图片说明

I had a similar problem and I found some help and did some customizations for my own need.我遇到了类似的问题,我找到了一些帮助并根据自己的需要进行了一些自定义。 Check my code below:在下面检查我的代码:

CSS CSS

#sidebar {
  top:70px; // make it for your own
  left: 0px; //make it for your own
  position: absolute;
}

jQuery jQuery

var leftOffset = parseInt($("#sidebar").css('left'));

$(window).scroll(function() {

  $("sidebar").css({

    'left': $(this).scrollLeft + leftOffset
  })
});

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

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