Fixed position but relative to container

后端 未结 25 1630
独厮守ぢ
独厮守ぢ 2020-11-21 23:52

I am trying to fix a div so it always sticks to the top of the screen, using:

position: fixed;
top: 0px;
right: 0px;

However,

25条回答
  •  时光说笑
    2020-11-22 00:20

    I have created this jQuery plugin to solve a similar issue I was having where I had a centered container (tabular data), and I wanted the header to fix to the top of the page when the list was scrolled, yet I wanted it anchored to the tabular data so it would be wherever I put the container (centered, left, right) and also allow it to move left and right with the page when scrolled horizontally.

    Here is the link to this jQuery plugin that may solve this problem:

    https://github.com/bigspotteddog/ScrollToFixed

    The description of this plugin is as follows:

    This plugin is used to fix elements to the top of the page, if the element would have scrolled out of view, vertically; however, it does allow the element to continue to move left or right with the horizontal scroll.

    Given an option marginTop, the element will stop moving vertically upward once the vertical scroll has reached the target position; but, the element will still move horizontally as the page is scrolled left or right. Once the page has been scrolled back down passed the target position, the element will be restored to its original position on the page.

    This plugin has been tested in Firefox 3/4, Google Chrome 10/11, Safari 5, and Internet Explorer 8/9.

    Usage for your particular case:

    
    
    
    $(document).ready(function() {
        $('#mydiv').scrollToFixed();
    });
    

提交回复
热议问题