import Styles from './index.less';
import { useEffect, useRef, useState } from 'react';
const AdviceBox = (props: any) => {
const { onBoxClick, show } = props;
const activeBox: any = useRef(null);
let left = document.documentElement.clientWidth - 200
let top = document.documentElement.clientHeight - 50;
const [activeInterval, setActiveInterval] = useState(null);
const [max_left, setMax_left] = useState<number>(left);
const [max_top, setMax_top] = useState<number>(top);
let active_x: number = 1;
let active_y: number = 1;
useEffect(() => {
startTimer();
return () => {
clearTimer();
};
}, []);
const changePos = () => {
let old_left = activeBox.current?.offsetLeft;
let old_top = activeBox.current?.offsetTop;
let new_left = old_left + active_x;
let new_top = old_top + active_y;
if (new_left > max_left) {
new_left = max_left;
}
if (new_top > max_top + document.documentElement.scrollTop) {
new_top = max_top + document.documentElement.scrollTop;
}
if (new_left < 0) {
new_left = 0;
}
if (new_top < document.documentElement.scrollTop) {
new_top = document.documentElement.scrollTop;
}
if (activeBox.current) {
activeBox.current.style.left = new_left + 'px';
activeBox.current.style.top = new_top + 'px';
}
if (new_top >= max_top + document.documentElement.scrollTop) {
active_y = -1;
}
if (new_top == document.documentElement.scrollTop) {
active_y = 1;
}
if (new_left >= max_left) {
active_x = -1;
}
if (new_left == 0) {
active_x = 1;
}
};
const clearTimer = () => {
if (activeInterval) {
clearInterval(activeInterval);
setActiveInterval(null);
}
};
const startTimer = () => {
const interval: any = setInterval(changePos, 10);
setActiveInterval(interval);
};
const onClickBox = () => {
clearTimer();
onBoxClick();
};
const resetActive = () => {
setMax_left(document.documentElement.clientWidth - activeBox.current?.offsetWidth);
setMax_top(document.documentElement.clientHeight - activeBox.current?.offsetHeight);
activeBox.current.style.left = 0;
activeBox.current.style.top = 0;
active_x = 1;
active_y = 1;
};
window.onresize = () => {
resetActive();
};
window.onscroll = () => {};
return (
<div
ref={activeBox}
className={Styles.advice_box}
style={show ? { display: 'none' } : { display: 'block' }}
onMouseOver={clearTimer}
onMouseLeave={startTimer}
onClick={() => onClickBox()}
>
<div className={Styles.advice_box_content}>
<div className={Styles.icon}>
<span
className="iconfont icon-message-reply"
style={{
position: 'absolute',
top: '-27rem',
left: '-7rem',
fontSize: '75rem',
color: '#FCCA02',
}}
/>
<span
className="iconfont icon-fankuixuanzhong"
style={{
position: 'absolute',
top: '-2rem',
left: '14rem',
fontSize: '35rem',
color: '#fff',
}}
/>
</div>
<div>ご意見箱</div>
</div>
</div>
);
};
export default AdviceBox;