日期:2011/11/01 来源:GBin1.com
因为本人是星际争霸系列游戏的忠实拥簇,所以在今天的jQuery教程中,我们将使用HTML5和jQuery插件Quicksand来创建一个超酷的星际争霸兵种效果图。希望大家喜欢!
如果你不熟悉jQuery插件Quicksand,那么推荐你查看这篇文章:2010年最佳jQuery插件 ,或者去Quicksand插件 查看具体介绍。

首先我们使用HTML5的代码来创建一个html文档,将所需的quicksand类库,及其jquery类库,还有HTML5类库倒入,如下:
Starcraft 2 Unit Show Demo
<!-- Our CSS stylesheet file -->
<!--[if IE]>
Starcraft 2 Unit
//所有的兵种图片显示在这里
在以上代码中,我们将在container中插入需要展示的兵种图片,如下:








以上代码中,我们定义了兵种类别,分别为Terran,Protoss和Zerg单位。
在HTML5中,我们可以在data属性中存储数据,然后在jQuery中直接使用data方法调用取出,以上代码中我们将通过data-tag中定义的类别来展示所有兵种。
gbin1.js
生成兵种的分类导航,如下:
$(document).ready(function(){
var items = $('#starcraft li'), itemsByTag = {};
items.each(function(i){
var elem = $(this);
//使用jQuery的html5数据处理方法取得兵种分类
var tag = elem.data('tag');
elem.attr('data-id',i);
//去空格
tag = $.trim(tag);
if(!(tag in itemsByTag)){
itemsByTag[tag] = [];
}
//添加到分类中
itemsByTag[tag].push(elem);
});
...
...
});
创建实际显示的兵种内容,如下:
function createList(text,items){
var ul = $('生成导航栏点击动作,并生成放大效果。
//使用live方法来给动态生成内容添加事件
$('li').live('click', function(e){
if($('#details').is(":visible")){
$('#details').hide();
}
var src = $(this).find('img').attr('src');
$('#details').html($('',{
src: src,
width: '150px',
height: '150px'
}));
var details = $('#details');
var ffset = $(this).offset();
$('#details').css({"left":offset.left-32, "top":offset.top-32}).show(function() {
$('#details img').animate({
width: '150px',
height: '150px',
}, 800);
});
});
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8288/viewspace-710067/ ,如需转载,请注明出处,否则将追究法律责任。