展不展開
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collapsible News Articles with YouTube</title>
<style>
.collapsible {
background-color: #f1f1f1;
color: #444;
cursor: pointer;
padding: 10px;
width: 100%;
border: none;
text-align: left;
font-size: 16px;
border-radius: 5px;
outline: none;
transition: background-color 0.3s;
}
.collapsible:hover {
background-color: #ddd;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: white;
border-radius: 0 0 5px 5px;
}
.arrow {
float: right;
margin-left: 10px;
transition: transform 0.3s;
}
.arrow.open {
transform: rotate(180deg);
}
.youtube-container {
display: none;
}
.youtube-container iframe {
width: 100%;
height: 315px;
}
</style>
</head>
<body>
<h2>News Articles</h2>
<button class="collapsible">Article 1 <span class="arrow">▼</span></button>
<div class="content">
<p>This is the content of the first article.</p>
<div class="youtube-container">
<iframe src="" data-src="https://www.youtube.com/embed/VIDEO_ID_1" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<button class="collapsible">Article 2 <span class="arrow">▼</span></button>
<div class="content">
<p>This is the content of the second article.</p>
<div class="youtube-container">
<iframe src="" data-src="https://www.youtube.com/embed/VIDEO_ID_2" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<script>
document.querySelectorAll('.collapsible').forEach(button => {
button.addEventListener('click', function() {
const content = this.nextElementSibling;
const arrow = this.querySelector('.arrow');
const youtubeContainer = content.querySelector('.youtube-container');
if (content.style.display === 'block') {
content.style.display = 'none';
arrow.classList.remove('open');
youtubeContainer.style.display = 'none';
content.querySelector('iframe').src = '';
} else {
content.style.display = 'block';
arrow.classList.add('open');
youtubeContainer.style.display = 'block';
const iframe = youtubeContainer.querySelector('iframe');
iframe.src = iframe.getAttribute('data-src');
}
});
});
</script>
</body>
</html>
頁:
[1]