elementor-editor-scroll.js
51 lines
| 1 | (function () { |
| 2 | function getTargetElementId() { |
| 3 | var match = (window.location.hash || '').match(/elementor-element-([\w-]+)/); |
| 4 | return match ? match[1] : null; |
| 5 | } |
| 6 | |
| 7 | function findInPreviewIframe(elementId) { |
| 8 | const iframe = document.querySelector('#elementor-preview-iframe'); |
| 9 | if (!iframe || !iframe.contentWindow) return null; |
| 10 | |
| 11 | return iframe.contentWindow.document.getElementById(elementId); |
| 12 | } |
| 13 | |
| 14 | function scrollToElement(elementId) { |
| 15 | if (!elementId) return false; |
| 16 | |
| 17 | const target = findInPreviewIframe(elementId); |
| 18 | if (!target) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | if ( ! target ) return false; |
| 23 | |
| 24 | target.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | function initScroll() { |
| 29 | const elementId = getTargetElementId(); |
| 30 | if (!elementId) return; |
| 31 | |
| 32 | let attempts = 0; |
| 33 | |
| 34 | const interval = setInterval(() => { |
| 35 | if (scrollToElement(elementId) || attempts > 30) { |
| 36 | clearInterval(interval); |
| 37 | } |
| 38 | attempts++; |
| 39 | }, 500); |
| 40 | } |
| 41 | |
| 42 | if (window.elementor) { |
| 43 | window.elementor.on('preview:loaded', function () { |
| 44 | console.log('Elementor preview loaded'); |
| 45 | initScroll(); |
| 46 | }); |
| 47 | } else { |
| 48 | window.addEventListener('load', initScroll); |
| 49 | } |
| 50 | })(); |
| 51 |