简体   繁体   English

在页面顶部插入 Div,插入 Div 后将所有内容向下移动

[英]Insert Div at the top of a page, shifting everything down after inserted Div

I have a userscript that adds a navigation bar to Google similar to the one they had a few years back.我有一个用户脚本,它向 Google 添加了一个导航栏,类似于几年前的导航栏。
The way I have the script set up now I have to hard code CSS code for each site that it matches in order for my bar to be at the top of the page, and to shift everything down.我现在设置脚本的方式我必须为它匹配的每个站点硬编码 CSS 代码,以便我的栏位于页面顶部,并将所有内容向下移动。

else if (/google.{2,6}forms/.test(vCurrentWebpage) == true ||
    /google.{2,6}slides/.test(vCurrentWebpage) == true ||
    /google.{2,6}sheets/.test(vCurrentWebpage) == true ||
    /google.{2,6}docs/.test(vCurrentWebpage) == true ||
    /google.{2,6}services/.test(vCurrentWebpage) == true ||
    /google.{2,6}cloudprint/.test(vCurrentWebpage) == true
) {
    GM_addStyle(`
    body{position:relative!important;
         top:29px!important;}
    .header,.stickyMobileHeader .mobileSearchWrapper,.mobileNavHeader{top:29px!important;}

    #GoogleBar{position:fixed!important;}
    `);

This method does work for most sites, but for sites with a fixed element on the bottom, the bottom element gets cut off :这种方法适用于大多数网站,但对于底部有固定元素的网站,底部元素会被截断

用我的酒吧底部元素被切断

But, the bottom element should be completely visible :但是,底部元素应该是完全可见的

好底

Is there a more reliable way to insert an element at the top of the page?有没有更可靠的方法在页面顶部插入元素?

I've searched, and searched but I can't seem to figure this one out myself.我搜索过,搜索过,但我自己似乎无法弄清楚这一点。

Here's a link to my full code 这是我的完整代码的链接

  • Short Answer: you can't (in an elegant way)简短回答:你不能(以优雅的方式)

  • Long Answer: you still can't because google calculates the height of the elements based on the visual-height of the window, and not of the relative parents, meaning that doesn't matter the technique you can't affect the calculation that is inserted as inline-style to the page.长答案:你仍然不能,因为谷歌根据窗口的视觉高度计算元素的高度,而不是相对父母的高度,这意味着你不能影响计算的技术无关紧要作为内联样式插入到页面中。 but you still have other UX tricks to do that like a button that opens your menu and overlaps for a moment the real menu.但是您仍然有其他 UX 技巧可以做到这一点,例如打开菜单并与真实菜单重叠片刻的按钮。

but...但...

you can affect the element that controls the height with a hacky js您可以使用 hacky js 影响控制高度的元素

// for google spreadsheets go to the console and try
var grid = $('.grid-table-container + .grid-table-container')
grid.style.height = parseInt(grid.style.height, 10) - 30 + 'px'

that will give you enough space to put something at the top and remember to transform: translateY(##) the body instead of the top prop that you have这会给你足够的空间在顶部放置一些东西并记住transform: translateY(##)身体而不是你拥有的top道具

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

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