动画显示/隐藏从CSS类
animate show/hide from css class
我是JavaScript和jQuery的新手,所以请对我温柔一点。我正在尝试动画显示/隐藏几个div的基础上,如果它有一个特定的类或不。
基本上,我正在为摄影师创建一个网站,并在顶部有一个带有过滤器列表的作品集部分,每个div都有一个"作品集项目"类以及所有类别的附加类,因此家庭/婚礼/孩子/夫妇。任何图像都可以有多个类。
我想做的是点击family链接它会隐藏任何没有family类的东西。如果我点击"婚礼",它会关闭当前打开的、没有婚礼类的页面,并打开当前关闭的、有婚礼类的页面。
我目前有它与下面的代码工作,但这只是关闭一切,然后打开那些有所需的类。而且我不知道怎么给它添加动画。
function portfolioItems(filter) {
$(".portfolio-items").hide();
$("."+filter).show(); }
function initEventHandlers () {
$(".port-all").click(function () {
$(".portfolio-items").show();
return false;
})
$(".port-wedding").click(function () {
portfolioItems("wedding");
return false;
})
$(".port-family").click(function () {
portfolioItems("family");
return false;
})
$(".port-kids").click(function () {
portfolioItems("kids");
return false;
})
$(".port-couples").click(function () {
portfolioItems("couples");
return false;
}) }
HTML是…
<div class="portfolio-container">
<div class="portfolio-links">
<a href="#"><img alt="All" class="port-all" src="images/port-all.png" /></a>
<a href="#"><img alt="family" class="port-family" src="images/port-family.png" /></a>
<a href="#"><img alt="wedding" class="port-wedding" src="images/port-wedding.png" /></a>
<a href="#"><img alt="couples" class="port-couples" src="images/port-couples.png" /></a>
<a href="#"><img alt="kids" class="port-kids" src="images/port-kids.png" /></a>
</div>
<div class="portfolio">
<div class="portfolio-items wedding couples family"></div>
<div class="portfolio-items kids"></div>
<div class="portfolio-items wedding kids family"></div>
<div class="portfolio-items couples"></div>
<div class="portfolio-items couples kids family"></div>
<div class="portfolio-items wedding"></div>
</div>
</div>
首先,你可以使用not选择器(看这里!)来避免隐藏所有照片。只需为带有照片的块分配两个类。像这样
<div class="portfolio-items wedding"></div>
<div class="portfolio-items family"></div>
<div class="portfolio-items kids"></div>
然后你可以这样重写portfolioItems
函数
function portfolioItems(filter) {
$(".portfolio-items:not(."+filter+")").hide();
}
其次,您可以创建一个通用函数来隐藏某些类别,但不要多次重复相同的代码。
你可以试试:
function portfolioItems(filter) {
$(".portfolio-items.not("+filter+")").fadeOut();
$("."+filter).fadeIn();
}
这是我发现的一种通过CSS进行过滤的有用方法。我喜欢在链接上使用data属性来指定过滤器。首先,设置一个带有一些链接的导航和一个带有一些图像或div的作品集:
<!-- set up some navigation -->
<nav>
<a href="#" class="filter">All Photos</a>
<a href="#" class="filter" data-filter=".family">Family Photos</a>
<a href="#" class="filter" data-filter=".art">Art Photos</a>
<a href="#" class="filter" data-filter=".wombats">Wombat Photos</a>
</nav>
<!-- set up a portfolio -->
<div class="portfolio">
<div class="family item">Some family image or something</div>
<div class="art item"> Some art image or something</div>
<div class="wombats item">Some wombat image or something</div>
<div class="wombats item">Some wombat image or something</div>
<div class="art item"> Some art image or something</div>
</div>
注意每个a标记都有您想要用作过滤器的类名作为数据过滤器属性。你可以在这里指定多个类,它会一样工作。例如"袋熊"。family"会让你在作品集中使用DOUBLE过滤器。
下面的脚本将帮助您设置过滤器:
//on document ready
$(document).ready(function(){
//when you click <a> tag in the <nav>
$("nav a").click(function(e){
//if the <a> has a data-filter attribute
if($(this).attr("data-filter")){
//show all the .items with the class in the data-filter attribute
$(".portfolio .item"+$(this).attr("data-filter")).show(300);
//hide all the .items that do not have that class
$(".portfolio .item:not("+$(this).attr("data-filter")+")").hide(300);
}else{
//if there's no data-filter attribute, show all the images
$(".portfolio .item").show(300);
}
});
});
对于这个示例,我只是在show()和hide()函数中使用时间,但是fadeIn() fadeOut()可能也适用于您。
为了启用"all"过滤器,我只是没有为特定的a标签编写数据过滤器属性,并确保JS知道该做什么(检查if/else)。
要记住的重要事情是在投资组合项目上使用的类和数据过滤器属性之间的链接。一开始很简单,不过我相信在你完成之前它会变得有点复杂:)
这里有一个jsfiddle来玩:http://jsfiddle.net/w4VWm/
祝你好运!
隐藏所有,将新的类名添加到过滤器字符串中,然后通过过滤器字符串
显示http://jsfiddle.net/uhCY5/3/var filters = "";
function portfolioItems(filter) {
filters += '.' + filter
$(".portfolio-items").hide();
$(filters).show();
$("#filter").text(filters)
}
function initEventHandlers() {
$(".port-all").click(function () {
filters = "";
$(".portfolio-items").show();
return false;
})
// the rest is the same
}
不确定你想要什么样的过渡,但这将实现淡入/淡出只用很少的jquery:
请注意,你也许可以删除div中的一些东西,但我不知道你需要在页面上的其他东西
小提琴:http://jsfiddle.net/Z5uXP/
<div class="portfolio-container">
<div class="portfolio-links">
<a href="#" data-type="all"><img alt="All" class="port-all" src="images/port-all.png" /></a>
<a href="#" data-type="family"><img alt="family" class="port-family" src="images/port-family.png" /></a>
<a href="#" data-type="wedding"><img alt="wedding" class="port-wedding" src="images/port-wedding.png" /></a>
<a href="#" data-type="couples"><img alt="couples" class="port-couples" src="images/port-couples.png" /></a>
<a href="#" data-type="kids"><img alt="kids" class="port-kids" src="images/port-kids.png" /></a>
</div>
<div class="portfolio">
<div class="portfolio-items wedding couples family"></div>
<div class="portfolio-items kids"></div>
<div class="portfolio-items wedding kids family"></div>
<div class="portfolio-items couples"></div>
<div class="portfolio-items couples kids family"></div>
<div class="portfolio-items wedding"></div>
</div>
</div>
<script>
$(document).ready(function(){
var $container = $("div.portfolio-container"),
$portfolio = $container.find("div.portfolio");
$container
.on("click", ".portfolio-links a", function(event){
var $obj = $(this);
event.preventDefault();
$portfolio
.fadeOut()
.queue(function(next){
$($(this)[0]).css("color", "red")
.removeClass("family wedding couples kids")
.addClass($($obj[0]).data("type"));
next();
})
.fadeIn();
});
});
</script>
<style>
.portfolio .portfolio-items{
display: none;
}
.portfolio.all .portfolio-items,
.portfolio.family .portfolio-items.family,
.portfolio.wedding .portfolio-items.wedding,
.portfolio.couples .portfolio-items.couples,
.portfolio.kids .portfolio-items.kids{
display: block;
}
</style>
- 渐进式增强-不使用CSS隐藏元素
- CSS Html使图像显示在单选按钮的选项上(单选按钮被CSS隐藏)
- 使用 css 隐藏 vs JS 创建元素 vs ajax
- 使用 jQuery 和 CSS 隐藏/显示选定的表单
- 如何使用 jquery 通过 CSS 隐藏来选择 col id
- 通过CSS隐藏元素的可能方法是什么
- 根据使用Javascript选择的单选按钮,使以前被CSS隐藏的图像可见
- Javascript;CSS:隐藏滚动条,保持滚动事件工作
- 使用CSS隐藏/显示块
- 用CSS隐藏元素,点击后用Javascript显示
- 通过CSS隐藏特定大小的图像
- 使用CSS隐藏表行,如果其中一列为空
- 使用hide、prop、attr或css隐藏JQuery元素之间的区别
- 什么'更好吗?使用css隐藏或使用jquery添加
- CSS隐藏光标而不触发JavaScript鼠标移动事件
- 使用CSS隐藏特定的tr与特定的类名只有当它包含关键字
- MouseEvent吞下了css隐藏的元素
- CSS隐藏文本非法搜索引擎
- 如何用CSS隐藏输入框
- 使用css隐藏对象