向下移动整个 html 页面时出现问题! margin-top 到 html 元素不适用于所有页面
我有一个奇怪的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它在该网站上不起作用,因为它的整个内容都是绝对定位的(请查看
)。您需要循环遍历整个 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 havetop
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.改为在身体上尝试一下;
HTML 标记 上不能有边距。
Try it on the BODY instead;
HTML tag can't have a margin on it.
根据以下链接,“!重要”仅受 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/