最近有个项目需求是点击问号显示提示窗口,窗口固定不随滚动条滚动,并可在范围内拖拽。又不影响下层表单输入。
我想到用antd-card结合react-draggable来实现。这里记录下实现代码:
import React from 'react';
import { Modal, Button, Card, Icon } from 'antd';
import Draggable from 'react-draggable';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
cardShow: false,
disabled: true,
bounds: { left: 0, top: 0, bottom: 0, right: 0 }, // 初始化拖动边界
};
this.draggleRef = React.createRef();
}
onStart = (event, uiData) => {
const { clientWidth, clientHeight } = window.document.documentElement;
const targetRect = this.draggleRef.current.getBoundingClientRect();
this.setState({
// 设置拖动边界,限定初始位置的上下左右可拖动距离,如下设置就是在浏览器视窗内可拖动
// 如果页面有固定显示头部,须在头部以下可拖动,可以设置 top: -targetRect.top + uiData.y + 头部高度,
bounds: {
left: -targetR