mirror of
https://github.com/kevinveenbirkenbach/homepage.veen.world.git
synced 2025-07-07 11:35:12 +02:00
Finished resize implementation for iframe
This commit is contained in:
parent
b8aad8b695
commit
f0d4206731
@ -1,52 +1,82 @@
|
|||||||
// Global variables to store elements and original state
|
// Global variables to store elements and original state
|
||||||
let mainElement, originalContent, originalMainStyle, container, customScrollbar;
|
let mainElement, originalContent, originalMainStyle, container, customScrollbar, scrollbarContainer;
|
||||||
|
|
||||||
// Function to open a URL in an iframe using global variables
|
// Synchronize the height of the iframe to match the scroll-container or main element
|
||||||
|
function syncIframeHeight() {
|
||||||
|
const iframe = mainElement.querySelector("iframe");
|
||||||
|
if (iframe) {
|
||||||
|
console.log("Setting iframe height based on scroll-container inline styles...");
|
||||||
|
if (scrollbarContainer) {
|
||||||
|
// Prefer inline height, otherwise inline max-height
|
||||||
|
const inlineHeight = scrollbarContainer.style.height;
|
||||||
|
const inlineMax = scrollbarContainer.style.maxHeight;
|
||||||
|
const target = inlineHeight || inlineMax;
|
||||||
|
if (target) {
|
||||||
|
console.log("Using scroll-container inline style:", target);
|
||||||
|
iframe.style.height = target;
|
||||||
|
} else {
|
||||||
|
console.warn("No inline height or max-height set on scroll-container. Using main element height instead.");
|
||||||
|
iframe.style.height = mainElement.style.height;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("No scroll-container found. Using main element height:", mainElement.style.height);
|
||||||
|
iframe.style.height = mainElement.style.height;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("No iframe to resize.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to open a URL in an iframe
|
||||||
function openIframe(url) {
|
function openIframe(url) {
|
||||||
// Set a fixed height for the main element if not already set
|
// Hide the container (and its scroll-container) so the iframe can appear in its place
|
||||||
if (!mainElement.style.height) {
|
if (scrollbarContainer) {
|
||||||
mainElement.style.height = `${mainElement.clientHeight}px`;
|
scrollbarContainer.style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the container class with container-fluid if not already applied
|
// Hide any custom scrollbar element if present
|
||||||
if (container && !container.classList.contains("container-fluid")) {
|
|
||||||
container.classList.replace("container", "container-fluid");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hide the custom scrollbar
|
|
||||||
if (customScrollbar) {
|
if (customScrollbar) {
|
||||||
customScrollbar.style.display = "none";
|
customScrollbar.style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if an iframe already exists in the main element
|
// Create or retrieve the iframe in the main element
|
||||||
let iframe = mainElement.querySelector("iframe");
|
let iframe = mainElement.querySelector("iframe");
|
||||||
if (!iframe) {
|
if (!iframe) {
|
||||||
// Create a new iframe element
|
|
||||||
iframe = document.createElement("iframe");
|
iframe = document.createElement("iframe");
|
||||||
iframe.width = "100%";
|
iframe.width = "100%";
|
||||||
iframe.style.border = "none";
|
iframe.style.border = "none";
|
||||||
iframe.style.height = mainElement.style.height; // Apply fixed height
|
iframe.style.overflow = "auto"; // Enable internal scrolling
|
||||||
iframe.style.overflow = "auto"; // Enable internal scrollbar
|
iframe.scrolling = "auto";
|
||||||
iframe.scrolling = "auto"; // Ensure scrollability
|
|
||||||
|
|
||||||
// Clear the main content before appending the iframe
|
|
||||||
mainElement.innerHTML = "";
|
|
||||||
mainElement.appendChild(iframe);
|
mainElement.appendChild(iframe);
|
||||||
|
syncIframeHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the URL of the iframe
|
// Set the iframe's source URL
|
||||||
iframe.src = url;
|
iframe.src = url;
|
||||||
|
|
||||||
// Update the browser URL without reloading the page
|
// Push the new URL state without reloading the page
|
||||||
const newUrl = new URL(window.location);
|
const newUrl = new URL(window.location);
|
||||||
newUrl.searchParams.set('iframe', url);
|
newUrl.searchParams.set('iframe', url);
|
||||||
window.history.pushState({ iframe: url }, '', newUrl.toString());
|
window.history.pushState({ iframe: url }, '', newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to restore the original main content and style
|
// Function to restore the original content and show the container again
|
||||||
function restoreOriginal() {
|
function restoreOriginal() {
|
||||||
// Restore the original content of the main element (removing the iframe)
|
// Remove the iframe from the DOM
|
||||||
mainElement.innerHTML = originalContent;
|
const iframe = mainElement.querySelector("iframe");
|
||||||
|
if (iframe) {
|
||||||
|
iframe.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the original container
|
||||||
|
if (scrollbarContainer) {
|
||||||
|
scrollbarContainer.style.display = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore any custom scrollbar
|
||||||
|
if (customScrollbar) {
|
||||||
|
customScrollbar.style.display = '';
|
||||||
|
}
|
||||||
|
|
||||||
// Restore the original inline style of the main element
|
// Restore the original inline style of the main element
|
||||||
if (originalMainStyle !== null) {
|
if (originalMainStyle !== null) {
|
||||||
@ -55,55 +85,41 @@ function restoreOriginal() {
|
|||||||
mainElement.removeAttribute("style");
|
mainElement.removeAttribute("style");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert the container class back to "container" if needed
|
// Update the URL to remove the iframe parameter
|
||||||
if (container && container.classList.contains("container-fluid")) {
|
|
||||||
container.classList.replace("container-fluid", "container");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show the custom scrollbar again
|
|
||||||
if (customScrollbar) {
|
|
||||||
customScrollbar.style.display = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adjust scroll container height if that function exists
|
|
||||||
if (typeof adjustScrollContainerHeight === "function") {
|
|
||||||
adjustScrollContainerHeight();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the URL to remove the iframe param
|
|
||||||
const newUrl = new URL(window.location);
|
const newUrl = new URL(window.location);
|
||||||
newUrl.searchParams.delete("iframe");
|
newUrl.searchParams.delete("iframe");
|
||||||
window.history.pushState({}, '', newUrl.toString());
|
window.history.pushState({}, '', newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize event listeners after DOM content is loaded
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
// Initialize global variables
|
// Cache references to elements and original state
|
||||||
mainElement = document.querySelector("main");
|
mainElement = document.querySelector("main");
|
||||||
originalContent = mainElement.innerHTML;
|
originalContent = mainElement.innerHTML;
|
||||||
originalMainStyle = mainElement.getAttribute("style"); // might be null if no inline style exists
|
originalMainStyle = mainElement.getAttribute("style"); // may be null
|
||||||
|
|
||||||
container = document.querySelector(".container");
|
container = document.querySelector(".container");
|
||||||
customScrollbar = document.getElementById("custom-scrollbar");
|
customScrollbar = document.getElementById("custom-scrollbar");
|
||||||
|
scrollbarContainer = container.querySelector(".scroll-container")
|
||||||
|
|
||||||
// Set up click handlers for iframe links
|
// Attach click handlers to links that should open in an iframe
|
||||||
document.querySelectorAll(".iframe-link").forEach(link => {
|
document.querySelectorAll(".iframe-link").forEach(link => {
|
||||||
link.addEventListener("click", function(event) {
|
link.addEventListener("click", function(event) {
|
||||||
event.preventDefault(); // Prevent default link behavior
|
event.preventDefault(); // prevent full page navigation
|
||||||
openIframe(this.href);
|
openIframe(this.href);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set up click handler on header h1 to restore original state
|
// Clicking the header's H1 will restore the original view
|
||||||
const headerH1 = document.querySelector("header h1");
|
const headerH1 = document.querySelector("header h1");
|
||||||
if (headerH1) {
|
if (headerH1) {
|
||||||
headerH1.style.cursor = "pointer";
|
headerH1.style.cursor = "pointer";
|
||||||
headerH1.addEventListener("click", restoreOriginal);
|
headerH1.addEventListener("click", restoreOriginal);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until all resources are loaded before doing the initial iframe check
|
// On full page load, check URL parameters to auto-open an iframe
|
||||||
window.addEventListener("load", function() {
|
window.addEventListener("load", function() {
|
||||||
const initialParams = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
const iframeUrl = initialParams.get('iframe');
|
const iframeUrl = params.get('iframe');
|
||||||
if (iframeUrl) {
|
if (iframeUrl) {
|
||||||
openIframe(iframeUrl);
|
openIframe(iframeUrl);
|
||||||
}
|
}
|
||||||
@ -112,13 +128,15 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
|
|
||||||
// Handle browser back/forward navigation
|
// Handle browser back/forward navigation
|
||||||
window.addEventListener('popstate', function(event) {
|
window.addEventListener('popstate', function(event) {
|
||||||
const url = new URL(window.location);
|
const params = new URLSearchParams(window.location.search);
|
||||||
const iframeUrl = url.searchParams.get('iframe');
|
const iframeUrl = params.get('iframe');
|
||||||
|
|
||||||
if (iframeUrl) {
|
if (iframeUrl) {
|
||||||
openIframe(iframeUrl);
|
openIframe(iframeUrl);
|
||||||
} else {
|
} else {
|
||||||
const headerH1 = document.querySelector("header h1");
|
restoreOriginal();
|
||||||
if (headerH1) headerH1.click();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Adjust iframe height on window resize
|
||||||
|
window.addEventListener('resize', syncIframeHeight);
|
||||||
|
@ -58,6 +58,9 @@ function exitFullscreen() {
|
|||||||
if (typeof updateCustomScrollbar === 'function') {
|
if (typeof updateCustomScrollbar === 'function') {
|
||||||
try { updateCustomScrollbar(); } catch (e) { console.warn('updateCustomScrollbar failed:', e); }
|
try { updateCustomScrollbar(); } catch (e) { console.warn('updateCustomScrollbar failed:', e); }
|
||||||
}
|
}
|
||||||
|
if (typeof syncIframeHeight === 'function') {
|
||||||
|
try { syncIframeHeight(); } catch (e) { console.warn('syncIframeHeight failed:', e); }
|
||||||
|
}
|
||||||
// Remove URL parameter
|
// Remove URL parameter
|
||||||
const url = new URL(window.location);
|
const url = new URL(window.location);
|
||||||
url.searchParams.delete('fullscreen');
|
url.searchParams.delete('fullscreen');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user