// تابع شروع پیشرفت نوار لودینگ
function startProgress() {
progressInterval = setInterval(() => {
if (progress < 100) {
progress += 20; // افزایش ۲۰٪ در هر ثانیه
if (progress > 100) progress = 100;
progressBar.style.width = progress + '%';
}
if (progress >= 100) {
clearInterval(progressInterval);
if (!iframeLoadedFlag) {
showError();
}
}
}, 1000);
// تایماوت برای نمایش پیام خطا بعد از ۵ ثانیه
errorTimeout = setTimeout(() => {
if (!iframeLoadedFlag) {
showError();
}
}, 5000);
}
// تابع بارگذاری موفق iframe
function iframeLoaded() {
iframeLoadedFlag = true;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
loading.style.display = 'none';
iframe.style.display = 'block';
}
// تابع خطا در بارگذاری iframe
function iframeError() {
clearInterval(progressInterval);
clearTimeout(errorTimeout);
showError();
}
// نمایش خطا
function showError() {
errorMessage.style.display = 'block';
retryButton.style.display = 'block';
}
// بازنشانی لودینگ
function resetLoading() {
progress = 0;
progressBar.style.width = '0%';
iframeLoadedFlag = false;
loading.style.display = 'flex';
errorMessage.style.display = 'none';
retryButton.style.display = 'none';
iframe.style.display = 'none';
clearInterval(progressInterval);
clearTimeout(errorTimeout);
}
// بارگذاری مجدد iframe
function loadIframe() {
iframe.src = iframe.src;
}
// رویداد کلیک بر روی دکمه بارگذاری مجدد
retryButton.addEventListener('click', () => {
resetLoading();
loadIframe();
startProgress();
});
// اسکرول کردن iframe با کلیک بر روی Overlay بالایی
overlayTop.addEventListener('click', function(e) {
const clickY = e.clientY;
const overlayHeight = overlayTop.clientHeight;
const direction = clickY > overlayHeight / 2 ? 1 : -1; // تعیین جهت اسکرول بر اساس محل کلیک
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition); // جلوگیری از اسکرول منفی
iframe.contentWindow.scrollTo(0, scrollPosition);
});
// اسکرول کردن iframe با کلیک بر روی Overlay پایینی
overlayBottom.addEventListener('click', function(e) {
const clickY = e.clientY;
const overlayHeight = overlayBottom.clientHeight;
const direction = clickY > overlayHeight / 2 ? 1 : -1; // تعیین جهت اسکرول بر اساس محل کلیک
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition); // جلوگیری از اسکرول منفی
iframe.contentWindow.scrollTo(0, scrollPosition);
});
// اسکرول کردن iframe با استفاده از چرخش موس در Overlay بالایی
overlayTop.addEventListener('wheel', function(e) {
e.preventDefault();
const direction = e.deltaY > 0 ? 1 : -1;
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition); // جلوگیری از اسکرول منفی
iframe.contentWindow.scrollTo(0, scrollPosition);
});
// اسکرول کردن iframe با استفاده از چرخش موس در Overlay پایینی
overlayBottom.addEventListener('wheel', function(e) {
e.preventDefault();
const direction = e.deltaY > 0 ? 1 : -1;
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition); // جلوگیری از اسکرول منفی
iframe.contentWindow.scrollTo(0, scrollPosition);
});
// اسکرول کردن iframe با استفاده از لمسی در Overlay بالایی
let touchStartYTop;
overlayTop.addEventListener('touchstart', function(e) {
touchStartYTop = e.touches[0].clientY;
});
overlayTop.addEventListener('touchmove', function(e) {
e.preventDefault();
const touchY = e.touches[0].clientY;
const direction = touchStartYTop > touchY ? 1 : -1;
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition);
iframe.contentWindow.scrollTo(0, scrollPosition);
touchStartYTop = touchY;
});
// اسکرول کردن iframe با استفاده از لمسی در Overlay پایینی
let touchStartYBottom;
overlayBottom.addEventListener('touchstart', function(e) {
touchStartYBottom = e.touches[0].clientY;
});
overlayBottom.addEventListener('touchmove', function(e) {
e.preventDefault();
const touchY = e.touches[0].clientY;
const direction = touchStartYBottom > touchY ? 1 : -1;
scrollPosition += direction * scrollStep;
scrollPosition = Math.max(0, scrollPosition);
iframe.contentWindow.scrollTo(0, scrollPosition);
touchStartYBottom = touchY;
});
// شروع بارگذاری iframe و لودینگ
loadIframe();
startProgress();
</script>
سلام،
مشکلی که شما با آن مواجه هستید به دلیل تغییر ارتفاع viewport هنگام باز شدن کیبورد یا اسکرول است که باعث میشود آن ۱ سانتیمتر مخفی در پایین صفحه نمایان شود. برای حل این مشکل، باید CSS کد را به گونهای تغییر دهیم که overlay پایین و iframe به درستی تنظیم شوند تا همیشه آن ۱ سانتیمتر مخفی باقی بماند، حتی زمانی که ارتفاع viewport تغییر میکند.
تغییرات پیشنهادی:
-
تنظیم مجدد استایل iframe:
در بخش CSS، تنظیمات مربوط به iframe را به صورت زیر تغییر دهید:
.iframe-container iframe {
position: fixed;
top: -227px; /* پنهان کردن ۶ سانتیمتر بالای صفحه (227px) */
left: 0;
width: 100%;
bottom: -38px; /* پنهان کردن ۱ سانتیمتر پایین صفحه (38px) */
border: none;
display: none; /* مخفی کردن iframe تا بارگذاری موفق */
}
توضیح: با استفاده از position: fixed و تنظیم مقادیر منفی برای top و bottom، مطمئن میشویم که iframe همیشه در موقعیت درست قرار دارد و بخشهای بالایی و پایینی آن توسط overlayها مخفی میشوند، حتی زمانی که ارتفاع viewport تغییر کند.
-
تنظیم مجدد استایل overlay پایین:
مطمئن شوید که استایل .overlay-bottom به صورت زیر تنظیم شده است:
.overlay-bottom {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 38px; /* ارتفاع ۱ سانتیمتر (38px) */
background-color: transparent; /* رنگ پسزمینه شفاف */
z-index: 900; /* زیر iframe-container */
pointer-events: none; /* اجازه دادن به اسکرول از طریق iframe */
cursor: pointer; /* تغییر نشانگر موس برای نشان دادن امکان کلیک */
}
توضیح: با استفاده از position: fixed و bottom: 0، overlay پایین همیشه در پایین viewport باقی میماند و بخش پایینی iframe را مخفی میکند.
-
اطمینان از ترتیب صحیح z-indexها:
مطمئن شوید که z-indexهای المانها به درستی تنظیم شدهاند تا overlayها روی iframe قرار گیرند.
-
بررسی تنظیمات دیگر:
اگر در بخشهای دیگر کد از تنظیماتی استفاده کردهاید که میتواند با این تغییرات تداخل داشته باشد، آنها را بررسی و در صورت نیاز اصلاح کنید.
کد کامل اصلاحشده:
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>صفحه با iframe و overlays</title>
<style>
/* تنظیمات پایه برای body و html */
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden; /* مخفی کردن اسکرولهای اضافی */
background-color: #f0f0f0; /* رنگ پسزمینه */
}
/* کانتینر iframe */
.iframe-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh; /* ارتفاع کامل viewport */
overflow: hidden; /* مخفی کردن بخشهای اضافی */
z-index: 1; /* ترتیب نمایش */
}
/* تنظیمات iframe */
.iframe-container iframe {
position: fixed;
top: -227px; /* پنهان کردن ۶ سانتیمتر بالای صفحه (227px) */
left: 0;
width: 100%;
bottom: -38px; /* پنهان کردن ۱ سانتیمتر پایین صفحه (38px) */
border: none;
display: none; /* مخفی کردن iframe تا بارگذاری موفق */
}
/* Overlay برای مخفی کردن بخش بالایی صفحه */
.overlay-top {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 227px; /* ارتفاع ۶ سانتیمتر (227px) */
background-color: transparent; /* رنگ پسزمینه شفاف */
z-index: 900; /* زیر iframe-container */
pointer-events: none; /* اجازه دادن به اسکرول از طریق iframe */
cursor: pointer; /* تغییر نشانگر موس برای نشان دادن امکان کلیک */
}
/* Overlay برای مخفی کردن بخش پایینی صفحه */
.overlay-bottom {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 38px; /* ارتفاع ۱ سانتیمتر (38px) */
background-color: transparent; /* رنگ پسزمینه شفاف */
z-index: 900; /* زیر iframe-container */
pointer-events: none; /* اجازه دادن به اسکرول از طریق iframe */
cursor: pointer; /* تغییر نشانگر موس برای نشان دادن امکان کلیک */
}
/* لودینگ */
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* مرکزیت عمودی و افقی */
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
z-index: 2000; /* بالاتر از iframe-container و overlays */
display: flex;
flex-direction: column;
align-items: center;
max-width: 90%; /* اطمینان از نمایش در دستگاههای کوچک */
box-sizing: border-box;
}
/* Media Query برای موبایل */
@media screen and (max-width: 768px) {
.loading {
width: 80%;
}
}
.loading-message {
margin-bottom: 10px;
font-size: 18px;
text-align: center;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 0.25s;
}
.error-message {
display: none;
color: red;
text-align: center;
margin-bottom: 10px;
font-size: 16px;
}
#retryButton {
display: none;
padding: 10px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#retryButton:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<!-- لودینگ -->
<div class="loading" id="loading">
<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 -->
<div class="iframe-container">
<iframe id="myIframe" src="https://yuntian-deng-o1.hf.space" onload="iframeLoaded()" onerror="iframeError()" scrolling="yes"></iframe>
</div>
<!-- Overlay برای مخفی کردن بخش بالایی صفحه -->
<div class="overlay-top" id="overlayTop"></div>
<!-- Overlay برای مخفی کردن بخش پایینی صفحه -->
<div class="overlay-bottom" id="overlayBottom"></div>
<script>
// JavaScript مربوط به مدیریت لودینگ و iframe و اسکرول
// کد شما بدون تغییر باقی میماند
// ...
</script>
</body>
</html>
توضیحات اضافی:
- با تغییر
position به fixed در iframe و تنظیم top و bottom به مقادیر منفی، iframe همیشه بخشهای مورد نظر را پوشش میدهد و بخشهای بالایی و پایینی آن توسط overlayها مخفی میشوند.
- با تنظیم
position: fixed و bottom: 0 در .overlay-bottom، overlay پایین همیشه در پایین صفحه باقی میماند، حتی زمانی که ارتفاع viewport تغییر میکند.
- اطمینان حاصل کنید که
z-index های المانها به درستی تنظیم شدهاند تا overlayها بر روی iframe قرار گیرند.
نکته: اگر باز هم با مشکلی مواجه شدید، ممکن است نیاز باشد سایر تنظیمات CSS یا JavaScript را بررسی کنید تا مطمئن شوید تداخلی وجود ندارد.
امیدوارم این تغییرات مشکل شما را حل کند. اگر سؤال یا مشکلی داشتید، در خدمت شما هستم.