如何在javascript中聚焦div元素并停止默认滚动

时间:2018-10-17 06:41:13

标签: javascript reactjs

我一直在尝试着重于div元素,但是焦点被页面滚动覆盖并停止了窗口滚动。 我是ReactJS的新手,正在尝试将此操纵杆和键盘箭头包含在控制器中。 谢谢您的帮助 这是我的代码

import React from 'react';
import { Joystick } from 'react-joystick-component';
import ArrowKeysReact from 'arrow-keys-react';
export default class Newjoystick extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        content: 'Use arrow keys on your keyboard!'
    };
    ArrowKeysReact.config({
        left: () => {
            this.setState({
                content: 'left key detected.'
            });
        },
        right: () => {
            this.setState({
                content: 'right key detected.'
            });
        },
        up: () => {
            this.setState({
                content: 'up key detected.'
            });
        },
        down: () => {
            this.setState({
                content: 'down key detected.'
            });
        }
    });
}
handleMove(event) {
    console.log('handle move' + event.direction);
    console.log('handle move' + event.x);
    console.log('handle move' + event.y);
    console.log('handle move' + event.type);
}

handleStop() {
    console.log('handle stop');
}
checking() {
    console.log('arrow');
}

focus() {
     this.refs.keyBoard.focus();
}

render() {
    return (
        <div>
            <div {...ArrowKeysReact.events} tabIndex="1"
                onClick={this.checking}
                ref="keyBoard"
            >
                {this.state.content}

                <Joystick size={100} baseColor="#b7b7b7" 
  stickColor="#ccc" move={this.handleMove} stop={this.handleStop}> 
  </Joystick>
            </div >
            <div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div><div>
                <p>Something</p>
                <br />
            </div>
        </div>
    )
  }
}

将两个npm组合在一起

请找到以下链接以供参考:

arrow-keys-reactJoystick

0 个答案:

没有答案