[英]How to Show a div near Cursor position on hover in React js?
I have two Text Items, item1 and item2, and a div with some text inside.我有两个文本项,item1 和 item2,还有一个 div,里面有一些文本。 I want to keep the div hidden by default but show it at cursor position when I hover on Text item1 or item2.我想默认隐藏 div,但当我将鼠标悬停在 Text item1 或 item2 上时,它会显示在光标位置。 Please check the sample GIF animation in this Link请查看此链接中的示例 GIF 动画
I tried this in Jquery code inside React.我在 React 中的 Jquery 代码中尝试了这个。 But it gives error.但它给出了错误。
$(".text-item").mouseenter(function (e) {
$(".box")
.css({
position: "absolute",
right: e.pageX,
top: e.pageY,
display: "block",
})
.show();
});
Try this:尝试这个:
import {useState, Fragment} from react;
const [visible, setVisible] = useState(false)
const MyApp = ()=> {
return (
<Fragment> {
visible &&
<div className="hiddenContainer>
<p>Your hidden text</p>
</div>
}
</Fragment>
<p onMouseEnter={()=>setVisible(!visible)}>Item 1</p>
<p onMouseEnter={()=>setVisible(!visible)}>Item 2</p>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.