我有这个代码使用Jquery Json成功从Behance中提取图像:
$('a.link').live('click', function () {
h = window.location.hash.replace('#', '');
$.getJSON("http://www.behance.net/v2/projects/" + h + "?api_key=" + api + "&callback=?", function (data) {
var project_data = "";
for (var i = 0; i < data.project.modules.length; i++) {
if (data.project.modules[i].type == "image") {
project_data += '<img src="' + data.project.modules[i].src + '" />';
}
if (data.project.modules[i].type == "text") {
project_data += '<p>' + data.project.modules[i].text + '</p>';
}
};
$('.portfolio-details h3').html(data.project.name);
$('.portfolio-details div').html(project_data);
$('.portfolio-details').fadeIn(6000);
});
});
他们正在拉入页面,目前显示为单独的图像。但是,如何在此代码中将所有图像包装在一起以进行幻灯片放映?每个图像文本代表图像需要添加的位置
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<div class="item"> each image </div>
<div class="item"> each image </div>
</div>
</div>
答案 0 :(得分:0)
我在你的HTML中没有看到.portfolio-details
,所以我认为它是.carousel-inner
:
for (var i = 0; i < data.project.modules.length; i++) {
...
$(".portfolio-details div").eq(i).html(project_data);
}
答案 1 :(得分:0)
为什么不能将var project_data = "";
变量所需的html前置/附加?
var project_data = '<div id="myCarousel" class="carousel slide"><div class="carousel-inner">';
// Loop goes here...
project_data += '<div class="item"><img src="' + data.project.modules[i].src + '"> </div>';
// more code here
// end of loop
project_data += '</div></div>';
修改强> 这是一种解决这个问题的简单方法。
(function($) {
var user = 'userNameGoesHere',
api = 'apiKeyGoesHere',
project_str = "",
i = 0;
var project_images, carousel;
for(i; i <= 3; i += 1) {
$.getJSON("http://www.behance.net/v2/users/" + user + "/projects?api_key=" + api + "&callback=?&page=" + i, function (data) {
if ($.type(data.projects) === 'array') {
$.each(data.projects, function(i, obj) {
project_str += '<a class="tips more-info link" data-placement="bottom" href="#' + obj.id + '">';
project_str += '<div class="span3 portfolioitem">';
project_str += '<img src="' + obj.covers['404'] + '">';
project_str += '<div class="portfolioitem-hoverinfo"> <h3>' + obj.name + '</h3> </div>';
project_str += '</div>';
project_str += '</a>';
});
}
$('#portfolio-items').append(project_str);
});
}
$('a.link').live('click', function () {
var h = window.location.hash.replace('#', '');
$.getJSON("http://www.behance.net/v2/projects/" + h + "?api_key=" + api + "&callback=?", function (data) {
var project_data = "";
// "data.project.modules" here throws an error on the first click... not sure why
for (var i = 0; i < data.project.modules.length; i++) {
if (data.project.modules[i].type == "image") {
project_data += '<img src="' + data.project.modules[i].src + '">';
}
if (data.project.modules[i].type == "text") {
project_data += '<p>' + data.project.modules[i].text + '</p>';
}
}
$('.portfolio-details h3').html(data.project.name);
$('.portfolio-details div').html(project_data);
$('.portfolio-details').fadeIn(6000);
project_images = $('.portfolio-details').find('img');
carousel = '<div id="myCarousel" class="carousel slide"> <div class="carousel-inner">';
$.each(project_images, function(i, img) {
carousel += '<div class="item"> <img src="' + img.src + '"> </div>';
});
carousel += "</div></div>";
// Do something with the carousel - append somewhere or something
$(document.body).prepend(carousel);
});
});
}(jQuery));