<p> </p> <style> /* تنظیمات پایه برای body و html / html, body { margin: 0;…
- Usercode
- Assistant
/* متغیر CSS برای ارتفاع نمایشگر */
:root {
--vh: 100%;
}
/* کانتینر دانلود تصویر */
.download-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: white;
padding: 10px 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
z-index: 3000; /* بالاتر از iframe و overlays */
box-sizing: border-box;
}
/* تنظیمات پایه برای فرآیند دانلود */
.input-container {
display: flex;
flex-direction: column;
align-items: center;
max-width: 1000px;
margin: 0 auto;
}
.input-box {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
resize: none;
box-sizing: border-box;
}
.input-box::placeholder {
color: #999;
font-size: 14px;
}
.message {
margin-top: 5px;
font-size: 12px;
color: #4CAF50;
text-align: center;
}
.download-link {
display: none;
margin-top: 10px;
padding: 8px 12px;
background-color: #4CAF50;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 14px;
text-align: center;
}
.download-link:hover {
background-color: #45a049;
}
.instructions {
display: none;
margin-top: 10px;
font-size: 12px;
color: #666;
text-align: center;
}
/* کانتینر iframe */
.iframe-container {
position: fixed;
top: 100px; /* ارتفاع کانتینر دانلود */
left: 0;
width: 100%;
height: calc(var(--vh) * 100 - 100px - 7.5px); /* ارتفاع کل صفحه منهای ارتفاع دانلود و مخفی پایین */
overflow: hidden; /* مخفی کردن بخشهای اضافی */
z-index: 1; /* ترتیب نمایش پایینتر از دانلود کانتینر */
}
/* تنظیمات iframe */
.iframe-container iframe {
position: absolute;
top: -150px; /* پنهان کردن ۴ سانتیمتر بالای صفحه (150px) */
left: 0;
width: 100%;
height: calc(var(--vh) * 100 + 50px); /* افزایش ارتفاع iframe به اندازه ۴ سانتیمتر بالا و ۰.۲ سانتیمتر پایین */
border: none;
display: none; /* مخفی کردن iframe تا بارگذاری موفق */
}
/* Overlay برای مخفی کردن بخش بالایی صفحه */
.overlay-top {
position: fixed;
top: 100px; /* بالا به اندازه کانتینر دانلود */
left: 0;
width: 100%;
height: 150px; /* ارتفاع ۴ سانتیمتر (150px) */
background-color: transparent; /* رنگ پسزمینه شفاف */
z-index: 900; /* زیر iframe-container */
pointer-events: none; /* اجازه دادن به اسکرول از طریق iframe */
}
/* Overlay برای مخفی کردن بخش پایینی صفحه */
.overlay-bottom {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 7.5px; /* ارتفاع ۰.۲ سانتیمتر (7.5px) */
background-color: transparent; /* رنگ پسزمینه شفاف */
z-index: 900; /* زیر iframe-container */
pointer-events: none; /* اجازه دادن به اسکرول از طریق iframe */
}
/* لودینگ */
.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%;
}
.download-container {
padding: 10px;
}
}
.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>
// تابع تنظیم متغیر CSS --vh به ارتفاع نمایشگر
function setVh() {
// محاسبه ارتفاع نمایشگر و تنظیم متغیر --vh
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
// فراخوانی تابع برای تنظیم متغیر vh
setVh();
// بهروزرسانی متغیر vh در تغییر اندازه صفحه
window.addEventListener('resize', setVh);
// عناصر DOM
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loading = document.getElementById('loading');
const iframe = document.getElementById('myIframe');
const overlayTop = document.getElementById('overlayTop');
const overlayBottom = document.getElementById('overlayBottom');
let progressInterval;
let errorTimeout;
let scrollPosition = 0;
const scrollStep = 100; // میزان اسکرول با هر کلیک
// تابع شروع پیشرفت نوار لودینگ
function startProgress() {
if (initialLoadCompleted) return; // جلوگیری از اجرای مجدد
progressInterval = setInterval(() => {
if (progress < 100) {
progress += 20; // افزایش ۲۰٪ در هر ثانیه
if (progress > 100) progress = 100;
progressBar.style.width = progress + '%';
}
if (progress >= 100) {
clearInterval(progressInterval);
if (!initialLoadCompleted) {
showError();
}
}
}, 1000);
// تایماوت برای نمایش پیام خطا بعد از ۵ ثانیه
errorTimeout = setTimeout(() => {
if (!initialLoadCompleted) {
showError();
}
}, 5000);
}
// تابع بارگذاری موفق iframe
function iframeLoaded() {
if (initialLoadCompleted) return;
initialLoadCompleted = true;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
loading.style.display = 'none';
iframe.style.display = 'block';
// پاک کردن تایماوتها و اینتروالها
progressInterval = null;
errorTimeout = null;
}
// تابع خطا در بارگذاری iframe
function iframeError() {
if (initialLoadCompleted) return;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
showError();
}
// نمایش خطا
function showError() {
errorMessage.style.display = 'block';
retryButton.style.display = 'block';
}
// بازنشانی لودینگ
function resetLoading() {
progress = 0;
progressBar.style.width = '0%';
initialLoadCompleted = false;
loading.style.display = 'flex';
errorMessage.style.display = 'none';
retryButton.style.display = 'none';
iframe.style.display = 'none';
clearInterval(progressInterval);
clearTimeout(errorTimeout);
// اضافه کردن دوباره رویدادها با once: true
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
}
// بارگذاری مجدد 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 با once: true
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
// شروع بارگذاری و لودینگ
startProgress();
// عناصر DOM برای دانلود تصویر
const inputBox = document.querySelector('.input-box');
const message = document.querySelector('.message');
const downloadLink = document.querySelector('.download-link');
const instructions = document.querySelector('.instructions');
// تابع بررسی معتبر بودن URL
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
// تابع مدیریت ورودی کاربر
function handleInput() {
const link = inputBox.value.trim();
if (isValidUrl(link)) {
message.textContent = 'لینک دانلود را گرفتم';
downloadLink.style.display = 'block';
downloadLink.href = link;
instructions.style.display = 'block';
instructions.innerHTML = 'برای دانلود تصویر:<br>' +
'1. روی دکمه "دانلود تصویر" کلیک کنید.<br>' +
'2. بعد از باز شدن مرورگر، روی تصویر محکم نگهدارید و "بارگیری تصویر" را انتخاب کنید.<br>' +
'3. تصویر داخل گوشی ذخیره میشود.';
} else {
message.textContent = '';
downloadLink.style.display = 'none';
instructions.style.display = 'none';
instructions.textContent = '';
}
}
// افزودن رویدادهای لازم به کادر ورودی
inputBox.addEventListener('input', handleInput);
inputBox.addEventListener('paste', handleInput);
inputBox.addEventListener('dragover', function(e) {
e.preventDefault();
});
inputBox.addEventListener('drop', function(e) {
e.preventDefault();
const link = e.dataTransfer.getData('text');
this.value = link;
handleInput();
});
downloadLink.addEventListener('click', function() {
// بازنشانی کامل فرم به حالت اولیه بدون نمایش هیچ پیامی
setTimeout(() => {
inputBox.value = '';
message.textContent = '';
downloadLink.style.display = 'none';
instructions.style.display = 'none';
instructions.textContent = '';
}, 100);
});
</script>
Your provided code sets up a comprehensive web page that incorporates several interactive elements, including a download section, a loading overlay with a progress bar, and an embedded iframe. Below is a detailed explanation of how each part functions, along with suggestions for improvements and best practices to enhance functionality, performance, and user experience.
The web page is structured into several key components:
Download Container: Allows users to input a URL to download an image, validates the URL, and provides a download link with instructions.
Loading Overlay: Displays a loading message and a progress bar while the iframe content is loading. If loading takes too long, it shows an error message with a retry option.
Iframe Container: Embeds an external website within an iframe. Initially hidden until the iframe successfully loads.
Overlay Elements: Transparent overlays at the top and bottom of the iframe to capture click events for scrolling the iframe content.
<!-- Download Container -->
<div class="download-container">
<!-- Input and Messages -->
</div>
<!-- Loading Overlay -->
<div class="loading" id="loading">
<!-- Loading Message, Progress Bar, Error Message, Retry Button -->
</div>
<!-- Iframe Container -->
<div class="iframe-container">
<iframe id="myIframe" src="https://yisol-idm-vton.hf.space/" scrolling="yes"></iframe>
</div>
<!-- Overlay Elements -->
<div class="overlay-top" id="overlayTop"></div>
<div class="overlay-bottom" id="overlayBottom"></div>
Recommendations:
Semantic HTML: Use more semantic tags for better accessibility and SEO. For example, use <header> for the download container and <main> for the main content.
Accessibility: Ensure all interactive elements (buttons, links) are keyboard accessible and have appropriate aria labels for screen readers.
The CSS provides responsive design, positioning, and visual styling for all components.
Highlights:
Responsive Design: Utilizes @media queries to adjust styles for different screen sizes, ensuring the page looks good on both desktop and mobile devices.
Fixed Positioning: Important elements like the download container and overlays are fixed to remain visible during scrolling.
Loading Overlay: Positioned centrally with a semi-transparent background to focus user attention during loading.
Recommendations:
CSS Variables: Utilize CSS variables more extensively for colors, spacing, and other repetitive values to facilitate easier theming and maintenance.
Reduce Specificity: Avoid overly specific selectors to make the CSS more maintainable and reduce the chance of conflicts.
Performance Optimization: Minimize the use of complex CSS properties that may hinder rendering performance, especially on mobile devices.
The JavaScript manages dynamic behaviors, including setting viewport height variables, handling iframe loading states, managing download link interactions, and scrolling within the iframe.
Key Functionalities:
Viewport Height Handling:
function setVh() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
window.addEventListener('resize', setVh);
setVh();
Loading and Progress Management:
function startProgress() { /* ... */ }
function iframeLoaded() { /* ... */ }
function iframeError() { /* ... */ }
function showError() { /* ... */ }
function resetLoading() { /* ... */ }
function loadIframe() { /* ... */ }
Scroll Handling via Overlays:
overlayTop.addEventListener('click', function(e) { /* ... */ });
overlayBottom.addEventListener('click', function(e) { /* ... */ });
Download Link Handling:
function isValidUrl(string) { /* ... */ }
function handleInput() { /* ... */ }
inputBox.addEventListener('input', handleInput);
inputBox.addEventListener('paste', handleInput);
inputBox.addEventListener('dragover', function(e) { /* ... */ });
inputBox.addEventListener('drop', function(e) { /* ... */ });
downloadLink.addEventListener('click', function() { /* ... */ });
Recommendations:
Error Handling Enhancements:
Security Considerations:
Cross-Origin Iframe Interactions:
iframe.contentWindow properties like scrollTo may be restricted due to cross-origin policies. Ensure that the embedded content allows such interactions or consider alternatives.postMessage API for safe cross-origin communication.Download Functionality Improvement:
download attribute or handle the download via JavaScript.downloadLink.addEventListener('click', function(event) {
event.preventDefault();
const link = inputBox.value.trim();
if (isValidUrl(link)) {
fetch(link)
.then(response => response.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = link.split('/').pop() || 'download';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
})
.catch(() => {
message.textContent = 'Failed to download the image.';
message.style.color = 'red';
});
}
});
To provide a more reliable download experience, especially for images, you can enhance the download functionality by programmatically fetching the image and triggering a download. Here's an improved approach:
// Enhanced Download Functionality
downloadLink.addEventListener('click', function(event) {
event.preventDefault();
const link = inputBox.value.trim();
if (isValidUrl(link)) {
fetch(link)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const fileName = link.split('/').pop() || 'download';
a.download = fileName;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
// Reset the form
inputBox.value = '';
message.textContent = '';
downloadLink.style.display = 'none';
instructions.style.display = 'none';
})
.catch(error => {
console.error('Download failed:', error);
message.textContent = 'Download failed. Please check the URL and try again.';
message.style.color = 'red';
});
}
});
Benefits:
Forced Download: The download attribute ensures that the image is downloaded rather than opened.
Error Handling: Provides feedback if the fetch request fails.
Resource Management: Revoke the object URL after the download to free up memory.
Testing Across Browsers and Devices: Ensure that all functionalities work seamlessly across different browsers (Chrome, Firefox, Safari, Edge) and devices (desktops, tablets, smartphones).
Performance Optimization: Minimize resource loading times by compressing assets, leveraging browser caching, and optimizing JavaScript execution.
Enhance User Experience:
Maintainability and Scalability:
Security Best Practices:
Accessibility Compliance:
By implementing these recommendations, you can enhance the functionality, performance, security, and user experience of your web page. If you encounter specific issues or need further assistance with particular aspects of your code, feel free to ask!
Not yet included in a published release.
Progress Bar Synchronization:
Code Organization:
Accessibility Enhancements: