我使用react js创建了聚光灯。我无法单击它下面的对象。我试图将pointerEvents:'none'应用于样式,但这不会引起人们的关注。如何使下面的对象可点击?
这是代码:-
import React from 'react';
export default class Spot extends React.Component {
constructor(props) {
super(props);
this.state = { x: 0, y: 0};
}
_onMouseMove(e) {
this.setState({ x: e.screenX , y: e.screenY });
}
render() {
const { x, y } = this.state;
return (
<div className={'spot'} onMouseMove={this._onMouseMove.bind(this)}
style={{background: `radial-gradient(${this.props.height}px
${this.props.width}px at ${x}px ${y}px , transparent 90%,
${this.props.color})`, top: '0', bottom: '0', left: '0', right:
'0', position: 'absolute'}}>
</div>
);
}
}
这是另一个组件:-
import React from 'react'
import Scene from '../components/Scene'
import Sobject from '../components/Object'
import Room from '../images/level1/Room.png'
import Spot from './Spotlight.js'
export default class Level1 extends React.Component {
constructor(props) {
super(props)
}
clickIt(){
console.log('room')
}
render() {
return (
<div>
<Scene>
<Sobject name={'room'} xPos={0} yPos={0}>
<img src={Room} height="725" width="1536" onClick=
{this.clickIt.bind(this)} />
</Sobject>
<Spot height={200} width={200} color={'rgba(0,0,0,0.91)'} />
</Scene>
</div>
)
}
}
答案 0 :(得分:0)
将level1组件重构如下:
请注意组件顺序的重要性,如果将Spot放在Sobject之后,它将不起作用。
以下几点仅是代码重构
使用箭头功能代替bind(this)
。
要在彼此内部渲染组件,可以使用父组件中的组件,而不必使用{this.props.children}
,除非您有特定的原因
// level1 component
import React from "react";
import Scene from "../components/Scene.js";
import Sobject from "../components/Object.js";
import Spot from "./Spotlight.js";
export default class Level1 extends React.Component {
render() {
return (
<div>
<Scene>
<Spot height={200} width={200} color={"rgba(0,0,0,0.91)"} />
<Sobject name={"room"} xPos={0} yPos={0}/>
</Scene>
</div>
);
}
}
// spotlight组件
import React from "react";
export default class Spot extends React.Component {
state = { x: 0, y: 0 };
_onMouseMove =(e) =>{
this.setState({ x: e.screenX, y: e.screenY });
}
render() {
const { x, y } = this.state;
return (
<div
className={"spot"}
onMouseMove={this._onMouseMove}
style={{
background: `radial-gradient(${this.props.height}px ${
this.props.width
}px at ${x}px ${y}px , transparent 90%, ${this.props.color})`,
top: "0",
bottom: "0",
left: "0",
right: "0",
position: "absolute"
}}
/>
);
}
}
//场景组件
import React from "react";
export default class Scene extends React.Component {
renderObjects = () => this.props.children;
render() {
return (
<div
className={"scene-container"}
style={{ position: "absolute", height: "100vh", width: "100%" }}
>
{this.renderObjects()}
</div>
);
}
}
//对象组件
import React from "react";
export default class Sobject extends React.Component {
click =() =>{
console.log("clicked");
}
render() { // below we use default values in case the prop is not there
const { name = "", type = "generic", xPos =0, yPos =0, ismoving =false} = this.props;
return (
<div
className={"object-container"}
style={{ left: xPos, top: yPos, position: "absolute" }}
>
<h1 onClick={this.click}>Hello</h1>
</div>
);
}
}