Fixing tabs to the top of the page, but underneath the header

我的梦境 提交于 2019-12-08 14:09:26
code-zoop

See this SO question, and answers

The basic approach is to position the element absolutely on page load but on scroll, change the positioning to fixed if supported, or move the element to the appropriate location.

<div id="fix" style="position:absolute;top:30px;">
My fixedish div
</div>

And some quick js (should be cleaned up to work cross-browser):

// register event handler
window.addEventListner("scroll", function(){
    var div = document.getElementById("fix");  
    if (document.body.scrollHeight > 30) {
        // fix it to the top
        div.style.position = "fixed";
        div.style.top = "0px";
    }
    else {
        // position it below the header
        div.style.position = "absolute";
        div.style.top = "30px";
    }
});

Remy Sharp, of Left Logic, and jQuery for Designers offers a tutorial on a similar-ish subject: fixed-floating-elements.

This emulates the shopping basket from the UK Apple store (click on a product to configure, and watch the summary/specification boxes as you scroll the page) covers pretty much everything you should need to know to adapt the technique to suit your own requirements.

Obviously, from the URL to the site, it does require (or, at least, makes use of) jQuery, rather than plain Javascript.

You could look into jQuery UI which has a tabs plugin, and there are many examples for use of that you can find after s short search.

I don't think you can do that with css only, as SLaks said. Even if there is some way of conditional statements or hacks I don't know of, I wouldn't do it because of mainly two reasons:

  • It would certainly not work with all browsers.
  • It certainly is not the appropiate choice (the language should match the problem).

Using javascript, I would suggest to use onScroll and scrollHeight to update the tabs css when they are at the top of the page.

Mine wasn't work underneath until I added this "z-index: 999;"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!