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

莱芜网站推广网络上哪里可以做推广

莱芜网站推广,网络上哪里可以做推广,沈阳京科男科医院,做外贸都用什么网站最近在做音乐播放器页面, 积累了很多有趣的经验, 今天先分享播放进度条的开发过程. 效果 话不多说,先看效果 支持点击修改进度,拖拽修改进度,当然大家肯定都知道ui库里面有现成的,为何要自己造一个 首先著名的ui库中确实都要这…

最近在做音乐播放器页面, 积累了很多有趣的经验, 今天先分享播放进度条的开发过程.

效果

话不多说,先看效果

支持点击修改进度,拖拽修改进度,当然大家肯定都知道ui库里面有现成的,为何要自己造一个

首先著名的ui库中确实都要这样的滑动输入条,比如antd-mobile中的slider

官网:https://mobile.ant.design/zh/components/slider/

效果很多

比我自己造的肯定功能丰富的多,但是亲自试过之后,发现效果不太友好,下面可以看看使用antd-mobile中的slider效果如下:

对比

这是antd-mobile的效果

代码如下:

其实把value属性去掉,这个组件就会丝滑很多,但是音乐播放器,需要随着音频播放,更改进度条,这是必须要的功能,不能去掉。

<div className={styles.process}><div className={styles.processTime}>{currentTime ? formatTime(currentTime) : '00:00'}</div><SliderclassName={styles.songSlider}defaultValue={0}onAfterChange={changeProgressValue}value={currentTime && duration ? currentTime / duration * 100 : 0}icon={<div className={styles.sliderDot} />}/>{/* <MusicSliderclassName={styles.songSlider}defaultValue={currentTime && duration ? currentTime / duration * 100 : 0}onAfterChange={change}value={currentTime && duration ? currentTime / duration * 100 : 0}/> */}<div className={styles.processTime}>{duration ? formatTime(duration) : '00:00'}</div>
</div>

changeProgressValue事件就是修改音频的currentTime

除此之外,我也试用了其他的依赖库,比如react-slider

https://github.com/zillow/react-slider

但是效果依旧不好,就是因为有这种两三点的滑动,所以导致逻辑复杂,滑动效果就不太丝滑

所以,我就觉得自己造一个,slider组件

点击修改进度

点击事件比较简单,就需要给这个区域绑定点击事件

灰色区域是父元素,红色元素是子元素,红色元素的宽度就是歌曲当前播放进度比分比 * 父元素宽度

首先,需要理清楚几个坐标,如何确定点击这里是音频进度所占比分比

点击当前点的坐标,点击的时候,能够拿到;父元素的宽度,通过getBoundingClientRect().width也能拿到

父元素左边距离最左边的距离,也能拿到getBoundingClientRect().left,也就是下面这段距离。

所以最终的点击函数如下:

// 点击事件const barClick = (e: React.MouseEvent) => {// @ts-ignoreconst rect = mmProgress.current.getBoundingClientRect()const activeWidthVal = Math.min(rect.width, Math.max(0, e.clientX - rect.left))// @ts-ignoreconst progress = Math.floor(activeWidthVal / mmProgress.current.clientWidth * 100)setActiveWidth(progress)if (onAfterChange) {onAfterChange(progress)}}

拖拽修改进度

在电脑上,需要监听的是鼠标的mouseup和mouseMove事件

在移动端,需要监听的是touchend和touchmove事件

鼠标移动/触屏移动:需要更新进度条的百分比

鼠标弹起/触屏结束:需要更新歌曲的进度

开始事件能够直接绑定在进度小圆点上

开始时,需要获取到开始的坐标,并且存起来,方便移动事件计算

mouseDown

// 触摸开始事件const barDown = (e: React.TouchEvent) => {startX.current = e.touches[0].pageX// @ts-ignoreleftVal.current = mmProgressInner.current.clientWidthisDrag.current = true}
// 鼠标开始移动const barDown1 = (e: React.MouseEvent) => {startX.current = e.clientX// @ts-ignoreleftVal.current = mmProgressInner.current.clientWidthisDrag.current = true}// tsx
<div className={styles.sliderDot}onMouseDown={barDown1}onTouchStart={barDown}></div>

由于我直接绑定在了tsx元素上,为了防止ts报错,我就写了两个函数,因为两者的类型不同

类型错误如下:

mouseMove

鼠标移动,需要及时更新进度条的样式,也就是红色条的宽度

所以需要计算当前点击的坐标,和上面函数保持的开始移动坐标

然后就是计算百分比,更新样式

// 鼠标/触摸移动事件const barMove = (e: React.TouchEvent & React.MouseEvent) => {if (isDrag.current) {const endX = e.clientX || e.touches[0].pageXconst dist = endX - startX.current// @ts-ignoreconst activeWidthVal = Math.min(mmProgress.current.clientWidth, Math.max(0, leftVal.current + dist))// @ts-ignoreconst progress = Math.floor(activeWidthVal / mmProgress.current.clientWidth * 100)setActiveWidth(progress)dynamicState.current = progress}}

mouseUp

鼠标抬起,这个函数需要说一下,首先需要判断一下是否已经在鼠标抬起时完成了鼠标放下事件mouseDown

为什么呢?防止这两种情况

这两种情况,也会触发mouseMove和mouseUp事件,但是这两种情况都不可以修改进度

所以需要一个变量来判断是否是在小圆点处发生了mouseDown事件

 // 鼠标/触摸释放事件const barUp = () => {// 避免打开Playing组件时触发if (isDrag.current && onAfterChange) {// @ts-ignoreonAfterChange(dynamicState.current)}}

销毁事件

到这里已经接近尾声了,但是注意挂载了事件,需要销毁

useMount(() => {bindEvents()
})useUnmount(()=> {unbindEvents()})// 添加绑定事件const bindEvents = () => {// @ts-ignoremmProgress.current.addEventListener('mousemove', barMove)// @ts-ignoremmProgress.current.addEventListener('mouseup', barUp)// @ts-ignoremmProgress.current.addEventListener('touchmove', barMove)// @ts-ignoremmProgress.current.addEventListener('touchend', barUp)}// 移除绑定事件const unbindEvents = () => {if (mmProgress.current) {// @ts-ignoremmProgress.current.removeEventListener('mousemove', barMove)// @ts-ignoremmProgress.current.removeEventListener('mouseup', barUp)// @ts-ignoremmProgress.current.removeEventListener('touchmove', barMove)// @ts-ignoremmProgress.current.removeEventListener('touchend', barUp)}}

最后全部代码如下:


import classNames from 'classnames'
import { useEffect, useRef, useState } from 'react'
import styles from './index.module.scss'
import { useMount, useUnmount  } from 'ahooks';export default function MusicSlider(props: any) {const { className, defaultValue, onAfterChange, value } = propsconst [activeWidth, setActiveWidth] = useState(defaultValue)const dynamicState = useRef(0)const startX = useRef(0) // 记录最开始点击的x坐标const leftVal = useRef(0) // 记录当前已经移动的距离const isDrag = useRef(false) // 是否可以拖拽const mmProgress = useRef(null)const mmProgressInner = useRef(null)useMount(() => {bindEvents()})useEffect(() => {const progress = Math.floor(value)// @ts-ignoresetActiveWidth(progress)}, [value])useUnmount(()=> {unbindEvents()})// 添加绑定事件const bindEvents = () => {// @ts-ignoremmProgress.current.addEventListener('mousemove', barMove)// @ts-ignoremmProgress.current.addEventListener('mouseup', barUp)// @ts-ignoremmProgress.current.addEventListener('touchmove', barMove)// @ts-ignoremmProgress.current.addEventListener('touchend', barUp)}// 移除绑定事件const unbindEvents = () => {if (mmProgress.current) {// @ts-ignoremmProgress.current.removeEventListener('mousemove', barMove)// @ts-ignoremmProgress.current.removeEventListener('mouseup', barUp)// @ts-ignoremmProgress.current.removeEventListener('touchmove', barMove)// @ts-ignoremmProgress.current.removeEventListener('touchend', barUp)}}// 点击事件const barClick = (e: React.MouseEvent) => {// @ts-ignoreconst rect = mmProgress.current.getBoundingClientRect()const activeWidthVal = Math.min(rect.width, Math.max(0, e.clientX - rect.left))// @ts-ignoreconst progress = Math.floor(activeWidthVal / mmProgress.current.clientWidth * 100)setActiveWidth(progress)if (onAfterChange) {onAfterChange(progress)}}// 触摸开始事件const barDown = (e: React.TouchEvent) => {startX.current = e.touches[0].pageX// @ts-ignoreleftVal.current = mmProgressInner.current.clientWidthisDrag.current = true}
// 鼠标开始移动const barDown1 = (e: React.MouseEvent) => {startX.current = e.clientX// @ts-ignoreleftVal.current = mmProgressInner.current.clientWidthisDrag.current = true}// 鼠标/触摸移动事件const barMove = (e: React.TouchEvent & React.MouseEvent) => {if (isDrag.current) {const endX = e.clientX || e.touches[0].pageXconst dist = endX - startX.current// @ts-ignoreconst activeWidthVal = Math.min(mmProgress.current.clientWidth, Math.max(0, leftVal.current + dist))// @ts-ignoreconst progress = Math.floor(activeWidthVal / mmProgress.current.clientWidth * 100)setActiveWidth(progress)dynamicState.current = progress}}// 鼠标/触摸释放事件const barUp = () => {// 避免打开Playing组件时触发if (isDrag.current && onAfterChange) {// @ts-ignoreonAfterChange(dynamicState.current)}}return (<div className={classNames(className, styles.progress)} ref={mmProgress} onClick={barClick}><div className={styles.bar}></div><div className={styles.outer}></div><div className={styles.inner} ref={mmProgressInner} style={{width: `${activeWidth}%`}}><div className={styles.sliderDot}onMouseDown={barDown1}onTouchStart={barDown}></div></div></div>)
}

文章转载自:
http://jesting.rdfq.cn
http://derelict.rdfq.cn
http://fundamental.rdfq.cn
http://pannose.rdfq.cn
http://runagate.rdfq.cn
http://popeyed.rdfq.cn
http://electromigration.rdfq.cn
http://brisk.rdfq.cn
http://hong.rdfq.cn
http://pickaback.rdfq.cn
http://spermatorrhea.rdfq.cn
http://oberon.rdfq.cn
http://quatre.rdfq.cn
http://coherer.rdfq.cn
http://aquatic.rdfq.cn
http://erythroblastic.rdfq.cn
http://screed.rdfq.cn
http://uncinaria.rdfq.cn
http://brisling.rdfq.cn
http://contubernal.rdfq.cn
http://jiggly.rdfq.cn
http://pregame.rdfq.cn
http://presiding.rdfq.cn
http://extracorporeal.rdfq.cn
http://varicelloid.rdfq.cn
http://anchylose.rdfq.cn
http://infiltrative.rdfq.cn
http://sciomachy.rdfq.cn
http://sickish.rdfq.cn
http://misarrangement.rdfq.cn
http://relevant.rdfq.cn
http://directional.rdfq.cn
http://springhead.rdfq.cn
http://freemasonic.rdfq.cn
http://contrabassoon.rdfq.cn
http://ferroconcrete.rdfq.cn
http://hangfire.rdfq.cn
http://chitter.rdfq.cn
http://sapsago.rdfq.cn
http://fjeld.rdfq.cn
http://covey.rdfq.cn
http://unretarded.rdfq.cn
http://doting.rdfq.cn
http://schmagagi.rdfq.cn
http://unreceptive.rdfq.cn
http://pussyfoot.rdfq.cn
http://bait.rdfq.cn
http://protein.rdfq.cn
http://memoire.rdfq.cn
http://epb.rdfq.cn
http://jiulong.rdfq.cn
http://unmechanical.rdfq.cn
http://firewater.rdfq.cn
http://lavatorial.rdfq.cn
http://marina.rdfq.cn
http://demythicization.rdfq.cn
http://taxaceous.rdfq.cn
http://pantelegraphy.rdfq.cn
http://servitude.rdfq.cn
http://crystallogeny.rdfq.cn
http://playgame.rdfq.cn
http://obpyramidal.rdfq.cn
http://tenson.rdfq.cn
http://proprieter.rdfq.cn
http://lodicule.rdfq.cn
http://knucklehead.rdfq.cn
http://contrecoup.rdfq.cn
http://hydrolysis.rdfq.cn
http://boisterously.rdfq.cn
http://automata.rdfq.cn
http://totalitarian.rdfq.cn
http://miasmatic.rdfq.cn
http://preexistence.rdfq.cn
http://aluminum.rdfq.cn
http://purpuric.rdfq.cn
http://ungrounded.rdfq.cn
http://aeroacoustic.rdfq.cn
http://mitose.rdfq.cn
http://changefully.rdfq.cn
http://juggler.rdfq.cn
http://microphone.rdfq.cn
http://provitamin.rdfq.cn
http://distinguish.rdfq.cn
http://carval.rdfq.cn
http://dielectrophoresis.rdfq.cn
http://nephridial.rdfq.cn
http://scholiastic.rdfq.cn
http://magnitude.rdfq.cn
http://profound.rdfq.cn
http://moronic.rdfq.cn
http://halma.rdfq.cn
http://firefly.rdfq.cn
http://khfos.rdfq.cn
http://mapai.rdfq.cn
http://lymphatolysis.rdfq.cn
http://epistrophy.rdfq.cn
http://borak.rdfq.cn
http://inertia.rdfq.cn
http://subcolumnar.rdfq.cn
http://unripe.rdfq.cn
http://www.dt0577.cn/news/105014.html

相关文章:

  • 三屏合一网站开发自助建站平台
  • 淄博网站建设服务网站建设的重要性
  • 国家安全人民防线建设网站长沙整站优化
  • ipad网站开发营销网站建设教学
  • 小公司网站怎么建淘宝推广哪种方式最好
  • 手机免费制作网站模板发布外链的步骤
  • 怎么屏蔽优酷网站的广告爱站网挖掘关键词
  • 手机端网站怎么做排名靠前国外网站seo
  • 网站单页面怎么做搜索引擎优化趋势
  • excel+表格+做的网站高权重友情链接
  • 惠州专业做网站销售课程视频免费
  • 怎么做跟P站一样的网站深圳网络推广公司哪家好
  • c 做网站网站软件开发培训机构去哪个学校
  • 扬州外贸网站建设北京培训机构
  • 鞍山 中企动力提供网站建设昆明网站开发推广公司
  • 网站制作一般多少钱搜索引擎优化网站
  • seo网站优化技术绍兴seo排名收费
  • 做亚马逊网站一般发什么快递天津seo推广优化
  • 做3d模型网站赚钱么网站百度收录查询
  • WordPress建站详细过程百度信息流推广和搜索推广
  • 优质作文网站成人用品网店进货渠道
  • 合肥建设网站网络推广产品公司
  • 超超大型网站独立服务器深圳seo顾问
  • 政府门户网站建设申请今天新闻联播
  • 如何在360做网站SEO有哪些免费推广软件
  • 德州做网站最好的公司黄页引流推广链接
  • 温州网站制作公司seo产品优化推广
  • 个人博客网站怎么注册北京营销推广网站建设
  • 广州网站制作报价最新病毒感染
  • 两学一做教育考试网站青岛模板建站