Taro抽屜動(dòng)畫(huà)

使用組件
<Consult consultHeight={consultHeight} />
index.jsx

import React, { useState, useEffect } from "react"
import Taro from "@tarojs/taro"
import { View, Image, ScrollView } from "@tarojs/components"
import { AtTabs, AtTabsPane, AtLoadMore } from "taro-ui"
import defaultIcon from "@images/ic_default.png"
import chuancai from "@images/top/ic_chuancai.svg"
import jiangzhecai from "@images/top/ic_jiangzhecai.svg"
import recommend from "@images/top/ic_recommend.svg"
import Http from "@/utils/http"
import { getPageHeight } from "@/utils/index"
import "./index.scss"

var touchStartX = 0 //觸摸時(shí)的原點(diǎn)
var touchStartY = 0 //觸摸時(shí)的原點(diǎn)
var time = 0 // 時(shí)間記錄,用于滑動(dòng)時(shí)且時(shí)間小于1s則執(zhí)行左右滑動(dòng)
var interval = "" // 記錄/清理時(shí)間記錄
var touchEndX = 0 // x軸方向移動(dòng)的距離
var touchEndY = 0 // y軸方向移動(dòng)的距離
var topHeight = 0
export default function Consult({ consultHeight }) {
  const [tabList, setTabList] = useState([{ key: "top", title: "熱門(mén)餐廳" }])
  const [currentTab, setTabCurrent] = useState(0) //當(dāng)前tab
  const [scrollMove, setScrollMove] = useState(0) //默認(rèn)0向下1向上2
  const [currentLocation, setcCurrentLocation] = useState("END") //當(dāng)前熱門(mén)餐廳card位置
  const [restaList, setRestaList] = useState([])
  const [pageData, setPageData] = useState({
    current: 0,
    pageSize: 10,
    total: 0,
  })
  const [loadStatus, setLoadStatus] = useState("more")
  const [animationData, setAnimationData] = useState({})
  const [criticalValue, setVriticalValue] = useState(120)
  const duration = 500,
    animation = Taro.createAnimation({
      duration: 500,
      timingFunction: "ease",
    })

  const handleClick = c => {
    setTabCurrent(c)
    getRestaurantList({ typeCode: tabList[c].key }, true)
  }
  // 觸摸開(kāi)始事件
  const touchStart = function (e) {
    if (!restaList.length) return
    touchStartX = e.changedTouches[0].pageX // 獲取觸摸時(shí)的原點(diǎn)
    touchStartY = e.changedTouches[0].pageY // 獲取觸摸時(shí)的原點(diǎn)
    // 使用js計(jì)時(shí)器記錄時(shí)間
    interval = setInterval(function () {
      time++
    }, 100)
  }

  // 觸摸結(jié)束事件
  const touchEnd = function (e) {
    if (!restaList.length) return
    touchEndX = e.changedTouches[0].pageX // 獲取觸摸時(shí)的原點(diǎn)
    touchEndY = e.changedTouches[0].pageY // 獲取觸摸時(shí)的原點(diǎn)
    var moveX = touchEndX - touchStartX
    var moveY = touchEndY - touchStartY
    if (Math.sign(moveX) == -1) {
      moveX = moveX * -1
    }
    if (Math.sign(moveY) == -1) {
      moveY = moveY * -1
    }
    if (moveX <= moveY) {
      // 上下
      // 向上滑動(dòng)
      if (touchEndY - touchStartY <= -30 && time < 10) {
        console.log("向上滑動(dòng)")
        setScrollMove(2)
      }
      // 向下滑動(dòng)
      if (touchEndY - touchStartY >= 30 && time < 10) {
        console.log("向下滑動(dòng)")
        const query = Taro.createSelectorQuery()
        query.select(".restaurant-card").boundingClientRect()
        query.exec(function (res) {
          console.log(res[0].top)
          if (res[0].top >= criticalValue) {
            setScrollMove(1)
          }
        })
      }
    }
    clearInterval(interval) // 清除setInterval
    time = 0
  }
  // 熱門(mén)餐廳上拉動(dòng)畫(huà)
  const moveToTop = () => {
    animation.height(topHeight).step({
      duration: duration,
      timingFunction: "ease",
    })
    setcCurrentLocation("TOP")
    setScrollMove(0)
    setAnimationData(animation.export())
  }

  // 熱門(mén)餐廳下滑動(dòng)畫(huà)
  const moveToEnd = () => {
    animation.height(consultHeight).step({
      duration: duration,
      timingFunction: "ease",
    })
    setcCurrentLocation("END")
    setScrollMove(0)
    setAnimationData(animation.export())
  }
  // 獲取餐廳類型
  const getRestaurantType = async () => {
    const res = await Http.get("/diet/public/dict/restaurantType")
    let types = res.data.map(r => {
      r.title = r.value
      return r
    })
    setTabList([...tabList, ...types])
  }
  // 獲取餐廳列表
  const getRestaurantList = (query, tag) => {
    setRestaList([])
    setLoadStatus("loading")
    setVriticalValue(120)
    const params = {
      typeCode: "top",
      current: 1,
      pageSize: pageData.pageSize,
      ...query,
    }
    Http.get("/diet/restaurant/top", params).then(({ data }) => {
      const { list, current, pageSize, total } = data
      if (list.length) {
        let arr = tag ? [...list] : [...restaList, ...list]
        setRestaList(arr)
        setPageData({
          current: current,
          pageSize: pageSize,
          total: total,
        })
        setLoadStatus(total <= pageSize * current ? "noMore" : "more")
      } else {
        setLoadStatus("noMore")
      }
    })
  }
  const goToRestaurant = resa => {
    Taro.navigateTo({ url: `/pages/restaurantMenus/index?title=${resa.name}&id=${resa.id}` })
  }
  const handleInit = async () => {
    let pageHeight = await getPageHeight()
    topHeight = pageHeight - pageHeight * 0.2
    getRestaurantType()
    getRestaurantList({ typeCode: "top", current: 1 }, false)
  }

  useEffect(() => {
    handleInit()
  }, [])

  useEffect(() => {
    if (currentLocation === "END" && scrollMove === 2) {
      moveToTop()
    } else if (currentLocation === "TOP" && scrollMove === 1) {
      moveToEnd()
    }
  }, [scrollMove, currentLocation])
  useEffect(() => {
    moveToEnd()
  }, [consultHeight])

  const onScroll = e => {
    setVriticalValue(260)
  }
  const renderRestaurant = resta => {
    return (
      <View
        className="restaurant-card"
        key={resta.id}
        onClick={() => {
          goToRestaurant(resta)
        }}
      >
        <View className="card-title">
          <View className="title-name">
            <Image className="res-icon" src={resta.img ?? defaultIcon} />
            <View className="res-sum">
              <View className="res-name">{resta.name}</View>
              <View className="res-desc">
                <Image className="desc-icon" src={resta.cuisineCode === "jiangzhecai" ? jiangzhecai : chuancai} />
                {resta.cuisine}
              </View>
            </View>
          </View>
          <View className="title-like">
            <View className="like-desc">
              <Image className="desc-icon" src={recommend} />
              {resta.recommendScore}+
            </View>
            <View className="like-type">糖友推薦</View>
          </View>
        </View>
        {resta.topFoods.length ? (
          <>
            <View className="card-divider" />
            <View className="card-eated">
              <View className="eated-title">糖友吃過(guò)</View>
              <View className="eated-list">
                {resta.topFoods.map(f => (
                  <Image className="list-icon" key={f.id} src={f.img} />
                ))}
              </View>
            </View>
          </>
        ) : (
          ""
        )}
      </View>
    )
  }
  return (
    <View className="hot-restaurant" onTouchStart={touchStart} onTouchEnd={touchEnd}>
      <AtTabs current={currentTab} tabList={tabList} onClick={handleClick} scroll>
        {tabList.map((tab, index) => {
          return (
            <AtTabsPane current={currentTab} key={tab.key} index={index}>
              <ScrollView
                animation={animationData}
                lowerThreshold={100}
                onScrollToLower={() => {
                  if (restaList.length < pageData.total) {
                    getRestaurantList({ typeCode: tabList[currentTab].key, current: pageData.current + 1 }, false)
                  }
                }}
                scrollY={currentLocation === "TOP"}
                className="hot-content"
                style={{ height: consultHeight }}
                onScroll={onScroll}
              >
                {restaList.map(resta => renderRestaurant(resta))}
                <AtLoadMore status={loadStatus} />
              </ScrollView>
            </AtTabsPane>
          )
        })}
      </AtTabs>
    </View>
  )
}

index.scss

.hot-restaurant {
  box-sizing: border-box;
  background-color: #fbfbfb;
  width: 100%;
  // min-height: 530rpx;
  max-height: 100vh;
  border-radius: 40rpx 40rpx 0 0;
  position: fixed;
  padding: 30rpx 48rpx 0 48rpx;
  bottom: 0;
  left: 0;
  background: linear-gradient(180deg, #ffffff 0%, #ffffff 39%, #f2f2f2 100%);
  box-shadow: 0rpx -12rpx 24rpx 0rpx rgba(0, 0, 0, 0.02);
  z-index: 2;
}
.at-tabs__header {
  text-align: left;
  background-color: transparent;
  margin-left: -24rpx;
}
.at-tabs__item {
  font-size: 28rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #555555;
}
.at-tabs__underline {
  display: none;
}
.at-tabs__item-underline {
  width: 76%;
  margin-left: 12%;
  height: 8rpx;
  background-color: #ff941a;
  bottom: 27rpx;
  opacity: 0.7;
}
.at-tabs__item--active {
  font-size: 36rpx;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #181818;
}
.hot-content {
  box-sizing: border-box;
  padding: 10rpx 0rpx 0rpx 0rpx;
  text-align: center;

  .at-button--full {
    border: none;
    font-size: 28rpx;
  }
}
.restaurant-card {
  box-sizing: border-box;
  width: 100%;
  // height: 336rpx;
  background: #ffffff;
  border-radius: 32rpx;
  padding: 24rpx;
  margin-bottom: 24rpx;
  box-shadow: 0rpx 8rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
}
.card-title {
  height: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.title-name {
  flex-shrink: 1;
  height: 100%;
  flex: 1;
  display: flex;
  justify-content: flex-start;
}
.res-icon {
  background-color: #f5f5f5;
  width: 120rpx;
  height: 120rpx;
  border-radius: 20rpx;
  margin-right: 16rpx;
}
.res-sum {
  height: 120rpx;
  font-size: 34rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #181818;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
}

.res-name {
  width: 280rpx;
  text-align: left;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.res-desc {
  font-size: 26rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #555555;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-top: 4rpx;
}
.desc-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 4rpx;
}
.title-like {
  width: 172rpx;
  height: 104rpx;
  background: #f8f8f8;
  border-radius: 20rpx 20rpx 20rpx 20rpx;
  opacity: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.like-type {
  font-size: 28rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #555555;
}
.like-desc {
  font-size: 28rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #555555;
  display: flex;
  justify-content: center;
  align-items: center;
}
.card-divider {
  width: 100%;
  height: 0rpx;
  opacity: 0.7;
  border-bottom: 1rpx solid #e1e1e1;
}
.card-eated {
  height: 50%;
}
.eated-title {
  font-size: 24rpx;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #555555;
  text-align: left;
  margin: 10rpx 0;
}
.eated-list {
  display: flex;
  justify-content: flex-start;
}
.list-icon {
  width: 88rpx;
  height: 88rpx;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末聚凹,一起剝皮案震驚了整個(gè)濱河市细溅,隨后出現(xiàn)的幾起案子慎宾,更是在濱河造成了極大的恐慌鲫忍,老刑警劉巖舰始,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件第焰,死亡現(xiàn)場(chǎng)離奇詭異果录,居然都是意外死亡上枕,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)雕憔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)姿骏,“玉大人,你說(shuō)我怎么就攤上這事斤彼》质荩” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵琉苇,是天一觀的道長(zhǎng)嘲玫。 經(jīng)常有香客問(wèn)我,道長(zhǎng)并扇,這世上最難降的妖魔是什么去团? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮穷蛹,結(jié)果婚禮上土陪,老公的妹妹穿的比我還像新娘。我一直安慰自己肴熏,他們只是感情好鬼雀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著蛙吏,像睡著了一般源哩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鸦做,一...
    開(kāi)封第一講書(shū)人閱讀 51,562評(píng)論 1 305
  • 那天励烦,我揣著相機(jī)與錄音,去河邊找鬼泼诱。 笑死坛掠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播却音,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼改抡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼矢炼!你這毒婦竟也來(lái)了系瓢?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤句灌,失蹤者是張志新(化名)和其女友劉穎夷陋,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體胰锌,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡骗绕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了资昧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酬土。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖格带,靈堂內(nèi)的尸體忽然破棺而出撤缴,到底是詐尸還是另有隱情,我是刑警寧澤叽唱,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布屈呕,位于F島的核電站,受9級(jí)特大地震影響棺亭,放射性物質(zhì)發(fā)生泄漏虎眨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一镶摘、第九天 我趴在偏房一處隱蔽的房頂上張望嗽桩。 院中可真熱鬧,春花似錦凄敢、人聲如沸碌冶。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)种樱。三九已至,卻和暖如春俊卤,著一層夾襖步出監(jiān)牢的瞬間嫩挤,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工消恍, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留岂昭,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓狠怨,卻偏偏與公主長(zhǎng)得像约啊,于是被迫代替她去往敵國(guó)和親邑遏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容