英文:
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:
<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>
<?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->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');
}
}
答案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="show && index > {{$loop->index}}"
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.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论