iframe.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const iframe = document.getElementById("headerIframe");
  2. let observer = null
  3. const backcolor = {
  4. project: { background: '#F6FEF9', color: '#3C86DF', logo: './image/logos.jpg' },
  5. mobile: { background: 'none', color: '#000', logo: './image/logos.jpg' },
  6. about: { background: 'none', color: '#000', logo: './image/logos.jpg' },
  7. dynamic: { background: 'none', color: '#000', logo: './image/logos.jpg' },
  8. customerNew: { background: '#fff', color: '#000', logo: './image/logos.jpg' }
  9. }
  10. function removeListener() {
  11. if (observer) {
  12. observer.disconnect();
  13. }
  14. }
  15. function reinitIframe() {
  16. if (iframe) {
  17. console.log('开始执行')
  18. const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  19. const iframeBody = iframeDocument.body
  20. observer = new MutationObserver(function(mutationsList, observer) {
  21. const bodyHeight = iframeBody.clientHeight;
  22. iframe.style.height = `${bodyHeight + 1}px`;
  23. });
  24. observer.observe(iframeBody, { attributes: true, childList: true, subtree: true });
  25. }
  26. }
  27. function handleScroll() {
  28. const scrollYnma = window.scrollY
  29. const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  30. let imgElement = iframeDocument.getElementById('logo');
  31. let headerItems = iframeDocument.querySelectorAll('.header-item')
  32. const urls = window.location.href
  33. const filed = urls.match(/\/([^\/]+)\.html$/) && urls.match(/\/([^\/]+)\.html$/)[1];
  34. if(scrollYnma > 1) {
  35. iframe.style.background = '#fff'
  36. imgElement.src = './image/logos.jpg'
  37. iframe.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';
  38. headerItems.forEach(function(item) {
  39. item.style.color = '#000'; // 设置你想要的字体颜色
  40. });
  41. return
  42. }
  43. console.log(backcolor[filed], filed)
  44. iframe.style.background = (backcolor[filed] && backcolor[filed].background) || 'none'
  45. iframe.style.boxShadow = '0 0 0 rgba(0, 0, 0, 0)';
  46. imgElement.src = (backcolor[filed] && backcolor[filed].logo) || './image/logos.png'
  47. headerItems.forEach(function(item) {
  48. item.style.color = (backcolor[filed] && backcolor[filed].color) || '#fff'; // 设置你想要的字体颜色
  49. });
  50. }
  51. // 页面滚动事件
  52. document.addEventListener('scroll', handleScroll);
  53. iframe.onload = function() {
  54. reinitIframe();
  55. handleScroll()
  56. }
  57. window.addEventListener('beforeunload', function() {
  58. removeListener();
  59. document.removeEventListener('scroll', handleScroll);
  60. });
  61. // 设计稿尺寸
  62. function bodyScale() {
  63. var devicewidth = document.documentElement.clientWidth;
  64. var scale = devicewidth / 1920; // 设计稿的尺寸
  65. document.body.style.zoom = scale;
  66. }
  67. window.onload = window.onresize = function () {
  68. bodyScale();
  69. };