CustomBg.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * 用于 page 根目录的样式等设置
  3. */
  4. import { View, Video } from "@tarojs/components";
  5. import React, { useEffect, useState } from "react";
  6. import pageStyle from "./index.module.less";
  7. import Taro from "@tarojs/taro";
  8. // import { useAppStore } from "@/store/appStore";
  9. interface Props {
  10. styleBg?: string|null;
  11. }
  12. const Index: React.FC<Props> = ({styleBg}) => {
  13. // 自定义背景样式
  14. // const {windowHeight, windowWidth} = useAppStore()
  15. // const [offsetMargin, setOffsetMargin] = useState({ x: 0, y: 0 });
  16. // const [bgSize, setBgSize] = useState({ width: windowWidth, height: windowHeight });
  17. const bg = styleBg || ''
  18. const bgImageStyle = {
  19. backgroundImage: `url(${bg})`,
  20. // marginLeft: `${offsetMargin.x}px`,
  21. // marginTop: `${offsetMargin.y}px`,
  22. // height: `${bgSize.height}px`,
  23. // width: `${bgSize.width}px`,
  24. backgroundSize: 'auto 100%',
  25. backgroundPosition: 'center center'
  26. };
  27. const isMp4 = bg?.lastIndexOf('.mp4') > -1
  28. // useEffect(()=> {
  29. // if(!bg) {
  30. // return;
  31. // }
  32. // if(isMp4){
  33. // return
  34. // }
  35. // Taro.getImageInfo({
  36. // src: bg,
  37. // success(res) {
  38. // const imgWidth = res.width;
  39. // const imgHeight = res.height;
  40. // let ratio = windowHeight / imgHeight;
  41. // let scaledWidth = imgWidth * ratio;
  42. // let scaledHeight = windowHeight;
  43. // let offsetX = 0;
  44. // let offsetY = 0;
  45. // if (scaledWidth >= windowWidth) {
  46. // offsetX = (scaledWidth - windowWidth) / 2;
  47. // offsetY = 0;
  48. // } else {
  49. // ratio = windowWidth / imgWidth;
  50. // scaledWidth = windowWidth;
  51. // scaledHeight = imgHeight * ratio;
  52. // offsetX = 0;
  53. // offsetY = (scaledHeight - windowHeight) / 2;
  54. // }
  55. // setOffsetMargin({ x: -offsetX, y: -offsetY });
  56. // setBgSize({ width: scaledWidth, height: scaledHeight });
  57. // },
  58. // fail(err) {
  59. // console.error('获取图片信息失败', err)
  60. // }
  61. // })
  62. // }, [styleBg])
  63. if(!bg) {
  64. return <></>
  65. }
  66. if(isMp4){
  67. return <Video
  68. controls={false}
  69. showCenterPlayBtn={false}
  70. loop={true}
  71. muted={true}
  72. autoplay
  73. objectFit="cover"
  74. className={pageStyle.bg}
  75. src={bg}
  76. />
  77. }
  78. return <View className={pageStyle.bg} style={bgImageStyle}></View>
  79. };
  80. export default Index;