我有一个指向代码笔的链接,这个问题最好在chrome中观察到:
https://codepen.io/pkroupoderov/pen/jdRowv
当用户在多个图像上快速移动鼠标时,mouseleave事件有时不会触发,这意味着某些图像仍将应用灰度过滤器。怎么解决?如果我使用div而不是锚元素,那么它工作得很好。我应该稍微更改标记还是对锚定元素应用某些样式?或者我应该改用react bootstrap中的某个组件?
我正试图创建一个覆盖效果的图像时,用户悬停在一个就像在Instagram。稍后我会将内容添加到覆盖图中,我只需要解决mouseleave事件问题。

const imageUrls = [
  'https://farm8.staticflickr.com/7909/33089338628_052e9e2149_z.jpg',
  'https://farm8.staticflickr.com/7894/46240285474_81642cbd37_z.jpg',
  'https://farm8.staticflickr.com/7840/32023738937_17d3cec52f_z.jpg',
  'https://farm8.staticflickr.com/7815/33089272548_fbd18ac39f_z.jpg',
  'https://farm5.staticflickr.com/4840/40000181463_6eab94e877_z.jpg',
  'https://farm8.staticflickr.com/7906/46912640552_4a7c36da63_z.jpg',
  'https://farm5.staticflickr.com/4897/46912634852_93440c416a_z.jpg',
  'https://farm5.staticflickr.com/4832/46964511231_6da8ef5ed0_z.jpg'
]

class Image extends React.Component {
  state = {hovering: false}

  handleHover = () => {
    this.setState({hovering: true})
  }
  handleLeave = () => {
    this.setState({hovering: false})
  }

  render() {
    if (!this.state.hovering) {
      return (
        <div onMouseOver={this.handleHover}>
          <img src={this.props.url} alt='' />
        </div>
      )
    } else {
      return ( // works fine when using <div> tag instead of <a> or <span>
        <a href="#" style={{display: 'block'}} onMouseLeave={this.handleLeave}>
          <img style={{filter: 'opacity(20%)'}} src={this.props.url} alt='' />
        </a>
      )
    }
  }
}

const Images = () => {
  return (
    <div className="gallery">
      {imageUrls.map((image, i) => {
        return <Image key={i} url={image} />
      })}
    </div>
  )
}

ReactDOM.render(<Images />, document.getElementById('app'))

最佳答案

您可以使用纯css实现这一点,无需使用javascript。

a {
   display: block;
}

a:hover {
   filter: opacity(.2);
}

07-28 05:25