با توجه به توضیح شما و بررسی کدهای ارسالی، به نظر میرسد که مشکل اصلی مربوط به نحوه نمایش اولیه صفحه و سپس تغییر وضعیت نمایش هنگام بارگذاری سایت است. برای حل این مشکل، بهتر است از ساختار و منطق مشابه کد نمونهای که فراهم کردهاید استفاده کنیم.
در کد اصلی شما، برخی از تنظیمات CSS و JavaScript ممکن است باعث شوند که صفحه به درستی در حالت اولیه نمایش داده نشود. در زیر نسخه اصلاح شده کد شما آورده شده است که از منطق کد الهام گرفته شده استفاده میکند و باید مشکل نمایش اولیه را رفع کند.
نسخه اصلاح شده کد شما:
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>دانلود صدا و توضیحات</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
overflow: hidden; /* جلوگیری از اسکرول ناحق */
height: 100vh;
}
.iframe-container {
position: relative;
width: 100%;
height: 100%; /* تنظیم ارتفاع به کل صفحه */
overflow: hidden;
margin-bottom: 20px;
}
iframe {
width: 100%;
height: 100%; /* تنظیم ارتفاع به کل کانتینر */
border: none;
display: none; /* مخفی کردن iframe تا بارگذاری شود */
position: relative;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
z-index: 10000;
background-color: rgba(240, 240, 240, 0.9);
padding: 20px;
border-radius: 10px;
}
.loading-message {
margin-bottom: 20px;
font-size: 18px;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 1s;
}
.error-message {
display: none;
color: red;
margin-top: 20px;
font-size: 16px;
text-align: center;
}
#retryButton {
display: none;
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
}
/* دکمههای دانلود و توضیحات */
.download-button, .description-button {
display: block;
width: 90px;
color: white;
text-align: center;
padding: 6px;
border-radius: 5px;
cursor: pointer;
font-size: 12px;
transition: background-color 0.3s;
position: fixed;
top: 10px;
z-index: 10001;
text-decoration: none;
}
.download-button {
background-color: #4CAF50;
right: 10px;
}
.description-button {
background-color: rgb(84, 110, 122);
left: 10px;
}
.download-button:hover, .description-button:hover {
background-color: #45a049;
}
/* استایل پنجره سفارشی (modal) */
.modal {
display: none;
position: fixed;
z-index: 10002;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-content, .confirm-modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 500px;
text-align: center;
border-radius: 5px;
}
.confirm-modal-content {
max-width: 400px;
}
.modal-button {
margin-top: 20px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.cancel-btn {
background-color: #f44336;
color: white;
}
.confirm-btn {
background-color: #4CAF50;
color: white;
}
.confirm-btn:hover {
background-color: #45a049;
}
.canceled-btn {
background-color: #f44336;
color: white;
}
.canceled-btn:hover {
background-color: #d32f2f;
}
.audio-container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 15px;
}
.audio-item {
margin: 20px 0;
width: 100%;
text-align: center;
}
.audio-label {
font-weight: bold;
margin-bottom: 5px;
font-size: 16px;
text-align: center;
}
.audio-player {
width: 80%;
margin-bottom: 10px;
}
.download-audio-button {
background-color: #4CAF50;
color: white;
padding: 8px 16px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.download-audio-button:hover {
background-color: #45a049;
}
.download-button-bottom {
position: fixed;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
padding: 16px 20px;
max-width: 90%;
width: 250px;
border-radius: 5px;
color: white;
text-align: center;
font-size: 16px;
text-decoration: none;
z-index: 10001;
cursor: pointer;
box-sizing: border-box;
display: none;
}
.download-button-bottom:hover {
background-color: #45a049;
}
/* تنظیمات برای صفحات بسیار کوچک */
@media (max-width: 300px) {
.download-button-bottom {
width: 90%;
}
}
</style>
</head>
<body>
<!-- دکمههای دانلود و توضیحات -->
<a href="#" class="download-button" id="downloadButton">دانلود صدا 📥</a>
<a href="#" class="description-button" id="descriptionButton">توضیحات</a>
<!-- پنجره سفارشی (modal) برای نمایش صداهای دانلود شده -->
<div id="myModal" class="modal">
<div class="modal-content">
<p style="text-align: center;">صداهای شناسایی شده</p>
<div class="audio-container" id="audioContainer">
<!-- پلیرهای صدا در اینجا قرار خواهند گرفت -->
</div>
<button class="modal-button cancel-btn" id="closeBtn">بستن</button>
</div>
</div>
<!-- پنجره تأیید دانلود -->
<div id="confirmModal" class="modal">
<div class="confirm-modal-content">
<p>برای دانلود فایل صوتی روی تایید کلیک کنید و بعد از باز شدن مرورگر سمت راست فایل کلیک کنید و بارگیری فایل را انتخاب کنید.</p>
<button class="modal-button confirm-btn" id="confirmDownloadBtn">تایید</button>
<button class="modal-button canceled-btn" id="cancelDownloadBtn">لغو</button>
</div>
</div>
<!-- پنجره سفارشی (modal) برای نمایش توضیحات -->
<div id="myDescriptionModal" class="modal">
<div class="modal-content">
<hr />
<p style="text-align: center;">دستیار مجهز به هوش مصنوعی شما</p>
<p style="text-align: center;"><span style="color: #0000ff;"><strong>افزایش کیفیت صدا</strong></span> بهروزی از پیشرفته ترین مدل هوش مصنوعی برای تقویت فایل های صوتی</p>
<p style="text-align: center;">حذف نویز و تقویت پیشرفته صدا Ai </p>
<button class="modal-button cancel-btn" id="closeDescriptionBtn">بستن</button>
</div>
</div>
<!-- لودینگ و iframe -->
<div class="iframe-container">
<div class="loading" id="loadingContainer">
<div class="loading-message">لطفاً صبر کنید...</div>
<div class="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="error-message" id="errorMessage">
بارگیری زیاد طول کشید.<br />
در صورت عدم بارگذاری، فیلترشکن خود را روشن کنید.
</div>
<button id="retryButton">بارگذاری مجدد</button>
</div>
<iframe id="myIframe" onload="iframeLoaded()" onerror="iframeError()"></iframe>
</div>
<!-- دکمه دانلود صدا در پایین صفحه (پنهان شده در ابتدا) -->
<a href="#" class="download-button-bottom" id="downloadButtonBottom">دانلود صدا 📥</a>
<script>
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loadingContainer = document.getElementById('loadingContainer');
let interval;
let iframeLoadedFlag = false;
// متغیر برای ذخیرهی لینک فایل صوتی انتخاب شده
let selectedAudioUrl = '';
function startProgress() {
loadingContainer.style.display = 'flex'; // نمایش دوباره دکمه "لطفاً صبر کنید"
progressBar.style.width = '0%'; // ریست شدن نوار پیشرفت
progress = 0; // ریست پیشرفت
interval = setInterval(() => {
if (progress < 100) {
progress += 10;
progressBar.style.width = progress + '%';
}
if (progress === 100) {
clearInterval(interval);
if (!iframeLoadedFlag) {
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
}
}, 1000);
}
function iframeLoaded() {
iframeLoadedFlag = true;
clearInterval(interval);
loadingContainer.style.display = 'none'; // مخفی کردن لودینگ
document.getElementById('myIframe').style.display = 'block'; // نمایش iframe
// نمایش دکمه دانلود پایین
document.getElementById('downloadButtonBottom').style.display = 'block';
}
function iframeError() {
clearInterval(interval);
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
retryButton.addEventListener('click', function() {
progress = 0;
progressBar.style.width = '0%';
iframeLoadedFlag = false;
retryButton.style.display = 'none';
errorMessage.style.display = 'none';
loadingContainer.style.display = 'flex'; // بازگرداندن لودینگ
document.getElementById('myIframe').style.display = 'none'; // مخفی کردن iframe
loadIframe();
startProgress();
});
function loadIframe() {
const iframe = document.getElementById('myIframe');
const urlParts = ['https:', '', 'resembleai-resemble-enhance.hf.space', '']; // آدرس iframe
iframe.src = urlParts.join('/');
}
function handleDownload(event) {
event.preventDefault();
const iframe = document.getElementById('myIframe');
let iframeDocument;
try {
iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
} catch (error) {
console.error("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.", error);
alert("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.");
return;
}
try {
const links = iframeDocument.querySelectorAll('a');
if (links.length === 0) {
alert("هیچ لینک صوتی برای دانلود یافت نشد.");
return;
}
let wavFiles = [];
links.forEach((link) => {
const href = link.href;
if (href && href.toLowerCase().endsWith('.wav')) {
wavFiles.push(href);
}
});
if (wavFiles.length === 0) {
alert("هیچ فایل صوتی با فرمت .wav پیدا نشد.");
return;
}
// محدود به دو فایل
wavFiles = wavFiles.slice(0, 2);
const audioContainer = document.getElementById('audioContainer');
audioContainer.innerHTML = '';
wavFiles.forEach((audioUrl, index) => {
const audioItem = document.createElement('div');
audioItem.classList.add('audio-item');
// تعیین برچسب بر اساس شماره پلیر
let labelText = '';
if (index === 0) {
labelText = 'خروجی صدای بدون نویز';
} else if (index === 1) {
labelText = 'خروجی صدای پیشرفته';
}
audioItem.innerHTML = `
<p class="audio-label">${labelText}</p>
<audio controls class="audio-player">
<source src="${audioUrl}" type="audio/wav">
مرورگر شما از پخش این فایل صوتی پشتیبانی نمیکند.
</audio>
<button class="download-audio-button" onclick="downloadAudio('${audioUrl}')">دانلود صدا</button>
`;
audioContainer.appendChild(audioItem);
});
document.getElementById('myModal').style.display = 'block';
} catch (error) {
console.error("خطا در شناسایی صداها: ", error);
alert("خطایی در شناسایی صداها رخ داد.");
}
}
function downloadAudio(audioUrl) {
// ذخیره لینک فایل صوتی انتخاب شده
selectedAudioUrl = audioUrl;
// نمایش پنجره تأیید
document.getElementById('confirmModal').style.display = 'block';
}
// Event listener برای دکمه تایید دانلود
document.getElementById('confirmDownloadBtn').addEventListener('click', function() {
if (selectedAudioUrl) {
window.open(selectedAudioUrl, '_blank'); // باز کردن لینک در تب جدید
// بسته شدن پنجره تأیید
document.getElementById('confirmModal').style.display = 'none';
// پاک کردن متغیر بعد از استفاده
selectedAudioUrl = '';
}
});
// Event listener برای دکمه لغو دانلود
document.getElementById('cancelDownloadBtn').addEventListener('click', function() {
// بسته شدن پنجره تأیید
document.getElementById('confirmModal').style.display = 'none';
// پاک کردن متغیر
selectedAudioUrl = '';
});
// Event listeners برای دکمه دریافت صدا
document.getElementById('downloadButton').addEventListener('click', handleDownload);
document.getElementById('downloadButtonBottom').addEventListener('click', handleDownload);
// بستن پنجره مودالها با کلیک خارج از آنها
window.addEventListener('click', function(event) {
const modal = document.getElementById('myModal');
const descriptionModal = document.getElementById('myDescriptionModal');
const confirmModal = document.getElementById('confirmModal');
if (event.target == modal) {
modal.style.display = 'none';
}
if (event.target == descriptionModal) {
descriptionModal.style.display = 'none';
}
if (event.target == confirmModal) {
confirmModal.style.display = 'none';
selectedAudioUrl = '';
}
});
// مدیریت پنجره توضیحات
const descriptionModal = document.getElementById('myDescriptionModal');
const descriptionButton = document.getElementById('descriptionButton');
const closeDescriptionBtn = document.getElementById('closeDescriptionBtn');
descriptionButton.addEventListener('click', function(event) {
event.preventDefault();
descriptionModal.style.display = 'block';
});
closeDescriptionBtn.addEventListener('click', function() {
descriptionModal.style.display = 'none';
});
// مدیریت پنجره بستن مودال صدا
const modal = document.getElementById('myModal');
const closeBtn = document.getElementById('closeBtn');
closeBtn.addEventListener('click', function() {
modal.style.display = 'none';
});
// اجرای بارگذاری iframe و آغاز لودینگ
loadIframe();
startProgress();
</script>
</body>
</html>
توضیحات تغییرات انجام شده:
-
تنظیمات CSS:
- Body: اضافه کردن
overflow: hidden; و height: 100vh; برای جلوگیری از اسکرول ناحق و تنظیم ارتفاع کل صفحه.
- .iframe-container: تنظیم ارتفاع به
100% تا کل فضای صفحه را پوشش دهد.
- iframe: تنظیم ارتفاع به
100% و مخفی کردن آن تا زمانی که بارگذاری شود.
- .loading: اضافه کردن پسزمینه سفیدکمرنگ و گرد کردن گوشهها برای زیبایی بیشتر.
- دکمهها و مودالها: تنظیم موقعیت و ظاهر به نحوی که با طراحی کلی هماهنگ باشند.
-
JavaScript:
- تابع
startProgress: شروع نوار پیشرفت و نمایش مجدد لودینگ در مواقع لازم.
- تابع
iframeLoaded: مخفی کردن لودینگ و نمایش iframe پس از بارگذاری موفقیتآمیز.
- تابع
iframeError: نمایش پیام خطا و دکمه بارگذاری مجدد در صورت عدم موفقیت.
- تابع
handleDownload: مدیریت دانلود فایلهای صوتی با بررسی وجود لینکهای .wav و نمایش مودال مربوطه.
- مدیریت مودالها: اضافه کردن قابلیت بستن مودالها با کلیک خارج از آنها و مدیریت دکمههای مختلف.
-
ساختار HTML:
- دکمهها: تنظیم دکمههای دانلود و توضیحات با موقعیتهای ثابت و زیبا.
- مودالها: تقسیم مودالها به هدفهای مختلف (نمایش صداها، توضیحات، تأیید دانلود) و طراحی مناسب آنها.
- لودینگ و iframe: قرار دادن لودینگ و iframe در یک کانتینر مشترک و مدیریت نمایش آنها با JavaScript.
نتیجه
با اعمال این تغییرات، صفحه شما ابتدا در حالت لودینگ نمایش داده میشود و پس از بارگذاری موفقیتآمیز iframe، لودینگ مخفی شده و محتوای iframe نمایش داده میشود. در صورت بروز خطا در بارگذاری، پیام خطا و دکمه بارگذاری مجدد به کاربر نشان داده میشود. همچنین دکمههای دانلود و توضیحات به صورت ثابت در بالای صفحه قابل دسترسی هستند و از مودالها برای نمایش اطلاعات و تأیید عملیات استفاده شده است.
اگر همچنان با مشکلی مواجه شدید، لطفاً جزئیات بیشتری راجع به مشکل ارائه دهید تا بتوانم دقیقتر راهنمایی کنم.