使用javascript或jquery的随机单词弹出脚本
所以我已经在这里找到了,这几乎正是我想要的。唯一的区别是,我不希望创建框,而是希望它们是从类似数组的数组中提取的单词使用javascript或jquery的随机单词弹出脚本,javascript,jquery,html,css,random,Javascript,Jquery,Html,Css,Random,所以我已经在这里找到了,这几乎正是我想要的。唯一的区别是,我不希望创建框,而是希望它们是从类似数组的数组中提取的单词 var textarray = [ "wow", "so amaze", "much hunt", "such treasure"]; 因此,它不是随机出现的彩色框,而是随机出现的随机单词之一。下面是来自JSFIDLE的代码 (function makeDiv(){ var divsize = ((Math.random()*100) +
var textarray = [
"wow",
"so amaze",
"much hunt",
"such treasure"];
因此,它不是随机出现的彩色框,而是随机出现的随机单词之一。下面是来自JSFIDLE的代码
(function makeDiv(){
var divsize = ((Math.random()*100) + 50).toFixed();
var color = '#'+ Math.round(0xffffff * Math.random()).toString(16);
$newdiv = $('<div/>').css({
'width':divsize+'px',
'height':divsize+'px',
'background-color': color
});
var posx = (Math.random() * ($(document).width() - divsize)).toFixed();
var posy = (Math.random() * ($(document).height() - divsize)).toFixed();
$newdiv.css({
'position':'absolute',
'left':posx+'px',
'top':posy+'px',
'display':'none'
}).appendTo( 'body' ).fadeIn(100).delay(300).fadeOut(200, function(){
$(this).remove();
makeDiv();
});
})();
这是怎么回事?
关于以下行中列出的更改的评论
var textArray = [
"wow",
"so amaze",
"much hunt",
"such treasure"];
function makeDiv(){
var divsize = ((Math.random()*100) + 50).toFixed();
var color = '#'+ Math.round(0xffffff * Math.random()).toString(16);
$newdiv = $('<div/>').css({
'width': ' 30 px', // sets width to constant
'height': '10px', // sets height to constant
// removes background-color property of div, but keeps
// generating random colors that will be applied to the random words
// 'background-color': color
});
// randomWord will accommodate for textArray of any size
var randomWord = textArray[ (Math.floor(Math.random() * textArray.length)) ]
var posx = (Math.random() * ($(document).width() - divsize)).toFixed();
var posy = (Math.random() * ($(document).height() - divsize)).toFixed();
// appends randomWord to new div
$newdiv.text(randomWord).css({
'position':'absolute',
'left':posx+'px',
'top':posy+'px',
'display':'none',
// adds randomly generated color to random word
'color' : color
}).appendTo( 'body' ).fadeIn(100).delay(300).fadeOut(200, function(){
$(this).remove();
makeDiv();
});
}
makeDiv();
所以你应该用“color”来改变“background color”,去掉divsize,然后用math.random调用一个介于1和数组长度之间的随机数,把数组中的值放到一个变量中,然后添加到div中。这些只是简单的提示,我相信你自己可以做到;您好,如果您发现任何答案有用,请选择一个作为正式答案。在这个网站上建立声誉很有帮助。非常感谢。