在 div 内移动内容?
更新:
父 div (#container) 必须具有固定的高度。我只是想移动其中的内容,以便活动 div 位于顶部,并且所有溢出都被隐藏。
我正在开发一个网页,其中父 div 具有固定大小,并且其中有多个子 div。这些子 div 中的每一个都有一个始终可见的锚标记,并且它自己的子 div 仅在单击其同级锚标记时才会显示。
我有这么多的工作,但我遇到了一个问题,当显示同级 div 时,它有时会超出父 div 并且它的内容被隐藏。我想将此 div 的内容重新定位到父 div 的顶部,以便它的内容永远不会隐藏。我如何使用 jQuery 来完成这个任务?
您可以通过单击此小提琴中的 Item2 来查看我在说什么: http://jsfiddle.net/ Broham/LA22t/1/
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:
.desc
{
display:none;
}
#container
{
height:75px;
overflow:hidden;
border-style:solid;
border-width:1px;
}
jQuery:
$(".item a").click(function () {
$(this).siblings(".desc").animate({ height: 'toggle' });
});
到目前为止我尝试过的:
http://jsfiddle.net/Broham/LA22t/3/
但这只会移动活动的div,我需要做的是将所有内容向上移动并隐藏溢出.. 。
UPDATE:
The parent div (#container) must have a fixed height. I jsut want to shift the content in it so the active div is at the top and all overflow is hidden.
I am working on a webpage where a parent div has a fix size and will have several child divs in it. 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.
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. 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. How would I accomplish this using jQuery?
You can see what I'm talking about by clicking Item2 in this Fiddle: http://jsfiddle.net/Broham/LA22t/1/
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:
.desc
{
display:none;
}
#container
{
height:75px;
overflow:hidden;
border-style:solid;
border-width:1px;
}
jQuery:
$(".item a").click(function () {
$(this).siblings(".desc").animate({ height: 'toggle' });
});
What I have tried so far:
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你有两个选择。
您可以隐藏其上方的所有 div(从而使其显示在顶部),也可以将 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. 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.
Here, this does what you want:
Just a crude example though, there are better ways to do it.
将
container
div 内的所有内容包装在另一个 div 中(例如wrapper
),然后为该wrapper
div 的位置(顶部)设置动画以实现所需的结果。HTML
CSS
JS
这样的内容可能会帮助您入门:
http://jsfiddle.net/sethi/zfdmC/2/
Wrap everything inside the
container
div in another div (e.g.wrapper
) and then animate the position (top) of thiswrapper
div to achieve desired result.HTML
CSS
JS
Something like this might help you getting started:
http://jsfiddle.net/sethi/zfdmC/2/
这应该可以解决问题,
但您可能还想关闭非活动标头
this should do the trick,
but you probably want to close the inactive headers also
http://jsfiddle.net/LA22t/2/
刚刚删除了 height 。
http://jsfiddle.net/LA22t/2/
Just removed height .
诀窍是实际移动 DOM 中的元素。使用
.prependTo("#container")
:http://jsfiddle.net/LA22t /7/
The trick is to actually move the elements in the DOM. Use
.prependTo("#container")
:http://jsfiddle.net/LA22t/7/