MainSwiper.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { View, Swiper, SwiperItem, Text } from "@tarojs/components";
  2. import './MainCard.scss';
  3. import MainDayNightCard from "./MainDayNightCard";
  4. import MainFastEatCard from "./MainFastEatCard";
  5. import MainSleepActiveCard from "./MainSleepActiveCard";
  6. import { useState } from "react";
  7. import { WindowType } from "@/utils/types";
  8. import { useDispatch, useSelector } from "react-redux";
  9. import { setTab } from "@/store/health";
  10. import { MainColorType } from "@/context/themes/color";
  11. import { rpxToPx } from "@/utils/tools";
  12. import dayjs from "dayjs";
  13. let scrollPage = 0;
  14. export default function MainSwiper(props: { count: number, pageChanged: Function, typeChanged: Function }) {
  15. const [current, setCurrent] = useState(0)
  16. const [type, setType] = useState(WindowType.day)
  17. const health = useSelector((state: any) => state.health);
  18. const [scale0, setScale0] = useState(1.0)
  19. const [scale1, setScale1] = useState(0.8)
  20. const [scale2, setScale2] = useState(0.8)
  21. const dispatch = useDispatch()
  22. function pageChanged(e) {
  23. const page = e.detail.current
  24. console.log(page)
  25. setCurrent(page)
  26. props.pageChanged(page)
  27. var strType = WindowType.day
  28. if (page == 0) {
  29. strType = WindowType.day
  30. }
  31. else if (page == 1) {
  32. strType = WindowType.fast
  33. }
  34. else {
  35. strType = WindowType.sleep
  36. }
  37. props.typeChanged(strType)
  38. dispatch(setTab(page % 3))
  39. }
  40. function typeChanged(e) {
  41. setType(e)
  42. props.typeChanged(e)
  43. }
  44. function onTransition(e) {
  45. let x = e.detail.dx
  46. let scale0 = 0
  47. let scale1 = 0
  48. let scale2 = 0
  49. const pageWidth = 143
  50. if (x >= -pageWidth && x <= pageWidth) {
  51. switch (scrollPage) {
  52. case 0:
  53. case 3:
  54. {
  55. scale0 = 1 - Math.abs(x) / pageWidth * 0.2
  56. scale1 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : 0.8
  57. scale2 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : 0.8
  58. }
  59. break;
  60. case 1:
  61. case 4:
  62. {
  63. scale0 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : 0.8
  64. scale1 = 1 - Math.abs(x) / pageWidth * 0.2
  65. scale2 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : 0.8
  66. }
  67. break;
  68. case 2:
  69. case 5:
  70. {
  71. scale0 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : 0.8
  72. scale1 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : 0.8
  73. scale2 = 1 - Math.abs(x) / pageWidth * 0.2
  74. }
  75. break;
  76. }
  77. }
  78. else if (x >= -pageWidth*2 && x <= pageWidth*2) {
  79. switch (scrollPage) {
  80. case 0:
  81. case 3:
  82. {
  83. scale1 = 1 - (Math.abs(x)-pageWidth) / pageWidth * 0.2
  84. scale2 = x > 0 ? 1 - (pageWidth - x+pageWidth) / pageWidth * 0.2 : 0.8
  85. scale0 = x < 0 ? 1 - (pageWidth + x+pageWidth) / pageWidth * 0.2 : 0.8
  86. }
  87. break;
  88. case 1:
  89. case 4:
  90. {
  91. scale1 = x < 0 ? 1 - (pageWidth + x+pageWidth) / pageWidth * 0.2 : 0.8
  92. scale2 = 1 - (Math.abs(x)-pageWidth) / pageWidth * 0.2
  93. scale0 = x > 0 ? 1 - (pageWidth - x+pageWidth) / pageWidth * 0.2 : 0.8
  94. }
  95. break;
  96. case 2:
  97. case 5:
  98. {
  99. scale1 = x > 0 ? 1 - (pageWidth - x+pageWidth) / pageWidth * 0.2 : 0.8
  100. scale2 = x < 0 ? 1 - (pageWidth + x+pageWidth) / pageWidth * 0.2 : 0.8
  101. scale0 = 1 - (Math.abs(x)-pageWidth) / pageWidth * 0.2
  102. }
  103. break;
  104. }
  105. }
  106. else if (x >= -pageWidth*3 && x <= pageWidth*3) {
  107. switch (scrollPage) {
  108. case 0:
  109. case 3:
  110. {
  111. scale2 = 1 - (Math.abs(x)-pageWidth*2) / pageWidth * 0.2
  112. scale0 = x > 0 ? 1 - (pageWidth - x+pageWidth*2) / pageWidth * 0.2 : 0.8
  113. scale1 = x < 0 ? 1 - (pageWidth + x+pageWidth*2) / pageWidth * 0.2 : 0.8
  114. }
  115. break;
  116. case 1:
  117. case 4:
  118. {
  119. scale2 = x < 0 ? 1 - (pageWidth + x+pageWidth*2) / pageWidth * 0.2 : 0.8
  120. scale0 = 1 - (Math.abs(x)-pageWidth*2) / pageWidth * 0.2
  121. scale1 = x > 0 ? 1 - (pageWidth - x+pageWidth*2) / pageWidth * 0.2 : 0.8
  122. }
  123. break;
  124. case 2:
  125. case 5:
  126. {
  127. scale2 = x > 0 ? 1 - (pageWidth - x+pageWidth*2) / pageWidth * 0.2 : 0.8
  128. scale0 = x < 0 ? 1 - (pageWidth + x+pageWidth*2) / pageWidth * 0.2 : 0.8
  129. scale1 = 1 - (Math.abs(x)-pageWidth*2) / pageWidth * 0.2
  130. }
  131. break;
  132. }
  133. }
  134. setScale0(scale0)
  135. setScale1(scale1)
  136. setScale2(scale2)
  137. }
  138. function onTransitionend(){
  139. scrollPage = current
  140. }
  141. return <View style={{ flexDirection: 'column', display: 'flex', alignItems: 'center' }}>
  142. <Text className="today">Today</Text>
  143. <Text className="main_date">{global.language == 'en' ? dayjs().format('dddd, MMM D') : dayjs().format('MMMD日, dddd')}</Text>
  144. {/* <View className="tabs">
  145. <View className="tab_item" style={{ backgroundColor: current != 0 ? '#B2B2B2' : isDay() ? MainColorType.day : MainColorType.night }} onClick={() => tapScroll(0)}></View>
  146. <View style={{ width: rpxToPx(40) }} />
  147. <View className="tab_item" style={{ backgroundColor: current != 1 ? '#B2B2B2' : isFast() ? MainColorType.fast : MainColorType.eat }} onClick={() => tapScroll(1)}></View>
  148. <View style={{ width: rpxToPx(40) }} />
  149. <View className="tab_item" style={{ backgroundColor: current != 2 ? '#B2B2B2' : isSleep() ? MainColorType.sleep : MainColorType.active }} onClick={() => tapScroll(2)}></View>
  150. </View> */}
  151. <Swiper
  152. onTransition={onTransition}
  153. onAnimationFinish={onTransitionend}
  154. style={{ height: 140, width: rpxToPx(750) }} circular
  155. previousMargin={rpxToPx(250)}
  156. nextMargin={rpxToPx(250)}
  157. onChange={pageChanged}
  158. current={current}>
  159. <SwiperItem>
  160. <MainDayNightCard scale={current==0?1:0.8} count={props.count} typeChanged={typeChanged} id={1} onClick={() => {
  161. setCurrent(0)
  162. }} />
  163. </SwiperItem>
  164. <SwiperItem>
  165. <MainFastEatCard scale={current==1?1:0.8} count={props.count} typeChanged={typeChanged} id={2} onClick={() => {
  166. setCurrent(1)
  167. }} />
  168. </SwiperItem>
  169. <SwiperItem>
  170. <MainSleepActiveCard scale={current==2?1:0.8} count={props.count} typeChanged={typeChanged} id={3} onClick={() => {
  171. setCurrent(2)
  172. }} />
  173. </SwiperItem>
  174. {/* <SwiperItem>
  175. <MainDayNightCard scale={current==3?1:0.8} count={props.count} typeChanged={typeChanged} id={7} onClick={() => {
  176. setCurrent(3)
  177. }} />
  178. </SwiperItem>
  179. <SwiperItem>
  180. <MainFastEatCard scale={current==4?1:0.8} count={props.count} typeChanged={typeChanged} id={8} onClick={() => {
  181. setCurrent(4)
  182. }} />
  183. </SwiperItem>
  184. <SwiperItem>
  185. <MainSleepActiveCard scale={current==5?1:0.8} count={props.count} typeChanged={typeChanged} id={9} onClick={() => {
  186. setCurrent(5)
  187. }} />
  188. </SwiperItem> */}
  189. </Swiper>
  190. {/* <View style={{flexDirection:'row',display:'flex'}}>
  191. <View style={{backgroundColor:current==0?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(0)}>
  192. <MainDayNightCard count={props.count} typeChanged={typeChanged} id={4}/>
  193. </View>
  194. <View style={{backgroundColor:current==1?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(1)}>
  195. <MainFastEatCard count={props.count} typeChanged={typeChanged} id={5}/>
  196. </View>
  197. <View style={{backgroundColor:current==2?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(2)}>
  198. <MainSleepActiveCard count={props.count} typeChanged={typeChanged} id={6}/>
  199. </View>
  200. </View> */}
  201. </View>
  202. }