这是一个使用绝对定位和jQuery动画Div的简单测试用例.
<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js'></script>
<script type='text/javascript'>
function slide(){
$('#box').animate({'left': 0},3000);
}
</script>
</head>
<body>
<button onclick="slide()">slide</button>
<div id="box" style="position: absolute; width: 120px; height: 100px; background: #ff0000; left: 500px"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
在Firefox 4(在Mac上)动画"泪流满面"并且非常恶劣.在Safari和Chrome中它更好,但仍然有明显的抖动.
将问题简化为上述测试用例后,我不确定下一步该怎么做.这是一个jQuery错误吗?我是否遗漏了导致浏览器重绘负载的绝对定位?如果有些人可以尝试上面的代码并进行思考,那将是非常感激....即使它只是让我放心,我不会生气:)
这是一个完整的测试用例:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style type="text/css">
html, body, ul, li, div, span {
padding: 0;
margin: 0;
}
ul.container {
display: table;
list-style-type: none;
margin-right: 24px;
position: relative;
}
ul.container li {
display: table-cell;
position: relative;
}
ul.container div, ul.container span {
border: 1px dotted #000;
}
ul.container div {
width: 40px;
height: 40px;
position: absolute;
left: 0;
top: 40px;
background-color: #008000;
}
ul.container span {
display: block;
width: 40px;
height: 40px;
background-color: #9acd32;
}
</style>
</head>
<body> …Run Code Online (Sandbox Code Playgroud) 我一直在试图让我的网站成为crossbrowser ..在IE6中我有一个相对定位的容器,里面有一个绝对的div,它有负的顶部和左边,但不是在相对的顶部div,正在下面看起来像这样:
++++++++++++++++++++++
+ container +
+++++++++++++er +
+llo +er +
+rld +er +
+++++++++++++er +
+ container +
+ container +
++++++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
注意:
++++++++++++++++++++++
+ container +
+++++++++++++++++++er +
+ hello +er +
+ world +er +
+++++++++++++++++++er +
+ container +
+ container +
++++++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
在所有其他浏览器中我只使用静态定位容器和hello world div的绝对位置,并且工作得很好,但在ie6绝对div是奇怪的定位,无论我怎么顶或左我没有移动,所以我想为IE6做这个,但我有上面描述的问题.
我有三个div标签,我想在div标签之间垂直留出空间,也希望将我的第一个div标签作为固定的。
当我将第一个div位置设置为非固定位置时,我可以留出垂直空间。
<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue;">
a
</div>
<div style="width:70%; height:100%; background-color:Gray; margin: auto; margin-top: 2em; margin-bottom: 2em;">
b
</div>
<div style="width:100%; height:5%; background-color:Aqua;">
c
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我将“ div a”的位置更改为固定值时,“ div a”和“ div b”都从空白处下降:2em。
<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue; position: fixed;">
a
</div>
<div style="width:70%; …Run Code Online (Sandbox Code Playgroud) 当你尝试绝对定位元素时tbody,tr甚至td你发现它在大多数浏览器中都不起作用.它在Firefox中按预期工作.不在IE,Edge和Chrome中.
position: relative在tbody,tr甚至td被忽略.然后第一个父母position: relative用作绝对放置的"锚".
BTW:position: relative当你设置做作品tbody来display: block.但是你可能会遇到表行宽度的麻烦.通常,子元素不再像表元素那样精确地表现.列已经消失..但它不是这个问题的一部分.
我的问题是:
为什么是position: relative忽略的tbody,tr td?
这种行为有什么理由吗?
这是一个应该修复的错误吗?
.example {
border: 1px solid #ccc;
position: relative;
background: #eee;
margin: 2em;
padding: 2em;
width: 50%;
}
.abs {
display: block;
position: absolute;
left: 100%;
top: 0;
}
table {
//border: 5px solid rgba(255,200,0,0.2);
border-collapse: collapse;
}
tbody {
border: …Run Code Online (Sandbox Code Playgroud)我有一个固定在屏幕顶部的元素,z-index 为 100。当我在 Chrome 中向上滚动页面时(OS X 上为 V. 21.0.1180.82,Windows 上为 V. 21.0.1180.83 m)我注意到一个渲染问题,看起来像这样:

更新:
我注意到每次<section>...</section>在向上滚动期间固定元素传递一个新元素时都会出现渲染问题。Chrome 似乎有一些与 HTML5 标签相关的问题,例如<section>...</section>或<nav>...</nav>。更改这些以<div>...</div>解决渲染问题,无论如何我仍然想继续使用 HTML5 标签。
有人知道解决这个问题的“黑客”吗?
在此之前,我将尝试创建一个可重现的代码示例并将错误提交给chromium。
是否可以使定位元素相对于当前不是其父元素的另一个元素?例子:
<div class="want-to-be-parent">
<div class="current-parent">
<div class="element-to-position"><!---content---></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,我想将“元素到位置”绝对定位在“想要成为父母”的左上角,但它目前相对定位在“当前父母”内。
编辑 - 假设所有这三个元素都具有“位置:相对”并且我无法控制它。我想绝对定位“元素到位置”,并让它相对于“想要成为父母”而不是“当前父母”进行绝对定位。我必须动态执行此操作,因为没有其他方法。
我现在对绝对定位有点困惑。我一直认为,如果我绝对定位一个元素,它将相对于它的父元素定位(与相对于它的通常位置(如相对定位)相反)。在做作业时,我现在遇到了这种情况,我很困惑:
<body>
<div> <!-- This is colored red in my example -->
...
</div>
<div style="position: absolute;"> <!-- This is colored green in my example -->
...
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
当然,当我设置的实际位置与left/ right/ top/bottom我得到什么,我会从一个绝对定位的元素的期望。那么是否position: absolute只是设置为在没有position: absolute另外指定的情况下采用它所在的确切位置?
我正在尝试创建两个单独的 div,一个覆盖屏幕的左半部分,一个覆盖屏幕的右侧。为什么我的代码只创建了一个 div 覆盖页面的左半部分,当我包含float:left和 时float:right?我该如何解决?
#col-1 {
position: fixed;
width: 50%;
float: left;
height: 100%;
background-color: #282828;
z-index: 1010101010
}
#col-2 {
position: fixed;
width: 50%;
float: right;
height: 100%;
background-color: #0080ff;
z-index: 1010101010
}Run Code Online (Sandbox Code Playgroud)
<div id="col-1">
<h1>This is half of a page</h1>
</div>
<div id="col-2">
<h1>This is another half of a page</h1>
</div>Run Code Online (Sandbox Code Playgroud)
旁注。当然,最重要的问题是将它们放置在相对于引用它们的文本高度的高度。我可以使用 JavaScript 来完成,查看offsetTop注释引用的属性,但随后我还必须处理视口调整大小和缩放,而 CSS 解决方案会将这些留给浏览器。因此,CSS 解决方案是更可取的。
这是博客响应式设计的一部分。如果页边空白处有足够的空间,则注释应显示为旁注;否则,它们将作为脚注出现。这篇文章更详细地解释了它。请记住,超过一定宽度后,无论如何我们都会有边距,因为如果要保持可读性,文本行不能超过一定长度,因此我们也可以使用其他未使用的空间。Tufte 是该解决方案的知名支持者。
到目前为止,我已经尝试了两种方法。它们都在标记 (a <sup>)上使用相对定位,以便能够以<span>相对于参考文本的顶部偏移量来定位注释 (a )。
我的第一次尝试使用绝对定位:
article {
width: 50%;
margin-left: auto;
margin-right: auto;
}
sup {
/* Needed to position sidenotes */
position: relative;
}
.sidenote {
width: 20%;
position: absolute;
/* BTW: This should be proportional to the note's height,
so as to make its vertical center align with the text
referencing it */
top: -100%;
} …Run Code Online (Sandbox Code Playgroud)css-position ×10
css ×9
html ×8
jquery ×2
animation ×1
css-float ×1
css-grid ×1
css-tables ×1
css3 ×1
html5 ×1
javascript ×1