iframe.js 2.9 KB

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