我有浮动元素网格,我为此创建了一个过滤器。我想要这个过滤器隐藏和显示项目,同时这样做我希望项目重新调整过渡和淡出..目前项目只是跳来跳去。
正如你在这里看到的那样:
如何让物品滑到他们的位置?
下面是代码:
HTML
<ul class="filters">
<li data="1">filter 1</li>
<li data="2">filter 2</li>
<li data="3">filter 3</li>
<li data="4">filter 4</li>
</ul>
<br>
<ul class="items">
<li data="1">1</li>
<li data="2">2</li>
<li data="1">3</li>
<li data="3">4</li>
<li data="4">5</li>
<li data="2">6</li>
<li data="3">7</li>
<li data="4">8</li>
</ul>
CSS
ul {
list-style: none;
padding: 0;
}
.filters li {
float:left;
margin-right: 10px;
cursor: pointer;
}
.items li {
width: 100px;
height: 100px;
background: #ccc;
float: left;
margin: 20px;
}
JQUERY
$(document).ready( function() {
$('.filters li').click( function () {
var data;
data = $(this).attr('data');
$('.items li').each( function() {
if($(this).attr('data') !== data) {
$(this).fadeOut();
}
else {
$(this).fadeIn();
}
});
});
});
答案 0 :(得分:2)
正如我在评论中提到的那样,您需要一个与fadeIn/Out
不同的操作,因为它涉及到display属性,并且无法设置动画或转换。
动画非display
值似乎更合理。
这样的事情:
$(document).ready(function() {
$('.filters li').click(function() {
var data;
data = $(this).attr('data');
$('.items li').each(function() {
if ($(this).attr('data') !== data) {
$(this).addClass('hidden');
} else {
$(this).removeClass('hidden');
}
});
});
});
&#13;
ul {
list-style: none;
padding: 0;
}
.filters li {
float: left;
margin-right: 10px;
cursor: pointer;
}
.items li {
width: 100px;
height: 100px;
background: #ccc;
float: left;
margin: 20px;
transition: all .5s ease;
}
li.hidden {
height: 0;
width: 0;
margin: 0;
opacity: 0;
font-size: 0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="filters">
<li data="1">filter 1</li>
<li data="2">filter 2</li>
<li data="3">filter 3</li>
<li data="4">filter 4</li>
</ul>
<br>
<ul class="items">
<li data="1">1</li>
<li data="2">2</li>
<li data="1">3</li>
<li data="3">4</li>
<li data="4">5</li>
<li data="2">6</li>
<li data="3">7</li>
<li data="4">8</li>
</ul>
&#13;
答案 1 :(得分:1)
您可以设置淡入的延迟以确保淡出已完成。
下面500ms的延迟是稍微长一点的淡入。
$(document).ready(function() {
$('.filters li').click(function() {
var data;
data = $(this).attr('data');
$('.items li').each(function() {
if ($(this).attr('data') !== data) {
$(this).fadeOut();
} else {
$(this).delay(525).fadeIn();
}
});
});
});
ul {
list-style: none;
padding: 0;
}
.filters li {
float: left;
margin-right: 10px;
cursor: pointer;
}
.items li {
width: 100px;
height: 100px;
background: #ccc;
float: left;
margin: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="filters">
<li data="1">filter 1</li>
<li data="2">filter 2</li>
<li data="3">filter 3</li>
<li data="4">filter 4</li>
</ul>
<br>
<ul class="items">
<li data="1">1</li>
<li data="2">2</li>
<li data="1">3</li>
<li data="3">4</li>
<li data="4">5</li>
<li data="2">6</li>
<li data="3">7</li>
<li data="4">8</li>
</ul>
答案 2 :(得分:1)
fadeOut和fadeIn的重叠让它看起来像是在跳跃。
尝试快速隐藏已过滤的项目并向下滑动适用的框:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Python]
[HKEY_CURRENT_USER\SOFTWARE\Python\PyLauncher]
"InstallDir"="C:\\Windows\\"
"AssociateFiles"=dword:00000001
@="C:\\Windows\\py.exe"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore]
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5]
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\Help]
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\Help\Main Python Documentation]
@="C:\\Program Files (x86)\\Python 3.5\\Doc\\python350.chm"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\Idle]
@="C:\\Program Files (x86)\\Python 3.5\\Lib\\idlelib\\idle.pyw"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\IdleShortcuts]
@=dword:00000001
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\InstalledFeatures]
"dev"="3.5.150.0"
"Shortcuts"="3.5.150.0"
"exe"="3.5.150.0"
"lib"="3.5.150.0"
"test"="3.5.150.0"
"doc_shortcut"="3.5.150.0"
"doc"="3.5.150.0"
"tools"="3.5.150.0"
"tcltk"="3.5.150.0"
"pip"="3.5.150.0"
"path"="3.5.150.0"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\InstallPath]
@="C:\\Program Files (x86)\\Python 3.5\\"
"ExecutablePath"="C:\\Program Files (x86)\\Python 3.5\\python.exe"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\InstallPath\InstallGroup]
@="Python 3.5"
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.5\PythonPath]
@="C:\\Program Files (x86)\\Python 3.5\\Lib\\;C:\\Program Files (x86)\\Python 3.5\\DLLs\\"
});