动态循环JSON,并在设置间隔后显示和隐藏对象元素
Dynamically loop JSON and show and hide object elements after set interval
JavaScript/jQuery新手在这里。。。我正在努力循环使用JSON文件,并使用jQuery成功地呈现顶级项。这里的目的是显示单个元素,然后在显示连续的元素之前将其淡出。但是,我的脚本只呈现最后一个元素。你知道我在这里做错了什么吗?
JSON文件-x.JSON
{"questions":[
{
"q":"Question1...?",
"a": [
"Answer 1a",
"Answer 1b",
"Answer 1c",
"Answer 1d"
]
},
{
"q":"Question2...?",
"a": [
"Answer 2a",
"Answer 2b",
"Answer 2c",
"Answer 2d"
]
}
]}
JavaScript文件-x.js
$(document).ready( function () {
$.getJSON('x.json', function (jsondata) {
// compute total number of questions in JSON file
var totalQuestions = jsondata.questions.length;
$.each(jsondata.questions, function (i) {
var questionNumber = i + 1; // add one since indicies start at 0
var questionContent = jsondata.questions[i]["q"];
var answerContent = '';
// generate questions progress HTML text
var questionHTML = '<div class="questionCount">Question <span class="current">' + questionNumber +'</span> of <span class="total">' + totalQuestions + '</span>';
// generate question HTML text
questionHTML += '</div><h3>' + questionNumber + '. ' + questionContent + '</h3>';
console.log(JSON.stringify(jsondata.questions[i]["q"]));
var answersHTML = '<ul type="A" class="answerswers">';
$.each(jsondata.questions[i]["a"], function (k) {
answerContent = jsondata.questions[i]["a"][k];
answersHTML += '<li><input type="radio"><label>' + answerContent + '</label></li>';
});
answersHTML += '</ul></li>';
questionHTML += answersHTML;
console.log(questionHTML);
$("#placeholder").html(questionHTML);
setInterval(function () {
$("#placeholder").html(questionHTML);
$("#placeholder").fadeIn(6000).delay(3000).fadeOut(1000);
}, 5000);
});
});
});
HTML文件-x.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JSON Sample</title>
</head>
<body>
<div id="placeholder"></div>
<script src="js/jquery.js"></script>
<script src="x.js"></script>
</body>
</html>
首先我假设,您希望将所有问题放在同一位置,对吗?添加一些CSS如下:
#placeholder {
position: relative;
}
#placeholder .question {
position: absolute;
}
我把你的问题和相应的答案封装在.question
类的DIV中。将jQuery代码更改为以下代码:
[...]
// generate questions progress HTML text
var questionHTML = '<div class="question" style="display:none;">
<div class="questionCount">Question <span class="current">' + questionNumber + '</span> of <span class="total">' + totalQuestions + '</span>';
[...]
answersHTML += '</ul></li>
</div>';
[...]
console.log(questionHTML);
$("#placeholder").html(questionHTML);
}); // end foreach
}); // end .getJSON callback
animate();
}); // end $(document).ready callback
function animate(current) {
var count = $('#placeholder .question').length;
if (isNaN(current)) { current = 0; }
console.log(current);
animateOne($('#placeholder .question').get(current), function() {
animate(++current % count);
});
};
function animateOne(el, callback) {
$(el).fadeIn(6000).delay(3000).fadeOut(1000, function() {
callback();
});
};
试试JSFiddle:http://jsfiddle.net/97buosve/7/
替代动画
你可以使用这个替代函数,在淡出时同时看到两个问题。我只是将回调从fadeOut
移回fadeIn
。。。
function animateOne(el, callback) {
$(el).fadeIn(6000, function() {
callback();
}).delay(3000).fadeOut(1000);
};
试试JSFiddle:http://jsfiddle.net/97buosve/8/
没有循环的替代动画
function animate(current) {
var count = $('#placeholder .question').length;
if (isNaN(current)) { current = 0; }
console.log(current);
animateOne(
$('#placeholder .question').get(current), // question to animate
(current +1 < count), // hide it after fadeIn, only if it isn't the last question
function() { // callback to animate the next question
if (current +1 < count) { // prevent loop, only set callback if it isn't the last question
animate(++current % count);
}
}
);
};
function animateOne(el, hideItAfter, callback) {
$(el).fadeIn(6000, function() {
callback();
});
if (hideItAfter) {
$(el).delay(3000).fadeOut(1000);
}
}
http://jsfiddle.net/97buosve/9/
两个错误:
1) setInterval不是一个"暂停"函数。它会立即返回,因此您的循环以毫秒为单位闪烁(因此您只能看到最后一次重复)。
2) 你没有设置一个闭包。查看这个常见的问题,关于如何在循环内的闭包中传递变量:循环内的JavaScript闭包——一个简单的实用示例
相关文章:
- 在jquery中为显示/隐藏设置cookie
- PHP Javascript显示/隐藏按钮不工作
- 打开/关闭按钮以显示/隐藏内容
- 显示隐藏复选框
- 在bootstrap中显示隐藏特定的li
- JQuery上下文菜单显示/隐藏问题
- JQuery在单击时停止显示/隐藏
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 使用 jQuery 切换显示/隐藏
- 显示/隐藏有关模型更改的指令内容
- Bootstrap在页面加载缓慢时会立即显示隐藏的模式对话框
- 根据特定条件使用ng显示/ng隐藏来显示/隐藏元素
- 显示/隐藏不起作用
- 使用ng-hide根据条件显示/隐藏按钮
- 仅在第一个结果中显示/隐藏MySQL结果函数
- 显示/隐藏http://ftp链接
- 在显示/隐藏中单击时删除的文本
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- 将页面内容向左移动时显示/隐藏右侧面板
- 显示/隐藏将不会加载