当前位置: 首页 > 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.yidumall.com/news/7608.html

相关文章:

  • 空间除了可以做网站还能干什么苏州优化网站公司
  • js前端开发大众点评seo关键词优化
  • 有些网站做不了seo北京搜索引擎优化seo专员
  • 淄博网站制作建设优化网络营销的理解
  • WordPress如何恢复最初seo词库排行
  • 企业网站制作的公司石家庄网站关键词推广
  • 关于做花茶网站的策划书谷歌google官网
  • 专业网站建设微信商城开发如何进入网站
  • 选择邯郸网站制作网络培训心得体会总结
  • 上海品牌网站建设百度线上推广
  • 学校网站制作模板网络推广app
  • 昆山做网站需要多少钱南昌seo快速排名
  • 怎样在绍兴e网做网站网络优化工具app手机版
  • 设计一个简单的旅游网站游戏推广员怎么做
  • 霸州市建设局网站网络推广服务费
  • 个人网站注册什么域名新泰网站seo
  • 国内最最早做虚拟货币的网站在运营中seo是什么意思
  • 小白怎么做网站赚钱百度人工客服在线咨询
  • 做房产网站长营销型制作网站公司
  • 网页制作与网站建设实战大全光盘安徽网络优化公司
  • 做网站的p什么2003白酒最有效的推广方式
  • 网站建设记账做什么科目目录型搜索引擎有哪些
  • 互联网平台推广seo分析师招聘
  • 用dw做网站怎么单独修改字体德芙巧克力软文推广
  • 网站建设做的好处网站运营
  • 网站建设需要的费用南京百度网站推广
  • 长春兼职合肥seo招聘
  • 网站建设有哪些怎样制作网站教程
  • 手机网站是什么整站优化排名
  • 北京公司建网站一般需要多少钱看广告收益最高的软件