gpt4 book ai didi

javascript - 如何创建掷骰子按钮?

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:45 24 4
gpt4 key购买 nike

我正在尝试为我的代码创建一个掷骰子按钮,但我不太确定如何才能做到这一点,因为如果我确实创建了一个,那么它要么无法按我想要的方式工作,要么我的按钮不起作用功能正确。

<html>

<head>
<link rel="stylesheet" type="text/css" href="style.css" title="Default Styles"/>
<script>

var r_text = new Array ();
r_text[0] = "All leave are brown";
r_text[1] = "fafwfaf";
r_text[2] = "fakfjwkfkajwkfawjf";
r_text[3] = "cornflakes";
r_text[4] = "bannana";
r_text[5] = "Choclate";
r_text[6] = "lol";
var i = Math.floor(7*Math.random())



var videos = [
{
id: 1,
url: "https://www.youtube.com/embed/ngUMyF9D9SQ?autoplay=1",
text: r_text[i]
},
{
id: 2,
url: "https://www.youtube.com/embed/v=r-l_gVPVylM?autoplay=1",
text: r_text[i]
},
{
id: 3,
url: "https://www.youtube.com/embed/6ukTzRjXcR0?autoplay=1",
text: r_text[i]
},
{
id: 4,
url: "https://www.youtube.com/embed/n5BXMNCTu8I?autoplay=1",
text: r_text[i]
},
{
id: 5,
url: "https://www.youtube.com/embed/JtwVmnMNaEY?autoplay=1",
text: r_text[i]
},
{
id: 6,
url: "https://www.youtube.com/embed/lAMgRaHP8Q4?autoplay=1",
text: r_text[i]
}

];

window.onload = function() {
var playerDiv = document.getElementById("random_player");
var player = document.createElement("IFRAME");
var previousId = localStorage.getItem("previousId");
if (previousId) {
var previousIndex = videos.findIndex(v => v.id === parseInt(previousId));
videos.splice(previousIndex, 1);
}
var current = Math.floor(Math.random() * videos.length);
localStorage.setItem("previousId", videos[current].id);
localStorage.getItem("previousId");
var randomVideoUrl = videos[current].url;
player.setAttribute("width", "640");
player.setAttribute("height", "390");
player.setAttribute("src", randomVideoUrl);
playerDiv.appendChild(player);
var textContainer = document.getElementById("r_text");
setTimeout(() => {
textContainer.innerHTML = videos[current].text;

}, 3000)
};




</script>
</head>


<div id="random_player">
<div id="r_text">
</div>
</div>


</html>

我希望它一开始除了按钮之外什么都没有,一旦单击按钮,视频就会开始播放,然后其他一切都开始发生。

最佳答案

试试这个 https://codepen.io/dsb/pen/XWrQbxR

解释:1) 添加按钮到 HTML

<button id="btn-roll">Roll</button>

2)修改JS代码

var r_text = [];
r_text[0] = "All leave are brown";
r_text[1] = "fafwfaf";
r_text[2] = "fakfjwkfkajwkfawjf";
r_text[3] = "cornflakes";
r_text[4] = "bannana";
r_text[5] = "Choclate";
r_text[6] = "lol";
var i = Math.floor(6 * Math.random())

var videos = [{
id: 1,
url: "https://www.youtube.com/embed/ngUMyF9D9SQ?autoplay=1",
text: r_text[1]
},
{
id: 2,
url: "https://www.youtube.com/embed/v=r-l_gVPVylM?autoplay=1",
text: r_text[2]
},
{
id: 3,
url: "https://www.youtube.com/embed/6ukTzRjXcR0?autoplay=1",
text: r_text[3]
},
{
id: 4,
url: "https://www.youtube.com/embed/n5BXMNCTu8I?autoplay=1",
text: r_text[4]
},
{
id: 5,
url: "https://www.youtube.com/embed/JtwVmnMNaEY?autoplay=1",
text: r_text[5]
},
{
id: 6,
url: "https://www.youtube.com/embed/lAMgRaHP8Q4?autoplay=1",
text: r_text[6]
}

];

window.onload = function() {
function rollVideo(numberRand) {
let playerDiv = document.getElementById("random_player");
if (document.querySelector("iframe") !== null) {
document.querySelector("iframe").remove();
}
let player = document.createElement("IFRAME");
let randomVideoUrl = videos[numberRand].url;
player.setAttribute("width", "640");
player.setAttribute("height", "390");
player.setAttribute("src", randomVideoUrl);
playerDiv.appendChild(player);
document.getElementById("r_text").innerHTML = "";
document.getElementById("r_text").innerHTML = videos[numberRand].text;
}
startRoll(i);

document.getElementById("btn-roll").addEventListener("click", startRoll);

function startRoll() {
let currentNumber = Math.floor(Math.random() * videos.length);
rollVideo(currentNumber);
}
};

没有关于文本变量和上一个视频 ID 的条件,所以我缩小了示例。

关于javascript - 如何创建掷骰子按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com