所以我只在我的JSON数据中添加了两个URL,它们都与一首歌曲相关。
现在,前两个图像的歌曲不同,而其他图像完全没有歌曲。
我想从所有div中获取选定的索引,以便可以使用类似songDetails[selectedIndex].Song
但是我不知道如何获得点击的选定索引。
那我该如何开始播放与单击的项目相对应的歌曲?
const imageData = [{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/09/Hi-tech-electronic-background-music-120-bpm.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/04/Relaxing-thinking-time-music.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
];
function generateTemplate(imageData) {
return `
<div class="image-box">
<p>${imageData.Name}</p>
<img src="${imageData.Icon}"
alt="${imageData.Name}"
style="width:100%"
onclick="playAudio(imageData, this);">
</div>`
}
function playAudio(songDetails) {
let collection = document.getElementsByClassName("image-box")[0].selectedIndex;
alert(collection);
let song = new Audio(songDetails.Song);
song.play();
}
document.getElementById("image-container").innerHTML = `
${imageData.map(generateTemplate).join('')}`
body {
padding: 0;
margin: 0;
background: url("/images/party.jpg");
background-size: cover;
/* background: green; */
display: flex;
/* new */
flex-direction: column;
/* new */
height: 100vh;
/* new */
}
#albumImage{
background: green;
height: 150px;
width: 150px;
margin-left: auto;
margin-right: auto;
}
#search-div {
/* position: fixed; */
flex: 0 0 55px;
/* adjusted; disabled flex-shrink */
width: 100%;
background: #242424;
/* z-index: 20; */
}
.search-bar {
border-radius: 50px;
width: 15%;
margin-top: 1%;
margin-left: 42%;
position: relative;
}
.container {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-wrap: wrap;
/* position: relative; */
min-height: 0;
}
.music-bar {
background-color: #242424;
width: 100%;
height: 80px;
position: fixed;
}
.media-controls {
display: flex;
margin-left: auto;
margin-right: auto;
width: 150px;
text-align: center;
height: 40%;
top: 10px;
position: relative;
}
#mediaControlBar {
background: #242424;
height: 10%;
}
.prev-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
.play-btn {
color: rgb(88, 88, 88);
font-size: 28px;
width: 50%;
}
.play-btn:hover {
color: rgb(187, 187, 187);
}
.next-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
#seek-bar {
width: 50%;
height: 5px;
background-color: rgb(78, 78, 78);
display: flex;
border-radius: 50px;
cursor: pointer;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
}
#fill {
background-color: rgb(129, 129, 129);
background-color: #FFF;
border-radius: 20px;
width: 10%;
}
.image-box {
width: 300px;
margin: 20px;
position: relative;
vertical-align: top;
font-family: 'Oswald', sans-serif;
}
.flex-container {
display: flex;
flex-wrap: wrap;
overflow-y: auto;
justify-content: center;
/* margin-top: 2%; */
text-align: center;
color: white;
}
.flex-container img {
opacity: 0.5;
}
.flex-container img:hover {
opacity: 1;
}
@media screen and (max-width: 960px) {
.search-bar {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
top: 15%;
width: 40%;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Images</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<link href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<div id="search-div">
<input type="text" class="search-bar" id="searchBar">
</div>
<div class="container">
<div class="flex-container" id="image-container">
</div>
</div>
<div class="control-bar" id="mediaControlBar">
<div class="media-controls">
<div class="prev-btn">
<i class="fas fa-angle-double-left"></i>
</div>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="next-btn">
<i class="fas fa-angle-double-right"></i>
</div>
</div>
<div id="seek-bar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>
<script src="/js/data.js"></script>
</body>
</html>
答案 0 :(得分:0)
您必须定义自己的索引,并根据索引选择歌曲的编号
function playAudio(songDetails, self) {
let index = [...self.parentElement.children].indexOf(self)
let song = new Audio(songDetails[index].Song);
您的点击事件也应该在您的div上
<div class="image-box" onclick="playAudio(imageData, this);">
const imageData = [{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/09/Hi-tech-electronic-background-music-120-bpm.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/04/Relaxing-thinking-time-music.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
];
function generateTemplate(imageData) {
return `
<div class="image-box"
onclick="playAudio(imageData, this);">
<p>${imageData.Name}</p>
<img src="${imageData.Icon}"
alt="${imageData.Name}"
style="width:100%">
</div>`
}
function playAudio(songDetails, self) {
let index = [...self.parentElement.children].indexOf(self)
console.log(index);
console.log(songDetails);
let song = new Audio(songDetails[index].Song);
song.play();
}
document.getElementById("image-container").innerHTML = `
${imageData.map(generateTemplate).join('')}`
body {
padding: 0;
margin: 0;
background: url("/images/party.jpg");
background-size: cover;
/* background: green; */
display: flex;
/* new */
flex-direction: column;
/* new */
height: 100vh;
/* new */
}
#albumImage{
background: green;
height: 150px;
width: 150px;
margin-left: auto;
margin-right: auto;
}
#search-div {
/* position: fixed; */
flex: 0 0 55px;
/* adjusted; disabled flex-shrink */
width: 100%;
background: #242424;
/* z-index: 20; */
}
.search-bar {
border-radius: 50px;
width: 15%;
margin-top: 1%;
margin-left: 42%;
position: relative;
}
.container {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-wrap: wrap;
/* position: relative; */
min-height: 0;
}
.music-bar {
background-color: #242424;
width: 100%;
height: 80px;
position: fixed;
}
.media-controls {
display: flex;
margin-left: auto;
margin-right: auto;
width: 150px;
text-align: center;
height: 40%;
top: 10px;
position: relative;
}
#mediaControlBar {
background: #242424;
height: 10%;
}
.prev-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
.play-btn {
color: rgb(88, 88, 88);
font-size: 28px;
width: 50%;
}
.play-btn:hover {
color: rgb(187, 187, 187);
}
.next-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
#seek-bar {
width: 50%;
height: 5px;
background-color: rgb(78, 78, 78);
display: flex;
border-radius: 50px;
cursor: pointer;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
}
#fill {
background-color: rgb(129, 129, 129);
background-color: #FFF;
border-radius: 20px;
width: 10%;
}
.image-box {
width: 300px;
margin: 20px;
position: relative;
vertical-align: top;
font-family: 'Oswald', sans-serif;
}
.flex-container {
display: flex;
flex-wrap: wrap;
overflow-y: auto;
justify-content: center;
/* margin-top: 2%; */
text-align: center;
color: white;
}
.flex-container img {
opacity: 0.5;
}
.flex-container img:hover {
opacity: 1;
}
@media screen and (max-width: 960px) {
.search-bar {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
top: 15%;
width: 40%;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Images</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<link href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<div id="search-div">
<input type="text" class="search-bar" id="searchBar">
</div>
<div class="container">
<div class="flex-container" id="image-container">
</div>
</div>
<div class="control-bar" id="mediaControlBar">
<div class="media-controls">
<div class="prev-btn">
<i class="fas fa-angle-double-left"></i>
</div>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="next-btn">
<i class="fas fa-angle-double-right"></i>
</div>
</div>
<div id="seek-bar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>
<script src="/js/data.js"></script>
</body>
</html>
答案 1 :(得分:0)
我更新了您的Option Explicit
Dim h as Long, i as Long, l as Long, j as Long, k as Long, lr as Long
Dim x as Integer
Sub IAR_macro()
On Error GoTo Canceled
i = (Range("B1").End(xlDown).Row) - 1
Sheets(x).Activate
Do While l < (i - l)
和generateTemplate
函数。请参阅底部的代码段。
在您的playAudio
函数中,我添加了第二个参数playAudio
。在您的clickedElement
中,您已经传递了参数generateTemplate
。 this
在这种情况下表示在其上设置onclick的元素。
我还向您的this
函数生成的data-song="${imageData.Song}"
元素中添加了img
。
因此,在generateTemplate
函数中,我们收到了单击的playAudio
元素。通过调用img
,我们检索在clickedElement.dataset.song
元素上声明的data-song
属性。这样,您就知道要播放什么歌曲。
请注意,当您的图片部分的JSON定义中没有img
属性时,它将作为点击的歌曲返回song
。因此,在调用undefined
之前,您可能必须添加if
语句。
let song = new Audio(song);
const imageData = [{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/09/Hi-tech-electronic-background-music-120-bpm.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/04/Relaxing-thinking-time-music.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
];
function generateTemplate(imageData) {
return `
<div class="image-box">
<p>${imageData.Name}</p>
<img src="${imageData.Icon}" data-song="${imageData.Song}"
alt="${imageData.Name}"
style="width:100%"
onclick="playAudio(imageData, this);">
</div>`
}
function playAudio(songDetails, clickedElement) {
var clickedSong = clickedElement.dataset.song;
alert("Clicked song: " + clickedSong );
let song = new Audio(clickedSong);
song.play();
}
document.getElementById("image-container").innerHTML = `
${imageData.map(generateTemplate).join('')}`
body {
padding: 0;
margin: 0;
background: url("/images/party.jpg");
background-size: cover;
/* background: green; */
display: flex;
/* new */
flex-direction: column;
/* new */
height: 100vh;
/* new */
}
#albumImage{
background: green;
height: 150px;
width: 150px;
margin-left: auto;
margin-right: auto;
}
#search-div {
/* position: fixed; */
flex: 0 0 55px;
/* adjusted; disabled flex-shrink */
width: 100%;
background: #242424;
/* z-index: 20; */
}
.search-bar {
border-radius: 50px;
width: 15%;
margin-top: 1%;
margin-left: 42%;
position: relative;
}
.container {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-wrap: wrap;
/* position: relative; */
min-height: 0;
}
.music-bar {
background-color: #242424;
width: 100%;
height: 80px;
position: fixed;
}
.media-controls {
display: flex;
margin-left: auto;
margin-right: auto;
width: 150px;
text-align: center;
height: 40%;
top: 10px;
position: relative;
}
#mediaControlBar {
background: #242424;
height: 10%;
}
.prev-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
.play-btn {
color: rgb(88, 88, 88);
font-size: 28px;
width: 50%;
}
.play-btn:hover {
color: rgb(187, 187, 187);
}
.next-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
#seek-bar {
width: 50%;
height: 5px;
background-color: rgb(78, 78, 78);
display: flex;
border-radius: 50px;
cursor: pointer;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
}
#fill {
background-color: rgb(129, 129, 129);
background-color: #FFF;
border-radius: 20px;
width: 10%;
}
.image-box {
width: 300px;
margin: 20px;
position: relative;
vertical-align: top;
font-family: 'Oswald', sans-serif;
}
.flex-container {
display: flex;
flex-wrap: wrap;
overflow-y: auto;
justify-content: center;
/* margin-top: 2%; */
text-align: center;
color: white;
}
.flex-container img {
opacity: 0.5;
}
.flex-container img:hover {
opacity: 1;
}
@media screen and (max-width: 960px) {
.search-bar {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
top: 15%;
width: 40%;
}
}
答案 2 :(得分:0)
您需要jquery API中的How to rollback, reset, or drop Ecto test database?。
来自文档:
从匹配的元素中搜索给定的元素。
const imageData = [{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/09/Hi-tech-electronic-background-music-120-bpm.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Song": "http://www.orangefreesounds.com/wp-content/uploads/2018/04/Relaxing-thinking-time-music.mp3",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
}, {
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
{
"Icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"Name": "Mona Lisa",
"Painter": "Picasso",
"Year": "1786"
},
];
function generateTemplate(imageData) {
return `
<div class="image-box">
<p>${imageData.Name}</p>
<img src="${imageData.Icon}"
alt="${imageData.Name}"
style="width:100%"
onclick="playAudio(imageData, this);">
</div>`
}
function playAudio(songDetails, ctx) {
//let collection = document.getElementsByClassName("image-box")[0].selectedIndex;
let collection = $('.image-box img').index(ctx);
alert(collection);
let song = new Audio(songDetails.Song);
song.play();
}
document.getElementById("image-container").innerHTML = `
${imageData.map(generateTemplate).join('')}`
body {
padding: 0;
margin: 0;
background: url("/images/party.jpg");
background-size: cover;
/* background: green; */
display: flex;
/* new */
flex-direction: column;
/* new */
height: 100vh;
/* new */
}
#albumImage{
background: green;
height: 150px;
width: 150px;
margin-left: auto;
margin-right: auto;
}
#search-div {
/* position: fixed; */
flex: 0 0 55px;
/* adjusted; disabled flex-shrink */
width: 100%;
background: #242424;
/* z-index: 20; */
}
.search-bar {
border-radius: 50px;
width: 15%;
margin-top: 1%;
margin-left: 42%;
position: relative;
}
.container {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-wrap: wrap;
/* position: relative; */
min-height: 0;
}
.music-bar {
background-color: #242424;
width: 100%;
height: 80px;
position: fixed;
}
.media-controls {
display: flex;
margin-left: auto;
margin-right: auto;
width: 150px;
text-align: center;
height: 40%;
top: 10px;
position: relative;
}
#mediaControlBar {
background: #242424;
height: 10%;
}
.prev-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
.play-btn {
color: rgb(88, 88, 88);
font-size: 28px;
width: 50%;
}
.play-btn:hover {
color: rgb(187, 187, 187);
}
.next-btn {
font-size: 22px;
color: rgb(88, 88, 88);
width: 50%;
margin-top: 5px;
}
#seek-bar {
width: 50%;
height: 5px;
background-color: rgb(78, 78, 78);
display: flex;
border-radius: 50px;
cursor: pointer;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
}
#fill {
background-color: rgb(129, 129, 129);
background-color: #FFF;
border-radius: 20px;
width: 10%;
}
.image-box {
width: 300px;
margin: 20px;
position: relative;
vertical-align: top;
font-family: 'Oswald', sans-serif;
}
.flex-container {
display: flex;
flex-wrap: wrap;
overflow-y: auto;
justify-content: center;
/* margin-top: 2%; */
text-align: center;
color: white;
}
.flex-container img {
opacity: 0.5;
}
.flex-container img:hover {
opacity: 1;
}
@media screen and (max-width: 960px) {
.search-bar {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
top: 15%;
width: 40%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Images</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<link href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<div id="search-div">
<input type="text" class="search-bar" id="searchBar">
</div>
<div class="container">
<div class="flex-container" id="image-container">
</div>
</div>
<div class="control-bar" id="mediaControlBar">
<div class="media-controls">
<div class="prev-btn">
<i class="fas fa-angle-double-left"></i>
</div>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="next-btn">
<i class="fas fa-angle-double-right"></i>
</div>
</div>
<div id="seek-bar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>
<script src="/js/data.js"></script>
</body>
</html>