Как организовать fadeOut() и fadeIn() без моргания?
Всем привет. Делаю фильтр на страницу. Сделал. Работает. Не нравится анимация. Подскажите, как можно сделать изменение видимости объектов без "моргания"?
Описание проблемы: например если у меня есть 3 объекта , один с фильтром "f1"
, другой с фильтром "f1,f2"
, третий с фильтром "f2"
. Я выбираю селектом фильтр "f2"
, скрываются все и появляется блоки (2,3). Если после этого я выбираю фильтр "Все", то блок 1 появляется, в два других моргают.
Что я сделал:
$('#filter_select').on('change', function () {
let val_filter = $(this).val();
if(val_filter==0){
$('.box').fadeIn();
}else{
$('.box').fadeOut(function(){
$('.box').each(function(index, val){
if($(val).data('filtertag').indexOf(val_filter)>=0){
$(this).fadeIn();
}
})
})
}
})
UPD. Ну вот пример, просили в комментариях.
$('#filter_select').on('change', function () {
let val_filter = $(this).val();
if(val_filter==0){
$('.box').fadeIn();
}else{
$('.box').fadeOut(function(){
$('.box').each(function(index, val){
if($(val).data('filtertag').indexOf(val_filter)>=0){
$(this).fadeIn();
}
})
})
}
})
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="container">
<select class="form-select my-3" id="filter_select">
<option value="0" selected>Все фильтры</option>
<option value="f1">F1</option>
<option value="f2">F2</option>
<option value="f3">F3</option>
</select>
<div class="row">
<div class="col-lg-2 box" data-filtertag="f1,f2">
<div class="card">
<img src="https://jjji.ru/300x300" class="card-img-top">
<div class="card-body">
<h5 class="card-title">Card title F1-F2</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
<div class="col-lg-2 box" data-filtertag="f3">
<div class="card">
<img src="https://jjji.ru/300x300" class="card-img-top">
<div class="card-body">
<h5 class="card-title">Card title F3</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
<div class="col-lg-2 box" data-filtertag="f1">
<div class="card">
<img src="https://jjji.ru/300x300" class="card-img-top">
<div class="card-body">
<h5 class="card-title">Card title F1</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
<div class="col-lg-2 box" data-filtertag="f2">
<div class="card">
<img src="https://jjji.ru/300x300" class="card-img-top">
<div class="card-body">
<h5 class="card-title">Card title F2</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
</div>
</div>