Jquery Fadeout fadein 与 li 元素问题

发布于 2024-08-12 05:41:31 字数 1247 浏览 4 评论 0原文

我有以下无序列表:

<ul id="#lastcompanieslist">
<li style="display: none;" page="0">whatever 1</li>
<li style="display: none;" page="0">whatever 2</li>
<li style="display: none;" page="0">whatever 2</li>
<li style="display: none;" page="0">whatever 3</li>
<li style="display: none;" page="0">whatever 4</li>
<li style="display: none;" page="1">whatever 5</li>
<li style="display: none;" page="1">whatever 6</li>
<li style="display: none;" page="1">whatever 7</li>
<li style="display: none;" page="1">whatever 8</li>
</ul>
<p class="NextPrevious">
<img src="/Images/PreviousItem.jpg" id="previousbutton" /> 
<img src="/Images/NextItem.jpg" id="nextbutton" />
</p>

我有一个计数器“实际页面是变量名称”,另一只手是当前页面。

使用 Jquery,当用户单击这两个按钮时,我尝试淡出和淡入 li 元素,具体取决于实际页面的计数器。

我用于淡出和淡入的线条如下:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500);
$('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500);

效果正在工作,但我有闪烁效果,项目开始淡出,但暂时列表的高度空间更大。淡入完成后,列表将返回到原始高度大小。

可能是什么问题,或者我该如何解决它?

提前致谢。 亲切的问候。 何塞马.

i have the following unordered list:

<ul id="#lastcompanieslist">
<li style="display: none;" page="0">whatever 1</li>
<li style="display: none;" page="0">whatever 2</li>
<li style="display: none;" page="0">whatever 2</li>
<li style="display: none;" page="0">whatever 3</li>
<li style="display: none;" page="0">whatever 4</li>
<li style="display: none;" page="1">whatever 5</li>
<li style="display: none;" page="1">whatever 6</li>
<li style="display: none;" page="1">whatever 7</li>
<li style="display: none;" page="1">whatever 8</li>
</ul>
<p class="NextPrevious">
<img src="/Images/PreviousItem.jpg" id="previousbutton" /> 
<img src="/Images/NextItem.jpg" id="nextbutton" />
</p>

I have a counter "actualpage is the variable name" with the current page for other hand.

With Jquery im trying to fadeout and fadein li elements when the user clicks this two buttons and depending of the counter of actual page.

The lines that im using for fadeout and fadein are the following:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500);
$('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500);

The effect is working, but im having a flickering effect, the items starts to fadeout, but for a moment the list is bigger in height space. When the fadein is complete, the list returns to the original height size.

What could be the problem, or how could i solve it?

Thanks in advance.
Kind Regards.
Josema.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉殇 2024-08-19 05:41:31

在我看来,你的问题非常简单。您正在创建一个列表,通常,此列表会列出您的项目,例如

  • 项目 1
  • 项目 2
  • 项目 3

等,假设上面的列表的高度为 3。现在,如果我要淡出项目 1 和 2,它们会' d 淡出 500 毫秒,最终消失。此时,jquery 将显示从 block 更改为 none,导致浏览器从视图中删除该元素,并且列表的高度为 1。

现在,如果第 3 项一开始是不可见的,我有一个高度为2的列表,项目1和2开始淡出(它们还没有消失),我开始淡入项目3,jquery将项目3的显示更改为'block' 给我一个高度为 3 的列表,同时褪色(直到 1 和 2 完成褪色并被删除)。

您可以做的是在完成淡出后开始淡入,如下所示:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500,
    function() {
       $('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500);
    }
);

当淡出完成时,将调用作为第二个参数传递给 fad​​eOut 的函数。在淡出之后和淡入之前,您仍然可能会看到短暂的闪烁,在此期间列表的高度为零。

另一种方法是有两个单独的列表,使用绝对定位放置以占据网页上的相同空间,并同时淡出和淡入它们,但这需要更多的工作。

It seems to me that your problem is pretty much straightforward. You're creating a list, normally, this list your items like this

  • item 1
  • item 2
  • item 3

etc, let's say the list above to has a height of 3. Now, if I were to fade out item 1 and 2, they'd fade out for 500 milliseconds, and eventually be invisible. At that point jquery changes the display from block to none, causing your browser to remove the element from view, and your list has a height of 1.

Now, if item 3 was invisible at the beginning, I'd have a list of height 2, item 1 and 2 starts to fade out (they're not gone yet), and I start to fade in item 3, jquery changes the display of item 3 to 'block' giving me a list of height 3, while fading (until 1 and 2 are finished fading, and are removed).

What you can do is start your fade-in after you've finished your fade out, like this:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500,
    function() {
       $('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500);
    }
);

The function which is passed as a second parameter to fadeOut will be called when the fading is complete. You'll still probably see a brief flicker after the fadeOut and before the fadeIn, during which the list will have a height of zero.

A different approach would be to have two separate lists, placed using absolute positioning to occupy the same space on the web page, and fade them out and in simultaneously, but that's a little bit more work.

浴红衣 2024-08-19 05:41:31

您的问题是您的淡入没有等待淡出完成。这使得列表与两者的总和一样大,直到淡出完全消失为止。

假设所需的效果是让它完全淡出,那么正确的淡入效果是您所追求的:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500, function() {
$('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500); });

您可能还需要考虑在 ul 上设置 CSS 最小高度,以最大程度地减少其减少为 0 个元素的影响页面其余部分的一瞬间,或者如果您真的想发疯,您可以首先设置当前高度的固定高度,淡出其现有内容,使用 动画,然后淡入新列表。这将提供最平稳的过渡。

(如果这不是您想要的,请澄清预期结果,我会再试一次=))

Your problem is that your fade-in is not waiting for your fade-out to finish. This is making the list as big as both combined until such time as the fade out is completely gone.

Assuming the desired effect is for it to fade out entirely then the correct ones to fade in you're after:

$('#lastcompanieslist li[page!=' + actualpage.toString() + ']').fadeOut(500, function() {
$('#lastcompanieslist li[page=' + actualpage.toString() + ']').fadeIn(500); });

You may also want to consider setting a CSS min-height on your ul to minimize the impact of it being reduced to 0 elements for a split second on the rest of the page, or if you really want to go nuts you could start by setting a fixed height of it's current height, fade out it's existing content, animate it to the desired height using Animate and then fade in your new list. This would give the smoothest transition.

(If that's not what you're after, clarify the intended result and I'll give it another shot =))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文