当前位置: 首页 > news >正文

和初中生做视频网站百度导航下载安装手机导航

和初中生做视频网站,百度导航下载安装手机导航,昆山网页设计公司书生商友,苹果网站模板简介 本文将会基于react实现滚动菜单栏功能。 技术实现 实现效果 点击菜单,内容区域会自动滚动到对应卡片。内容区域滑动,指定菜单栏会被选中。 ScrollMenu.js import {useRef, useState} from "react"; import ./ScrollMenu.css;export co…

简介

        本文将会基于react实现滚动菜单栏功能。

技术实现

实现效果

       点击菜单,内容区域会自动滚动到对应卡片。内容区域滑动,指定菜单栏会被选中。

ScrollMenu.js
import {useRef, useState} from "react";
import './ScrollMenu.css';export const ScrollMenu = ({products}) => {// 获取 categoryProductMapconst categoryProductMap = new Map();products.forEach(product => {const category = product.category;let categoryProductList = categoryProductMap.get(category);if (!categoryProductList) {categoryProductList = [];}categoryProductList.push(product);categoryProductMap.set(category, categoryProductList);});// 获取类别列表const categoryList = Array.from(categoryProductMap.keys());// 菜单选中索引const [current, setCurrent] = useState(0);/*** 内容引用*/const contentRef = useRef();/*** 当左侧菜单点击时候*/const onMenuClick = (idx) => {if (idx !== current) {// 内容自动滚动到对应菜单位置contentRef.current.scrollTop = height.slice(0, idx).reduce((a, b) => a + b, 0);setCurrent(idx);}}/***  计算右侧商品类别卡片高度*/const height = [];const itemHeight = 25;categoryList.forEach((category, index) => {var productCnt = categoryProductMap.get(category).length;height.push((productCnt + 1) * itemHeight); // 0.8 是header高度});console.log(height)/*** 当右侧内容滚动时候*/const onContentScroll = () => {const scrollTop = contentRef.current.scrollTop;if (current < height.length - 1){const nextIdx = current + 1;// 计算下一个位置高度const nextHeight = height.slice(0, nextIdx).reduce((a, b) => a + b, 0);console.log('scrollTop', scrollTop, 'nextHeight', nextHeight, 'nextIdx', nextIdx)if (scrollTop >= nextHeight) {contentRef.current.scrollTop = nextHeight;setCurrent(nextIdx);return;}}if (current > 0) {const lastIdx = current - 1;// 计算上一个位置高度const lastHeight = height.slice(0, lastIdx).reduce((a, b) => a + b, 0);console.log('scrollTop', scrollTop, 'lastHeight', lastHeight, 'lastIdx', lastIdx)if (scrollTop <= lastHeight) {contentRef.current.scrollTop = lastHeight;setCurrent(lastIdx);return;}}}return (<div className='scroll-menu'><div className='menu'>{// 菜单列表categoryList.map((category, index) => {return (<div className={"menu-item" + ((index === current )? '-active' : '')}key={`${index}`} id={`menu-item-${index}`}onClick={(event) => {onMenuClick(index)}}>{category}</div>)})}</div><div className='content' ref={contentRef} onScroll={(event) => {onContentScroll()}}>{categoryList.map((category, index) => {// 获取类别商品const productList = categoryProductMap.get(category);return (<div key={index}><div className='content-item-header' key={`${index}`}id={`content-item-${index}`} style={{height: itemHeight}} >{category}</div>{productList.map((product,idx) => {return <div className='content-item-product'style={{height: itemHeight}}  key={`${index}-${idx}`} >{product.name}</div>})}</div>)})}</div></div>)
}
ScrollMenu.css
.scroll-menu {display: flex;flex-direction: row;width: 300px;height: 100px;
}.menu{width: 90px;height: 100px;display: flex;flex-direction: column;
}.menu-item {text-align: center;vertical-align: middle;}.menu-item-active {text-align: center;vertical-align: middle;background-color: lightcoral;
}.content {width: 210px;overflow: auto;
}.content-item-header{text-align: left;vertical-align: top;background-color: lightblue;
}.content-item-product{text-align: center;vertical-align: center;background-color: lightyellow;
}
App.js
import './App.css';
import {ScrollMenu} from "./component/scroll-menu/ScrollMenu";const App = ()=> {const products = [{category:'蔬菜',name:'辣椒'},{category:'蔬菜',name:'毛豆'},{category:'蔬菜',name:'芹菜'},{category:'蔬菜',name:'青菜'},{category:'水果',name:'苹果'},{category:'水果',name:'梨'},{category:'水果',name:'橘子'},   {category:'食物',name:'肉'},   {category:'食物',name:'罐頭'},   {category:'食物',name:'雞腿'}];return (<ScrollMenu products={products}/>)
}export default App;

http://www.wooajung.com/news/785.html

相关文章:

  • 广州网站建设怎样做吉林seo关键词
  • 建设网站几钱北京seo顾问服务
  • 广州论坛网站百度怎么发免费广告
  • 做软件测试的网站html网站模板免费
  • 做最好的导航网站百度公司电话是多少
  • 北京养老网站开发google框架一键安装
  • 顺义哪里有做网站设计的品牌推广手段
  • 淄博网站建设兼职腾讯云域名购买
  • 网站付费推广竞价网上接单平台
  • 临沂网站seo百度一下你就知道官网网址
  • 网站建设怎么申请域名网络营销的营销方式是什么
  • jquery动画特效网站seo关键词挖掘工具
  • 今日头条wordpress主题seo怎样才能优化网站
  • TP5.1做的网站首页被挂马原因最近一周的国内新闻
  • 烟台 o2o平台带动做网站行业简述网络推广的方法
  • 徐州建设工程招投标官方网站搜索引擎排名2020
  • 自家电脑做网站服务器w7花生壳互联网医疗的营销策略
  • 内网站做映射上海知名网站制作公司
  • 做竞价网站访问突然变少网站交易平台
  • 网站后台管理系统展望百度人工服务24小时热线电话
  • 设计制作一个网站seo岗位
  • 男人和女人做性网站优化教程网下载
  • 宜春网站建设公司哪家好seo外链增加
  • 备案做电影网站吗青岛网站建设公司哪家好
  • 成都航空公司官方网站免费网站安全检测
  • wordpress 软件下载主题优化关键词的方法正确的是
  • wordpress 图片放大seo页面优化技术
  • 数据库做后台网站长尾词优化外包
  • 网站的运行与维护推广运营平台
  • 网站打开慢怎么回事河北百度推广电话