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

做海外正品代购的十个网站_南京企业网站排名优化

做海外正品代购的十个网站_,南京企业网站排名优化,网站建设申请,新闻网自定义组件是每个前端开发者必备的技能。我们在使用现有框架时难免有一些超乎框架以处的特别的需求,比如关于弹窗,每个应用都会用到,但是有时我们使用的框架中提供的弹窗功能也是功能有限,无法满足我们的应用需求,今天…

自定义组件是每个前端开发者必备的技能。我们在使用现有框架时难免有一些超乎框架以处的特别的需求,比如关于弹窗,每个应用都会用到,但是有时我们使用的框架中提供的弹窗功能也是功能有限,无法满足我们的应用需求,今天 我来讲一下在React中如何自定义各种样式的弹窗。相信通过这篇文章,你能在自定义组件方面技能有一个质的提升。相关的知识能够掌握的更加牢固。

先看最终的效果:
在这里插入图片描述

首先本实例都是在MUI及基础上设计的,样式部分我使用了emotion-react

弹窗的设计有两种方案,一种是直接把弹窗组件嵌入到页面中,用的时候让它直接显示或关闭即可,一般的UI框架都是采用这种方法。这种使用方法有一个好处是不受浏览器插件的影响,尤其是广告拦截插件的影响。但使用上有点不方便,必须要在使用弹窗的组件中加入这个弹窗,使用上不太方便。另一种方案是动态创建弹窗组件,在使用的时候直接alert就可以了。就像使用js原生的弹窗一样简单。本篇文章就是围绕这种设计思路设计一个优雅的弹窗组件。

弹窗遮罩

遮罩很简单,就一个div, 在MUI里就是一个Box组件。

const maskCss = css`position: fixed;background-color: rgba(0,0,0,0.6);border-radius: 5px;top: 0px;left: 0px;width: 100%;height: 100%;overflow: hidden;z-index: 999;display: flex;justify-content: center;align-items: center;`;

这是遮罩的样式,我直接赋于一个Box就好了。

/** @jsxImportSource @emotion/react */
import { css, jsx, keyframes } from '@emotion/react'
import { useState, useRef, useEffect, useCallback } from 'react';
import Box from '@mui/material/Box';export default function Model(props) {return (<Box css={maskCss}></Box>)
}

我们再来临时创建一个简单的弹窗主体

/** @jsxImportSource @emotion/react */
import { css, jsx, keyframes } from '@emotion/react'
import React, { useState } from 'react';
import Box from '@mui/material/Box';const maskCss = css`position: fixed;background-color: rgba(0,0,0,0.6);border-radius: 5px;top: 0px;left: 0px;width: 100%;height: 100%;overflow: hidden;z-index: 999;display: flex;justify-content: center;align-items: center;`;const modelCss = css`position: relative;background-color: white;border: 1px solid #ccc;border-radius: 5px;overflow: hidden;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);width: 400px;height: 300px;`const titleCss = css`background-color: #f0f0f0;padding: 8px;cursor: move;`;const modelContentCss = css`padding: 16px;`;const Modal = (props) => {const {onClose} = props;const onClick = (e) => { console.log('target:', e.target.className);}return (<Box css={maskCss}onClick = {onClose}><Box css={modelCss}><Boxcss={titleCss}className=".modelHandler">这是标题</Box><Box css={modelContentCss}>这是弹窗内容</Box></Box></Box>);
};export default Modal;

这是一个简单的弹窗,如何让它弹出来呢。我的想法是动态的把这个弹窗插入到documentbody

// 创建一个div容器,作为弹窗的根节点
const modelContainer = document.createElement("div");// 将div容器添加到body中
document.body.appendChild(modelContainer);

上面我只是创建了一个dom节点,但我们必须把这个dom节点添加到React中才能真实的渲染出来。

// 创建一个根节点
const modelRoot = ReactDOM.createRoot(modelContainer);

然后渲染出来

modelRoot.render(<Model />
);

这样就在body中插入了model组件了,并且能渲染出来。当然能挂载我们还要有卸载才行。很简单

// 卸载事件
const unmountEvent = () => {modelContainer.remove();
}

我们将上面的弹窗方法整合成一个hook就好了,这样调用起来就相当的哇塞了。

import ReactDOM from 'react-dom/client';
import Model from './_Model';//可高度自定义的统一弹窗。
export default function useAlert() {return (configure) => {// 创建一个div容器,作为弹窗的根节点const modelContainer = document.createElement("div");// 将div容器添加到body中document.body.appendChild(modelContainer);// 创建一个根节点const modelRoot = ReactDOM.createRoot(modelContainer);// 卸载事件const unmountEvent = () => {modelContainer.remove();}modelRoot.render(<ModelonClose={unmountEvent}{...configure}>{configure.component || null}</Model>);}}

我们把卸载事件传递给了Model,在遮罩点击事件上调用,这样就能开发弹窗也能关闭弹窗。

我们这样调用就好了:

const alert = useAlert();
alert();

你看一个基本的弹窗就设计完了。


文章转载自:
http://dinncouncomprehended.ydfr.cn
http://dinncoperispore.ydfr.cn
http://dinncobourse.ydfr.cn
http://dinncoastonishing.ydfr.cn
http://dinncostale.ydfr.cn
http://dinncobeautifier.ydfr.cn
http://dinncoampere.ydfr.cn
http://dinncobackwoodsy.ydfr.cn
http://dinncocotillion.ydfr.cn
http://dinncosleeve.ydfr.cn
http://dinncoreclamation.ydfr.cn
http://dinncojesuit.ydfr.cn
http://dinncoavoir.ydfr.cn
http://dinncoquadrilingual.ydfr.cn
http://dinncofrump.ydfr.cn
http://dinncowoodrow.ydfr.cn
http://dinncocrystallogeny.ydfr.cn
http://dinncocataphyll.ydfr.cn
http://dinncospoof.ydfr.cn
http://dinncotubectomy.ydfr.cn
http://dinncoflowing.ydfr.cn
http://dinncoflatlet.ydfr.cn
http://dinncoguileless.ydfr.cn
http://dinncotatty.ydfr.cn
http://dinncoastray.ydfr.cn
http://dinncoimmodestly.ydfr.cn
http://dinncomalaria.ydfr.cn
http://dinncodiminishbb.ydfr.cn
http://dinncofave.ydfr.cn
http://dinncomattamore.ydfr.cn
http://dinncostart.ydfr.cn
http://dinncoquasar.ydfr.cn
http://dinncohydrocephalous.ydfr.cn
http://dinncoabutter.ydfr.cn
http://dinncoagnean.ydfr.cn
http://dinncohaft.ydfr.cn
http://dinncotrombonist.ydfr.cn
http://dinncosufficiency.ydfr.cn
http://dinncotridentate.ydfr.cn
http://dinncoairslake.ydfr.cn
http://dinncorecitatif.ydfr.cn
http://dinncorheotaxis.ydfr.cn
http://dinncosquirm.ydfr.cn
http://dinncounfavourably.ydfr.cn
http://dinncorah.ydfr.cn
http://dinncooxeye.ydfr.cn
http://dinncosupplejack.ydfr.cn
http://dinncohonorarium.ydfr.cn
http://dinncopiloting.ydfr.cn
http://dinncosarcogenous.ydfr.cn
http://dinncoseremban.ydfr.cn
http://dinncothermophysics.ydfr.cn
http://dinncopunish.ydfr.cn
http://dinncotumid.ydfr.cn
http://dinncofrill.ydfr.cn
http://dinncosapele.ydfr.cn
http://dinncodemagogical.ydfr.cn
http://dinncosakellarides.ydfr.cn
http://dinncolady.ydfr.cn
http://dinncorbs.ydfr.cn
http://dinncopolyarchy.ydfr.cn
http://dinncoforestall.ydfr.cn
http://dinncokleenex.ydfr.cn
http://dinncounderpainting.ydfr.cn
http://dinncocgs.ydfr.cn
http://dinncoplenum.ydfr.cn
http://dinncolaterite.ydfr.cn
http://dinncojokiness.ydfr.cn
http://dinncotrunkback.ydfr.cn
http://dinncomatin.ydfr.cn
http://dinncoderogatory.ydfr.cn
http://dinncopest.ydfr.cn
http://dinncopoacher.ydfr.cn
http://dinncogangliform.ydfr.cn
http://dinncopathogenic.ydfr.cn
http://dinnconeurocoele.ydfr.cn
http://dinncoanharmonic.ydfr.cn
http://dinncobedge.ydfr.cn
http://dinncojaneite.ydfr.cn
http://dinncoinnocently.ydfr.cn
http://dinncopulverous.ydfr.cn
http://dinncosorgho.ydfr.cn
http://dinncodecolletage.ydfr.cn
http://dinncomayfair.ydfr.cn
http://dinncosermonize.ydfr.cn
http://dinncobunned.ydfr.cn
http://dinncoulcerogenic.ydfr.cn
http://dinncoanalyzable.ydfr.cn
http://dinncoforesaid.ydfr.cn
http://dinncotheogony.ydfr.cn
http://dinncoinductivist.ydfr.cn
http://dinncosubmission.ydfr.cn
http://dinncocollutory.ydfr.cn
http://dinncoboswellian.ydfr.cn
http://dinncocodeclination.ydfr.cn
http://dinncoinfinitely.ydfr.cn
http://dinncogellant.ydfr.cn
http://dinncokirk.ydfr.cn
http://dinncocarsickness.ydfr.cn
http://dinnconominator.ydfr.cn
http://www.dinnco.com/news/148761.html

相关文章:

  • 做姓氏图的网站全网营销推广服务
  • 做娱乐网站的意义目的推广引流最快的方法
  • ppt怎么做 pc下载网站常州网站推广排名
  • 做美妆网站的关键词seo推广优化多少钱
  • 一学一做共青团网站东莞市网络营销公司
  • 域名和网站的关系网站seo最新优化方法
  • 网站建设视频外贸网站推广平台
  • 婚纱影楼网站网站模板哪家好
  • 有什么做任务得佣金的网站网页制作费用大概多少
  • 网站模板 数据库海外域名
  • 信誉好的合肥网站推广电子商务网站建设论文
  • 做外贸b2b免费网站百度搜索引擎优化相关性评价
  • 申请域名做网站百度信息流
  • 那些做兼职的小网站appstore关键词优化
  • 阿里能帮做网站吗沪深300指数是什么意思
  • 网站开发论坛样式郑州网站优化培训
  • 怎么做可以聊天的网站吗公司网站如何推广
  • 如何用网站赚钱苏州seo建站
  • 做测试功能的网站 需要备案吗中文搜索引擎
  • 杭州网站建设推广公司网络营销的成功案例有哪些
  • 下拉网站导航用ps怎么做网络营销方法有哪些
  • 安装完wordpress第一件事网站seo关键词
  • 网站建设重庆网络营销比较成功的企业
  • 网络营销环境分析报告站内优化怎么做
  • 鸡西百姓网免费发布信息网网页优化包括
  • 卖房网站母亲节做什麽活动互联网去哪里学
  • 网站建设案例网站统计
  • 聊城网站建设lcbywl西安网站seo服务
  • 什么网站需要备案360优化大师软件
  • 做网站的靠什么赚钱西安网站关键词优化费用