简体   繁体   English

在div内移动内容?

[英]Moving content inside of a div?

UPDATE: 更新:

The parent div (#container) must have a fixed height. 父div(#container)必须具有固定的高度。 I jsut want to shift the content in it so the active div is at the top and all overflow is hidden. 我想在其中移动内容,以便活动div位于顶部,所有溢出都被隐藏。


I am working on a webpage where a parent div has a fix size and will have several child divs in it. 我正在处理一个网页,其中父div的大小固定,并且其中会有几个子div。 Each of these child divs will have an anchor tag that is always visible and it's own child div that will only be shown when it's sibling anchor tag is clicked. 这些子div中的每个子div都有一个始终可见的锚标记,并且它自己的子div仅在单击其同级锚标记时才会显示。

I have this much working but I am running into the issue that when the sibling div is shown it sometimes goes out of the parent div and it's content is hidden. 我已经做了很多工作,但是我遇到了一个问题,当显示同级div时,有时它会超出父div,并且其内容被隐藏了。 I would like to have the contents of this div repositioned to the top of the parent div so that it's content is never hidden. 我想将此div的内容重新定位到父div的顶部,以使其内容永远不会被隐藏。 How would I accomplish this using jQuery? 我将如何使用jQuery完成此操作?

You can see what I'm talking about by clicking Item2 in this Fiddle: http://jsfiddle.net/Broham/LA22t/1/ 您可以通过单击此小提琴中的Item2来查看我在说什么: http//jsfiddle.net/Broham/LA22t/1/

Html: HTML:

<div id="container">
    <div class="item">
        <a href="#">Item1</a>
        <div class="desc">A bunch of stuff about item 1!</div>
    </div>
    <div class="item">
        <a href="#">Item2</a>
        <div class="desc">
            A bunch of stuff about item 2!<br/>More stuff for item 2!<br/>last item 2 thing
        </div>
    </div>
    <div class="item">
        <a href="#">Item3</a>
        <div class="desc">
            A bunch of stuff about item 3!<br/>More stuff for item 3
        </div>
    </div>
</div>

css: CSS:

.desc
{
 display:none;   

}

#container
{
    height:75px;
    overflow:hidden;
    border-style:solid;
    border-width:1px;
}

jQuery: jQuery的:

$(".item a").click(function () {
    $(this).siblings(".desc").animate({ height: 'toggle' });
            });

What I have tried so far: 到目前为止我尝试过的:

http://jsfiddle.net/Broham/LA22t/3/ http://jsfiddle.net/Broham/LA22t/3/

But this only moves the active div, what I need to do is shift all of the content up and hide the overflow... 但这只会移动活动的div,我需要做的是将所有内容上移并隐藏溢出...

You have two options. 你有两个选择。

You can either hid all of the div above it (thus making it appear at the top), or you can float the div over the top of the other elements, which sounds more like what you want. 您可以将其上方的所有div隐藏(从而使其显示在顶部),也可以将div浮动在其他元素的顶部,这听起来更像您想要的。 However, floating it to the top is difficult, and requires different methods in different browsers, so the easiest way for you is just to set all the other items to display:none, and then display:block again when you click to close the one you are viewing. 但是,将其浮动到顶部很困难,并且在不同的浏览器中需要使用不同的方法,因此,最简单的方法是将所有其他项设置为display:none,然后在单击以关闭该项目时再次显示:block您正在查看。

Here, this does what you want: 在这里,这就是您想要的:

$(".item a").click(function () {
    if(!this.opvar)this.opvar=false;
    $(this).siblings(".desc").animate({ height: 'toggle' });
    if(!this.opvar)$(this).parent().siblings(".item").css('display','none');
    else $(this).parent().siblings(".item").css('display','block');
    this.opvar = !this.opvar;
        });

Just a crude example though, there are better ways to do it. 不过,这只是一个粗略的例子,还有更好的方法。

this should do the trick, but you probably want to close the inactive headers also 这应该可以解决问题,但是您可能还想关闭不活动的标题

  $(".item a").click(function () {
       var a=  $(this).parent();
       $(this).parent().remove();
       $("#container > div.item").first().before(a);
       $('.removeMe').remove();
       $(this).siblings(".desc").animate({ height: 'toggle' });
  });

Wrap everything inside the container div in another div (eg wrapper ) and then animate the position (top) of this wrapper div to achieve desired result. container div内的所有内容都wrapper在另一个div中(例如wrapper ),然后对该wrapper div的位置(顶部)进行动画处理,以获得所需的结果。

HTML HTML

<div id="container">
  <div id="wrapper">
    <div class="item">
.........
</div>
</div>

CSS CSS

#wrapper{
    height:100%;
    position:relative;
}

JS JS

$(".item a").click(function () {
    $(this).siblings(".desc").animate({ height: 'toggle' });
    $("#wrapper").animate({top:-1*$(this).position().top},"slow");
 });

Something like this might help you getting started: http://jsfiddle.net/sethi/zfdmC/2/ 这样的事情可能会帮助您入门: http : //jsfiddle.net/sethi/zfdmC/2/

http://jsfiddle.net/LA22t/2/ http://jsfiddle.net/LA22t/2/

Just removed height . 刚拆下身高。

The trick is to actually move the elements in the DOM. 诀窍是实际移动DOM中的元素。 Use .prependTo("#container") : 使用.prependTo("#container")

http://jsfiddle.net/LA22t/7/ http://jsfiddle.net/LA22t/7/

$(".item a").click(function() {
    $(".desc").hide();
    $(this).parent().prependTo("#container").find(".desc").animate({height:"show"});
});

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

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