这个网站:http://www.twosides.co/在它的主页上,如果你看几秒钟,文字会继续变化,效果非常酷。有人有想法这样做吗?
我完全不知道怎么做,所以我来到stackoverflow社区寻求帮助!
感谢所有回答的人!
答案 0 :(得分:4)
这样的事情会改变信息:
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script>
$(function () {
var messages = [],
index = 0;
messages.push('Message 1');
messages.push('Message 2');
messages.push('Message 3');
function cycle() {
$('#some-id').html(messages[index]);
index++;
if (index === messages.length) {
index = 0;
}
setTimeout(cycle, 3000);
}
cycle();
});
</script>
<div id="some-id"></div>
只需添加一些动画代码。
答案 1 :(得分:0)
从源代码看,他们似乎正在使用Masonry。一般来说,使用animate function在jQuery中可以很容易地完成这个。
以下是带动画的示例实现。它使用tmp元素来确定文本宽度。
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script>
$(function () {
var messages = [],
index = 0;
messages.push('I am short.');
messages.push('Longer messages are nice.');
messages.push('And short again.');
var tmp = $('#tmp');
var el = $('#el');
function cycle() {
var i = index%message.length;
tmp.html(messages[i]);
el.html('').animate({width: tmp.width()+1}, function() {
el.html(messages[i]);
index++;
setTimeout(cycle, 3000);
});
}
cycle();
});
</script>
<style>
#tmp {
visibility: hidden;
}
</style>
I am a text. <span id="el"></span> Me too.
<span id='tmp'>foo<span>
答案 2 :(得分:-1)
要每x秒更改一次文本,您应该使用 延迟(X);
所以:
$("#yourElem").delay(1000).text("Text changed");
你必须把这段代码放在循环中......
答案 3 :(得分:-1)
查看twoSides'代码
的有效版本的评论我刚刚在my personal site上使用vanilla JS和一些CSS实现了类似的功能。以下是该代码的修改版本,以满足这些目的。
var seconds = 5;
var madlibs = {
'a': ['eat pie', 'jump rope', 'write code', 'do things'],
'b': ['a fork', 'your friends', 'the quickness', 'other things']
};
var madlibA = document.getElementById('a');
var madlibB = document.getElementById('b');
function rand(x) {
return Math.floor(Math.random() * x);
}
function change() {
var txtA = madlibs['a'][rand(madlibs['a'].length)];
var txtB = madlibs['b'][rand(madlibs['b'].length)];
madlibA.innerHTML = txtA;
madlibB.innerHTML = txtB;
madlibA.classList.remove('flip');
madlibB.classList.remove('flip');
}
function reveal() {
madlibA.classList.add('flip');
madlibB.classList.add('flip');
setTimeout(change, 500); // half-second like in CSS
}
setInterval(reveal, seconds * 1000);
.madlib {
transition: all 0.5s linear;
font-size:1rem;
color:black;
}
.flip {
font-size:0px;
color:transparent;
}
<p>
we make it easy to <span id="a" class="madlib">do things</span> with <span id="b" class="madlib">other things.</span>
</p>
转换并不完全相同,因为我还没有想出一个用css3做一个好的方法来进行slideDown效果(在overflow
元素上设置inline-block
会使文本不对齐)
设置JS中的秒数比你看到的短几秒,因为动画需要0.5秒进出,所以要做出相应的计划。
您可以使用setInterval
定期调用函数,或setTimeout
稍后调用函数。
所以你可以做到
function changeText(){
// code to change the text
}
setInterval(changeText,500);
或
function changeText(){
// code to change text
setTimeout(changeText,500);
}
changeText();
让函数在一段时间后自行调用。
要更改文本,您可以使用这样的选项数组:
var sentences = ["test 1", "test 2", "test 3"],
iter = 0,
changeEl = document.getElementById("change");
function changeText(){
changeEl.innerHTML = sentences[iter%sentences.length];
iter++;
setTimeout(changeText,500);
}
changeText();
对于动画,您可以将此技术转换为与jQuery或mootools等库一起使用。
编辑:
我没有测试任何这个,但看起来他们正在使用的代码就是这样:
HTML:
<div id="madlib-sentence">
<span class="twosides">TwoSides</span> makes it easy to
<div class="madlibs" style="width:400px;">
<ul id="madlibs-verbs">
<li class="madlibs-verb"><span>discuss and debate contentious topics</span></li>
<li class="madlibs-verb"><span>see every side of an issue</span></li>
<li class="madlibs-verb"><span>compare your viewpoints</span></li>
</ul>
</div> with
<div class="madlibs" style="width:100px;">
<ul id="madlibs-nouns">
<li class="madlibs-noun"><span>friends.</span></li>
<li class="madlibs-noun"><span>the world.</span></li>
<li class="madlibs-noun"><span>co-workers.</span></li>
<li class="madlibs-noun"><span>family.</span></li>
</ul>
</div>
</div>
的CSS:
#primary p .madlibs-verb,#primary p .madlibs-noun {
font-family:HelveticaNeue,'Helvetica Neue',HelveticaNeueRoman,HelveticaNeue-Roman,'Helvetica Neue Roman',TeXGyreHerosRegular,Helvetica,Arial,sans-serif;
font-weight:bold;
font-style:normal;
}
#primary #madlib-sentence {
margin-top:20px;
color:#f1f1f1;
text-shadow:0 0 2px #000;
font-size:19px;
font-style:italic;
}
#primary #madlib-sentence .madlibs {
display:inline-block;
position:relative;
height:19px;
overflow:hidden;
padding-bottom:3px;
margin-bottom:-3px;
}
#primary #madlib-sentence .madlibs ul {
position:absolute;
left:0;
top:0;
list-style-type:none;
}
#primary #madlib-sentence .madlibs ul .madlibs-verb,#primary #madlib-sentence .madlibs ul .madlibs-noun {
line-height:24px;
padding-bottom:30px;
font-family:HelveticaNeue,'Helvetica Neue',HelveticaNeueRoman,HelveticaNeue-Roman,'Helvetica Neue Roman',TeXGyreHerosRegular,Helvetica,Arial,sans-serif;
font-weight:bold;
font-style:normal;
}
#primary #madlib-sentence .madlibs ul .madlibs-verb span,#primary #madlib-sentence .madlibs ul .madlibs-noun span {
white-space:nowrap;
}
JS:
function animate_madlib(container) {
if (typeof container.data("index") === "undefined") {
container.data("index", 0);
}
index = container.data("index") + 1;
if (index == container.children().length) {
index = 0;
}
current_child = container.children().eq(index);
parent = container.parent();
new_width = current_child.children("span").outerWidth();
if (new_width > parent.width()) {
parent.animate({width: new_width+"px"}, 100, "swing", function() {
container.animate({top: -(index*current_child.outerHeight())}, 1000, "easeInOutQuint");
});
}
else {
container.animate({top: -(index*current_child.outerHeight())}, 1000, "easeInOutQuint", function() {
parent.animate({width: new_width+"px"}, 100, "swing");
});
}
container.data("index", index);
new_container = (container.attr('id') == 'madlibs-verbs' ? $('#madlibs-nouns') : $('#madlibs-verbs'));
setTimeout(function(new_container) {animate_madlib(new_container)}, 2500, new_container);
}
$(document).ready( function() {
// madlib
$('.madlibs').each(function() {
$(this).css("width", $(this).find("span:first").outerWidth());
});
setTimeout(function(){animate_madlib($('#madlibs-verbs'))}, 1500);
});