<img alt="" src={Url}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
/>
事件
let flag = false
const handleMouseDown = () => {
flag = true
console.log('down')
}
const handleMouseMove = () => {
if (flag) {
console.log('move')
}
}
const handleMouseUp = () => {
console.log('up')
flag = false
}
想要实现从图片中选取一个区域,当按下鼠标时可以监听到,按着移动和松开都监听不到。在原地按下松开可以监听到。
请大家帮忙看看
import React from 'react'
import PropTypes from 'prop-types'
import styles from './Poster.less'
class PosterDemo extends React.Component {
constructor (props) {
super(props)
this.state = {
}
}
handleMouseDown = (event) => {
let target = event.target
console.log('down')
document.onmousemove = () => {
event.preventDefault()
console.log('move')
}
target.onmouseup = () => {
console.log('up')
document.onmousemove = null
target.onmouseup = null
}
}
render () {
return (
<div className={styles.posterDemo}>
<div id="phone" className={styles.phone}>
<div className={styles.Content}>
<div className={styles.Image}>
<img id="poster" alt="" src={this.props.url}
onMouseDown={(e) => this.handleMouseDown(e)}
/>
</div>
</div>
</div>
</div>
)
}
}
PosterDemo.propTypes = {
url: PropTypes.string,
}
export default PosterDemo
首先onmousemove的对象应该是document,其次onMouseMove和onMouseUp应该都是写在onMouseDown里面的
给你个拖拽的小例子吧,大致原理一样的