简体   繁体   English

左浮动div中相对定位的元素

[英]Relatively positioned elements in a float left div

I have the following css: 我有以下CSS:

.contents{
    width: 70%;
    float: right;
}

.sidebar{
    width: 25%;
    float: right;
}

div.area {
    position: relative;
    border: 1px solid;
}

span.letter {
    position: absolute;
    font-family: mono;
    font-size: 14;
}

and the following html structure: 和以下html结构:

<div class="sidebar">

</div>
<div class="content">
    <button id="button">>>></button>
    <div id="area">
        <span class="letter"></span>
        <span class="letter"></span>
        <span class="letter"></span>
        ...
    </div>
</div>

However, the letters, which are later position using the jquery.animate({left: ?px, top: ?px}) function, show up in the sidebar. 但是,这些字母(稍后使用jquery.animate({left:?px,top:?px})函数定位)显示在侧边栏中。

I should mention that I initially had the button and area-div on their own page, where the problem didn't occur to me. 我应该提到的是,我最初在自己的页面上有button和area-div,这对我来说并没有出现问题。

How do I get the letters back in the area div? 我如何才能将字母恢复到div区域?

Edit: Here's a jsfiddle version: http://jsfiddle.net/herrturtur/mPBgg/ 编辑:这是一个jsfiddle版本: http : //jsfiddle.net/herrturtur/mPBgg/

I think I managed to produce something like what you wanted. 我想我设法产生了您想要的东西。

Brian was right. 布莱恩是对的。 The letters are appearing there because you're putting them there with absolute positioning in the javascript, and there's not much you can do to fix it without messing with the JS. 这些字母之所以出现在这里,是因为您在javascript中以绝对的位置将它们放在那里,并且在不弄乱JS的情况下,您可以做很多事情来解决它。 You had the letter absolutely positioned before via JQuery's .offset, but it just seem so much simpler to just remove that positioning entirely and let them go where they would normally with position: block , which is right on top of one another, so that's what I did: 您已经通过JQuery的.offset将字母绝对定位在了前面,但是看起来完全简单的是完全删除该定位并将它们放到通常具有position: block ,它彼此叠置,所以就是这样我做了:

CSS: CSS:

div#area {   //<--- you had a '.' instead of '#' here. Irrelevant, but I thought I'd mention it
    position: relative;
    border: 1px solid;
}

span.letter {
    display: block;   //<--- added this
    font-family: mono;
    font-size: 14;
}

JS: JS:

/*
 * Draws one letter to the screen at the specified position.
 */
function createLetter(letter){    //removed extraneous parameter
    var letter = $('<span class="letter">' + letter + '</span>');
    $("#area").append(letter);
    //  letter.offset(position);  Commented this out to leave letters where they are
    return letter;
}

Since I removed the position parameter to the createLetter method, I also updated the one line of code where it was called in the createWord method. 由于我将position参数删除到createLetter方法中,因此我还更新了在createWord方法中调用的一行代码。 You seem to be passing around a parameter named position a lot, and if you apply the change I suggested I believe you'll be able to pull it out of several methods to make your code a bit more concise. 您似乎经常绕过一个名为position的参数,并且如果您应用更改,我建议我相信您可以从几种方法中拉出它,以使您的代码更加简洁。

If you need them to go horizontally again, you can probably just remove the position: block with JS and maybe adding some padding to tweak it. 如果您需要它们再次水平移动,则可以删除position: block使用JS position: block并添加一些填充以对其进行调整。 As I said, your problem seemed so much better suited to normal positioning that I think that's the way to go. 正如我所说,您的问题似乎更适合常规定位,我认为这是要走的路。

You've also probably noticed that there's an extra J that wasn't there before. 您可能还注意到,以前没有一个额外的J。 There were actually 2 J's previously, just stacked perfectly on top of each other so you couldn't seem them, but this change to normal document flow makes the first one reappear. 实际上以前有2个J,它们彼此完全叠放在一起,所以您看不到它们,但是对常规文档流的这种更改使第一个重新出现。 If you need help getting rid of it I can look through your code again, but you should probably do so in another question since its so far digressed from your original problem in this one. 如果您需要摆脱它的帮助,我可以再次浏览您的代码,但是您可能应该在另一个问题中进行查找,因为到目前为止,它与您原来的问题有所出入。 :D :D

I THINK that the problem is that the CSS for span.letter has a position absolute, which positions it against the whole page, and not the local block. 我认为问题在于,span.letter的CSS具有绝对位置,它将其定位在整个页面上,而不是本地块上。

I modified span.letter to be: 我将span.letter修改为:

span.letter {
    font-family: mono;
    display: block;
}

This positioned them in the area block, which is what I think you're looking for. 这将它们定位在区域块中,这就是我认为您正在寻找的区域。

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

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