iframe.js 3.0 KB

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