Jen*_*ieb 3 html javascript jquery position css-float
我有许多行中浮动的div。div包含文本预览和向下滑动全部内容的链接(有关示例,请参见http://jsfiddle.net/yDcKu/)。
现在发生的情况:向下滑动content-div时,它将在连接的预览之后立即打开。我要发生的事情:在该行的最后一个div之后打开content-div。
我认为可以通过以下方式完成此操作:
1.找出哪个div是激活的预览行中的最后一个div;
2。向该div添加一个id;
3。将content-div附加到该div。
我有一个使用jQuery的第2步和第3步的解决方案,但不知道如何进行第一步。我可以设法获取每个div的文档宽度以及x和y值,但我不知道如何找出哪个div具有最高的x和y值,并且也位于激活的预览分区。
有人知道吗?谢谢
这是一个可以满足您需求的示例。我简化了代码,因此您不必手动标识每个条目和预览。
这有点复杂。如果您有任何问题,请告诉我。
基本上,当调整窗口大小时,脚本将通过查找与左偏移量相同的预览来查找每行中的第一个预览。然后,将一个类添加last到该条目的前面(上一行),并将类first添加到此预览。我clear: left对这两种方法都进行了css 处理,以便在打开条目时一切都能正常包装。
我使您的代码通用,没有ID:
<div class="preview">
<p>Some preview text <a class="trigger" href="#">…</a></p>
</div>
<div class="entry">
<div class="close_button">
<a class="close" href="#">×</a>
</div>
<p>Some content text.</p>
</div>
Run Code Online (Sandbox Code Playgroud)
这使您不必一遍又一遍地编写相同的代码。
打开/关闭脚本:
$('.trigger').click(function() {
$('.openEntry').slideUp(800); // Close the open entry
var preview = $(this).closest('.preview'); // Grab the parent of the link
// Now, clone the entry and stick it after the "last" item on this row:
preview.next('.entry').clone().addClass('openEntry').insertAfter(preview.nextAll('.last:first')).slideDown(800);
});
// Use "on()" here, because the "openEntry" is dynamically added
// (and it's good practice anyway)
$('body').on('click', '.close', function() {
// Close and remove the cloned entry
$('.openEntry').slideUp(800).remove();
});
Run Code Online (Sandbox Code Playgroud)
我敢肯定,这可以简化一点,特别是如果您愿意通过将条目放置在Preview元素内(但仍处于隐藏状态)来重新格式化html。这是一个稍微简单的版本,重新排列了html:
(我还为行的第一个和最后一个元素上色,以便您看到正在发生的事情)