本篇內(nèi)容介紹了“react如何實(shí)現(xiàn)浮動(dòng)菜單”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)秦淮免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過(guò)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
react實(shí)現(xiàn)浮動(dòng)菜單的方法:1、利用onMouseOver和onMouseLeave來(lái)監(jiān)聽鼠標(biāo)的變化;2、在樣式中設(shè)置父類及子類的position值;3、設(shè)置父類值為relative,子類值為absolute,并在菜單的css中加入“z-index:999;”;4、通過(guò)控制display來(lái)控制顯示與否即可。
React中hover懸浮菜單的做法
對(duì)于懸浮菜單,主要是借助html標(biāo)簽的事件機(jī)制,或者h(yuǎn)over來(lái)實(shí)現(xiàn),先看下效果圖:
當(dāng)鼠標(biāo)放在名字上時(shí),彈出菜單,離開時(shí),菜單消失。
1.先說(shuō)下利用事件機(jī)制做法:
在事件機(jī)制中,主要利用鼠標(biāo)的一些事件來(lái)監(jiān)聽,具體如下:
可以利用onMouseOver(鼠標(biāo)進(jìn)入),onMouseLeave (鼠標(biāo)離開)來(lái)監(jiān)聽鼠標(biāo)的變化
class UserMenu extends React.Component{
constructor(props){
super(props),
this.state={
modalIsOpen:'none',
atUserItems:false,
}
this.contentBtn=this.contentBtn.bind(this),
this.programBtn=this.programBtn.bind(this),
this.handleMouseOver = this.handleMouseOver.bind(this);
this.handleMouseOut = this.handleMouseOut.bind(this);
this.userCenter = this.userCenter.bind(this);
this.handleMouseUserOver = this.handleMouseUserOver.bind(this);
}
contentBtn(){
this.context.router.history.push("/details");
}
programBtn(){
this.context.router.history.push("/gui");
}
handleMouseOver(e){
this.setState({
modalIsOpen: 'block',
})
}
handleMouseOut(){
this.setState({
modalIsOpen: 'none',
})
}
handleMouseUserOver(e){
this.setState({
modalIsOpen: 'block',
})
}
userCenter(){
this.setState({
modalIsOpen: 'none',
})
}
render(){
const {username} = this.props;
return(
<div className={styles.body} >
<div className={styles.uname}
onMouseOver={this.handleMouseOver}
onMouseLeave={this.handleMouseOut}
>{username}</div>
<div className={styles.menus}
style={{display:this.state.modalIsOpen}}
onMouseOver={this.handleMouseUserOver}
onMouseLeave={this.handleMouseOut}
>
<ul className={styles.ul}>
<li className={styles.li} onClick={this.userCenter}>個(gè)人中心</li>
<li className={styles.li} >賬號(hào)設(shè)置</li>
<li className={styles.li} >注銷</li>
</ul>
</div>
</div>
)
}
}
UserMenu.contextTypes = {
router: PropTypes.object.isRequired
};
export default UserMenu
同時(shí) 需在樣式中設(shè)置父類及子類的position值,父類值為 relative,子類值為 absolute,同時(shí)為使懸浮菜單在最前端顯示,菜單的css中需加入 z-index:999;(數(shù)值越大,越靠前端,999最大值)
.body{
position:relative
}
.menus{
display:none;
position:absolute;
right: 0;
z-index:999;
}
.uname{
color: white;
margin-left: 5px;
margin-right: 10px;
cursor: pointer;
padding-top: 25px;
padding-bottom: 20px;
padding-left: 5px;
}
.uname:hover{
color: darkorange;
}
.ul{
width: 120px;
background-color: #fff;
padding: 10px;
border-radius: 8px;
-webkit-box-shadow: 0 5px 10px 0 rgba(12,40,46,0.20);
box-shadow: 0 5px 10px 0 rgba(12,40,46,0.20);
}
.li{
list-style: none;
height: 40px;
display: list-item;
cursor: pointer;
}
.li:hover{
color: darkorange;
}
2.如果通過(guò)hover判斷,需在css中加入 父類:hover .子類{} ,這個(gè)樣式,然后在其中通過(guò)控制display來(lái)控制顯示與否,
如父組件樣式名為A,子組件樣式名為B即需這樣寫: A:hover .B{display:'block'},來(lái)控制。
“react如何實(shí)現(xiàn)浮動(dòng)菜單”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁(yè)名稱:react如何實(shí)現(xiàn)浮動(dòng)菜單
URL網(wǎng)址:http://chinadenli.net/article32/jiihsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開發(fā)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)