AlpineJS 加载更多动画

huangapple go评论64阅读模式
英文:

AlpineJS Load More Animation

问题

I am running into an issue where the load more animation is not getting fired when I have the index set to 10 and I increment by 10, however if I switch the index to 9 and the increment to 9 it runs just fine. On the backend there is a total of 20, so ideally, 10 start on the page, and you can load the additional ten or more by clicking the button. I am hoping someone can help me understand why ten doesn't work but 9 does:

<div x-data="{ show: false, index: 10 }">
<div class="grid grid-cols-2 md:grid-cols-2 lg:grid-cols-5 gap-4 md:gap-6 lg:gap-6 mt-4">
    @foreach($tributes as $tribute)
        <div class="gallery-border p-8" x-show="show && index > {{$loop->index}}" x-transition:enter="transition ease-out duration-500" x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100">
            <p class="text-2xl md:text-4xl lg:text-4xl">{{ $tribute->message }}</p>
            <p class="text-sm">-{{ $tribute->name }}</p>
        </div>
    @endforeach
</div>
@if(count($tributes) < $totalTributes)
    <div class="text-center mt-4">
        @if($totalTributes - count($tributes) < 10)
            <button x-on:click="show = true; index = index + {{$totalTributes - count($tributes)}}; $wire.loadMore()" x-init="setTimeout(() => { show = true }, 500)" class="tributes-gallery-load-more">Load More</button>
        @else
            <button x-on:click="show = true; index = index + 10; $wire.loadMore()" x-init="setTimeout(() => { show = true }, 500)" class="tributes-gallery-load-more">Load More</button>
        @endif
    </div>
@endif
</div>
namespace App\Http\Livewire;

use App\Models\Tribute;
use Livewire\Component;

class TributeGallery extends Component
{
    public $tributes, $totalTributes;
    public $perPage = 10;
    public $page = 1;

    public function loadMore()
    {
        $this->page++;
        $newTributes = Tribute::where('status', 'approved')
                              ->orderBy('id', 'desc')
                              ->skip(($this->page - 1) * $this->perPage)
                              ->take($this->perPage)
                              ->get();

        $this->tributes = $this->tributes->concat($newTributes);

    }

    public function mount()
    {
        $this->totalTributes = Tribute::where('status', 'approved')->count();
        $this->tributes = Tribute::where('status', 'approved')
                                  ->orderBy('id', 'desc')
                                  ->take($this->perPage)
                                  ->get();

    }

    public function render()
    {
        return view('livewire.tribute-gallery');
    }
}
英文:

I am running into an issue where the load more animation is not getting fired when I have the index set to 10 and I increment by 10, however if i switch the index to 9 and the increment to 9 it runs just fine. On the backend there is a total of 20 so Ideally 10 start on the page and you can load the additional ten or more by clicking the button. I am hoping someone can help me understand why ten doesnt work but 9 does:

&lt;div x-data=&quot;{ show: false, index: 10 }&quot;&gt;
&lt;div class=&quot;grid grid-cols-2 md:grid-cols-2 lg:grid-cols-5 gap-4 md:gap-6 lg:gap-6 mt-4&quot;&gt;
    @foreach($tributes as $tribute)
        &lt;div class=&quot;gallery-border p-8&quot; x-show=&quot;show &amp;&amp; index &gt; {{$loop-&gt;index}}&quot; x-transition:enter=&quot;transition ease-out duration-500&quot; x-transition:enter-start=&quot;opacity-0&quot; x-transition:enter-end=&quot;opacity-100&quot;&gt;
            &lt;p class=&quot;text-2xl md:text-4xl lg:text-4xl&quot;&gt;{{ $tribute-&gt;message }}&lt;/p&gt;
            &lt;p class=&quot;text-sm&quot;&gt;-{{ $tribute-&gt;name }}&lt;/p&gt;
        &lt;/div&gt;
    @endforeach
&lt;/div&gt;
@if(count($tributes) &lt; $totalTributes)
    &lt;div class=&quot;text-center mt-4&quot;&gt;
        @if($totalTributes - count($tributes) &lt; 10)
            &lt;button x-on:click=&quot;show = true; index = index + {{$totalTributes - count($tributes)}}; $wire.loadMore()&quot; x-init=&quot;setTimeout(() =&gt; { show = true }, 500)&quot; class=&quot;tributes-gallery-load-more&quot;&gt;Load More&lt;/button&gt;
        @else
            &lt;button x-on:click=&quot;show = true; index = index + 10; $wire.loadMore()&quot; x-init=&quot;setTimeout(() =&gt; { show = true }, 500)&quot; class=&quot;tributes-gallery-load-more&quot;&gt;Load More&lt;/button&gt;
        @endif
    &lt;/div&gt;
@endif

</div>

&lt;?php

namespace App\Http\Livewire;

use App\Models\Tribute;
use Livewire\Component;

class TributeGallery extends Component
{
    public $tributes, $totalTributes;
    public $perPage = 10;
    public $page = 1;

    public function loadMore()
    {
        $this-&gt;page++;
        $newTributes = Tribute::where(&#39;status&#39;, &#39;approved&#39;)
                              -&gt;orderBy(&#39;id&#39;, &#39;desc&#39;)
                              -&gt;skip(($this-&gt;page - 1) * $this-&gt;perPage)
                              -&gt;take($this-&gt;perPage)
                              -&gt;get();

        $this-&gt;tributes = $this-&gt;tributes-&gt;concat($newTributes);

    }

    public function mount()
    {
        $this-&gt;totalTributes = Tribute::where(&#39;status&#39;, &#39;approved&#39;)-&gt;count();
        $this-&gt;tributes = Tribute::where(&#39;status&#39;, &#39;approved&#39;)
                                  -&gt;orderBy(&#39;id&#39;, &#39;desc&#39;)
                                  -&gt;take($this-&gt;perPage)
                                  -&gt;get();

    }


    public function render()
    {

        return view(&#39;livewire.tribute-gallery&#39;);
    }
}

答案1

得分: 1

以下是代码部分的翻译:

第一页加载时延迟半秒,您将`show`切换为`true`。您从不将此变量切换回`false`,因此`x-show`条件的一半总是对后续加载满足。`index`从10开始,当单击“加载更多”按钮时,`index`变量首先增加了10,现在是20,`show`仍然是`true`。之后,您调用`$wire.loadMore()`来加载下一批。但是由于您已经增加了`index`变量,而`show`仍然一直是`true`,因此在Livewire加载下一批时,`x-show="show && index > {{$loop->index}}"`条件已经满足,因此Alpine.js不会触发过渡动画,因为过渡不会发生。

现在修复很简单:首先加载下一批,然后在稍后(可选带有小延迟)增加`index`变量,以便`x-show`条件可以从`false`更改为`true`,因此新项目的过渡也可以发生。`show`变量是多余的,可以删除。

```html
<div x-data="{ index: 0 }">
<div class="grid grid-cols-2 md:grid-cols-2 lg:grid-cols-5 gap-4 md:gap-6 lg:gap-6 mt-4">
    @foreach($tributes as $tribute)
        <div class="gallery-border p-8" x-show="index > {{$loop->index}}" x-transition:enter="transition ease-out duration-500" x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100">
            <p class="text-2xl md:text-4xl lg:text-4xl">{{ $tribute->message }}</p>
            <p class="text-sm">-{{ $tribute->name }}</p>
        </div>
    @endforeach
</div>
@if(count($tributes) < $totalTributes)
    <div class="text-center mt-4">
      <button x-on:click="$wire.loadMore(); setTimeout(() => { index = index + 10 }, 500)" class="tributes-gallery-load-more">Load More</button>
    </div>
@endif
</div>

这是您要求的代码部分的翻译。

英文:

On the first page load with a half second delay you switch show to true. You never switch back this variable to false so half of the x-show condition is always satisfied for the subsequent loads. The index starts from 10 and when you click on the Load More button first the index variable is increased by 10, so now its 20 and show is still true. After that you call $wire.loadMore() to load the next batch. However since you have already increased the index variable and again show remains true for the whole time, the x-show=&quot;show &amp;&amp; index &gt; {{$loop-&gt;index}}&quot; condition already satisfied when Livewire loads the next batch, so Alpine.js will not fire the transition animation since the transition will not occur.

The fix is now straightforward: load the next batch first and just after that increase the index variable (optionally with a small delay), so the x-show condition can change from false to true for the new items therefore the transition can occur as well. The show variable is redundant and can be removed.

&lt;div x-data=&quot;{ index: 0 }&quot;&gt;
&lt;div class=&quot;grid grid-cols-2 md:grid-cols-2 lg:grid-cols-5 gap-4 md:gap-6 lg:gap-6 mt-4&quot;&gt;
    @foreach($tributes as $tribute)
        &lt;div class=&quot;gallery-border p-8&quot; x-show=&quot;index &gt; {{$loop-&gt;index}}&quot; x-transition:enter=&quot;transition ease-out duration-500&quot; x-transition:enter-start=&quot;opacity-0&quot; x-transition:enter-end=&quot;opacity-100&quot;&gt;
            &lt;p class=&quot;text-2xl md:text-4xl lg:text-4xl&quot;&gt;{{ $tribute-&gt;message }}&lt;/p&gt;
            &lt;p class=&quot;text-sm&quot;&gt;-{{ $tribute-&gt;name }}&lt;/p&gt;
        &lt;/div&gt;
    @endforeach
&lt;/div&gt;
@if(count($tributes) &lt; $totalTributes)
    &lt;div class=&quot;text-center mt-4&quot;&gt;
      &lt;button x-on:click=&quot;$wire.loadMore(); setTimeout(() =&gt; { index = index + 10 }, 500)&quot; class=&quot;tributes-gallery-load-more&quot;&gt;Load More&lt;/button&gt;
    &lt;/div&gt;
@endif
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年3月23日 09:30:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定