向下移动整个 html 页面时出现问题! margin-top 到 html 元素不适用于所有页面

发布于 2024-10-30 10:40:38 字数 592 浏览 1 评论 0原文

我有一个奇怪的 css、JS 问题。我的 chrome 扩展应该将洞网站向下移动一些像素,这样我就可以在其顶部显示一个工具栏。

这适用于大多数页面,但不适用于少数页面。而且我无法找出问题所在。向下移动页面的代码非常简单。就这一行,它为 html 元素添加了一个 margin-top :

document.getElementsByTagName('html')[0].style.marginTop = '36px !important';

如果您安装插件并浏览到 www(dot)interspar(dot)at (这里有效)和 www(dot)weinwelt ,这是最容易解释和看到的(点)at(这里不起作用)。该插件位于https://chrome.google.com/webstore/detail/pilobbegphefikcgjpajnneiiahhejam

请不要将其视为垃圾邮件,因为此插件仅对德语人士有用,而且我认为这里的插件并不多;)

i have a strange css, JS problem. My chrome-extension should move the hole website some pixels down, so i can display a toolbar on top of it.

This works for most pages, but doesn't work for a few. And i can't find out what's the problem. The code for moving the page down is really simple. Just this line, which adds a margin-top to the html-element:

document.getElementsByTagName('html')[0].style.marginTop = '36px !important';

It's easiest explained and seen if you install the addon and browse to www(dot)interspar(dot)at (here it works) and to www(dot)weinwelt(dot)at (here it doesn't work). The addon is found here https://chrome.google.com/webstore/detail/pilobbegphefikcgjpajnneiiahhejam

Please don't consider this as spam, as this addon is only useful for german speaking people and i think there aren't many around here ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-11-06 10:40:38

它在该网站上不起作用,因为它的整个内容都是绝对定位的(请查看

)。您需要循环遍历整个 DOM,寻找具有 top 属性的绝对定位元素,并相应地增加其值。

但这只是冰山一角,因为某些网站会动态向页面添加绝对定位的元素,因此您还需要监听 dom 更改(DOMSubtreeModified 事件)并再次重新定位。

It doesn't work on that site because its whole content is positioned absolutely (look at <div class="parent">). You need to loop through the whole DOM looking for absolutely positioned elements that have top property and increase its value accordingly as well.

That's only a tip of the iceberg though, as some sites would be adding absolutely positioned elements to the page dynamically, so you would need to listen to dom changes (DOMSubtreeModified event) as well and do all repositioning again.

攒一口袋星星 2024-11-06 10:40:38

改为在身体上尝试一下;

document.getElementsByTagName('body')[0].style.marginTop = '36px !important';

HTML 标记 上不能有边距。

Try it on the BODY instead;

document.getElementsByTagName('body')[0].style.marginTop = '36px !important';

HTML tag can't have a margin on it.

终陌 2024-11-06 10:40:38

根据以下链接,“!重要”仅受 Safari 支持。

http://hungred.com/how-to/jquery-javascript-css-重要/

According to following link "!important" is only supported by safari.

http://hungred.com/how-to/jquery-javascript-css-important/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文