JavaScript實(shí)現(xiàn)下拉菜單的顯示隱藏
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)下拉菜單顯示隱藏的具體代碼,供大家參考,具體內(nèi)容如下
有時(shí)需要這種頁(yè)面效果:
鼠標(biāo)移動(dòng)到元素上面時(shí),實(shí)現(xiàn)下拉菜單
鼠標(biāo)移開元素后,下拉菜單不見了
實(shí)現(xiàn)思路
1、一個(gè)盒子里包含上下兩部分,下面部分為子菜單,先設(shè)置為隱藏:display: none;
2、當(dāng)鼠標(biāo)移動(dòng)到盒子上:觸發(fā)事件- - -onmouseover ,js設(shè)置下面部分子菜單的display值為- - -block,使子菜單顯示
3、鼠標(biāo)移開盒子:觸發(fā)事件- - -onmouseout ,js又設(shè)置下面部分子菜單的display值為- - -none,使子菜單又隱藏起來(lái)
4、字體顏色,背景顏色等樣式的改變,根據(jù)所需進(jìn)行相應(yīng)變化
代碼示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>操作元素-新浪下拉菜單</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul li {
list-style: none;
}
a {
text-decoration: none;
color: #4c4c4c;
}
a:hover {
color: #e88415;
}
.box {
width: 80px;
margin: 50px auto;
font-size: 14px;
color: #4c4c4c;
}
.weibo {
position: relative;
background-color: #fcfcfc;
}
.weibo a {
display: block;
height: 40px;
line-height: 40px;
padding-left: 20px;
}
.change {
color: #f9a74f;
background-color: #edeef0;
}
i {
position: absolute;
top: 50%;
right: 15px;
margin-top: -4px;
width: 5px;
height: 5px;
border-bottom: 1px solid orangered;
border-right: 1px solid orangered;
transform: rotate(45deg);
}
.weiboList {
display: none;
}
.weiboList li a {
display: block;
width: 80px;
height: 33px;
line-height: 33px;
padding-left: 15px;
border-bottom: 1px solid #fecc5b;
background-color: #fff;
}
.weiboList li a:hover {
background-color: #fff5da;
}
</style>
</head>
<body>
<div class="box">
<div class="weibo"><a href="#" >微博<i class="select"></i></a></div>
<ul class="weiboList">
<li><a href="#" >私信</a></li>
<li><a href="#" >評(píng)論</a></li>
<li><a href="#" >@我</a></li>
</ul>
</div>
<script>
var box = document.querySelector('.box');
var weibo = document.querySelector('.weibo');
var weiboList = document.querySelector('.weiboList');
box.onmouseover = function() {
weibo.className = 'weibo change'
weiboList.style.display = 'block';
}
box.onmouseout = function() {
weibo.className = 'weibo';
weiboList.style.display = 'none';
}
</script>
</body>
</html>
頁(yè)面效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js String對(duì)象中常用方法小結(jié)(字符串操作)
js String對(duì)象中常用方法小結(jié),需要的朋友可以參考下2012-01-01
JavaScript從數(shù)組的indexOf()深入之Object的Property機(jī)制
這篇文章主要介紹了JavaScript從數(shù)組的indexOf()深入——Object的Property機(jī)制的相關(guān)資料,需要的朋友可以參考下2016-05-05
extract-text-webpack-plugin用法詳解
這篇文章主要介紹了extract-text-webpack-plugin用法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
JavaScript易錯(cuò)知識(shí)點(diǎn)整理
本文主要對(duì)JavaScript易錯(cuò)知識(shí)點(diǎn)進(jìn)行整理和匯總。需要的朋友可以看下,希望對(duì)大家有所幫助2016-12-12
微信小程序云開發(fā)實(shí)現(xiàn)云數(shù)據(jù)庫(kù)讀寫權(quán)限
這篇文章主要為大家詳細(xì)介紹了微信小程序云開發(fā)實(shí)現(xiàn)云數(shù)據(jù)庫(kù)讀寫權(quán)限,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
OfflineSave離線保存代碼再次發(fā)布使用說(shuō)明
OfflineSave離線保存代碼再次發(fā)布使用說(shuō)明...2007-05-05

